🔥 認識我們的新專案 t0ggles - 您終極的專案管理工具! 🔥

使用方式

完成安裝程序後,我們就可以開始在我們的 React 應用程式中使用 Konsta UI 元件。

元件

這個過程非常直接,我們需要從 konsta/react 匯入 Konsta UI React 元件

/* App.jsx */
import React from 'react';
import { Block, Button } from 'konsta/react';

export function MyApp() {
  // ...

  return (
    <>
      <Block>
        <p>This is block with text</p>
      </Block>
      <Block className="space-y-4">
        <p>Here comes the button</p>
        <Button>Action</Button>
      </Block>
    </>
  );
}

App 元件

我們假設您會搭配其他框架(例如 Framework7Ionic)使用 Konsta UI 元件。

但是如果您只使用 Konsta UI,那麼我們需要用 Konsta UI 的App 元件包裝我們所有的元件。

App 元件中,我們可以定義全域主題(iOS 或 Material)和其他有用的全域設定

/* App.jsx */
import React from 'react';
import { App, Page, Navbar, Block } from 'konsta/react';

export default function MyApp() {
  return (
    <App theme="ios">
      <Page>
        <Navbar title="My App" />
        <Block>
          <p>Here comes my app</p>
        </Block>
      </Page>
    </App>
  );
}

Konsta UI Provider

如果您使用 Konsta UI 搭配其他框架,我們仍然可以(而且應該)指定 Konsta UI 的全域設定(例如主題)。對於這種情況,我們需要使用 KonstaProvider

我們還需要在應用程式的根元素中新增 k-iosk-material 類別。

/* App.jsx */
import React from 'react';
// we use main App and Router components from Framework7
import { App, View, Page, Navbar } from 'framework7-react';
// we use KonstaProvider instead
import { KonstaProvider, Block, Button } from 'konsta/react';

export default function MyApp() {
  return (
    <>
      {/* Wrap our app with KonstaProvider */}
      <KonstaProvider theme="ios">
        {/* We add extra `k-ios` class to the app root element */}
        <App theme="ios" className="k-ios">
          <View>
            <Page>
              <Navbar title="My App" />
              {/*  Konsta UI components */}
              <Block>
                <p>Here comes my app</p>
              </Block>
              <Block className="space-y-4">
                <p>Here comes the button</p>
                <Button>Action</Button>
              </Block>
            </Page>
          </View>
        </App>
      </KonstaProvider>
    </>
  );
}
程式碼授權使用 MIT.
2022 © Konsta UI by nolimits4web.