ドキュメントを検索

はじめに

leve-uiをインストールして、最初のコンポーネントをレンダリングします。

要件#

  • Qwik 1.x(@builder.io/qwik)。Qwikはpeer dependencyであるため、leve-uiはプロジェクト内のバージョンを使用します。

インストール#

個別のパッケージをインストールするか、@leve-ui/coreを使用してすべてのコンポーネントを一度に取得します。

pnpm add @leve-ui/switch

どちらも同じAPIを公開します。唯一の違いはインポートパスです:

import { Switch } from '@leve-ui/switch';
// or
import { Switch } from '@leve-ui/core';

個別パッケージを使用すると、使用しないコンポーネントを依存関係から除外できます。これは、leve-uiを別のコンポーネントライブラリと組み合わせる際に便利です。

最初のコンポーネント#

コンポーネントのパーツを組み合わせ、クラスを付与します:

import { component$, useStyles$ } from '@builder.io/qwik';
import { Switch } from '@leve-ui/switch';
import styles from './airplane-mode.css?inline';

export default component$(() => {
  useStyles$(styles);

  return (
    <Switch.Root class="switch">
      <Switch.Label class="switch-label">Airplane mode</Switch.Label>
      <Switch.Control class="switch-control">
        <Switch.Thumb class="switch-thumb" />
      </Switch.Control>
    </Switch.Root>
  );
});

次にパーツをスタイル設定します。状態はdata-stateとして公開されます:

.switch {
  display: inline-flex;
  align-items: center;
  gap: 12px;
}

.switch-control {
  width: 36px;
  height: 20px;
  padding: 2px;
  border: none;
  border-radius: 9999px;
  background: #cbd5e1;
}

.switch-control[data-state='checked'] {
  background: #009dfd;
}

.switch-thumb {
  display: block;
  width: 16px;
  height: 16px;
  border-radius: 9999px;
  background: #ffffff;
  transition: transform 0.15s;
}

.switch-control[data-state='checked'] .switch-thumb {
  transform: translateX(16px);
}

Switchのデモのスタイルを適用した例がこちらです:

import { component$, useStyles$ } from '@builder.io/qwik';
import { Switch } from '@leve-ui/switch';
import styles from './default.css?inline';

export default component$(() => {
  useStyles$(styles);

  return (
    <Switch.Root class="switch-default-root">
      <Switch.Label class="switch-default-label">Airplane mode</Switch.Label>
      <Switch.Control class="switch-default-control">
        <Switch.Thumb class="switch-default-thumb" />
      </Switch.Control>
    </Switch.Root>
  );
});

Controlはaria-checkedを持つ<button role="switch">であり、Labelがそれに関連付けられているため、スイッチはキーボード(Space、Enter)で操作でき、スクリーンリーダーによって正しく読み上げられます。それらを独自に記述する必要はありませんでした。

フォームでの使用#

Switch、Checkbox、Radio GroupなどのフォームコントロールにはHiddenInputパーツがあります。ネイティブフォーム送信やFormDataに値を含めるには、nameとともにこれを追加します:

<Switch.Root>
  <Switch.Label>Notifications</Switch.Label>
  <Switch.Control>
    <Switch.Thumb />
  </Switch.Control>
  <Switch.HiddenInput name="notifications" />
</Switch.Root>

例について#

このサイトの例はプレーンなTSXおよびCSSファイルです。プロジェクトにそのままコピーしてください。leve-uiとQwikのみを使用しており、少数のアイコン(Lucide)用に@qwikest/iconsが追加されています。各例はCSSのlight-dark()関数を通じてライトテーマとダークテーマに対応しており、ページのcolor-schemeに従います。

バージョニング#

leve-uiは0.xシリーズです。1.0までは、破壊的変更はマイナーバージョン(0.1 → 0.2)としてリリースされ、変更履歴に記載されます。計画的にアップデートしたい場合は、~0.1.0などのマイナーレンジで固定してください。

次のステップ#