Switch
native checkbox input に role=switch を適用し、form とブラウザーの動作を維持します。checked、defaultChecked、onCheckedChange に対応します。
@neumorphism-ui/switchプレビュー
インストール
インストール: switch
npx shadcn@latest add @neumorphism-ui/switchCLI が npm 依存関係、base token、utils、コンポーネントソースを順番に処理します。
この Registry を初めて使う場合は、先に namespace の設定 を完了してください。
使い方
使い方: インストール
npx shadcn@latest add @neumorphism-ui/switch @neumorphism-ui/labelImport
Switch の import コード
import { Switch } from "@/components/ui/switch"
import { Label } from "@/components/ui/label"Example
Switch の使用コード
<div className="flex items-center justify-between gap-4"> <Label htmlFor="system-theme">システムテーマを使用</Label> <Switch id="system-theme" name="systemTheme" defaultChecked /> </div>
API リファレンス
| コンポーネント | プロパティ | 型 | デフォルト | 説明 |
|---|---|---|---|---|
Switch | checked / defaultChecked | boolean | — | 制御されたオン状態、または非制御時の初期状態です。 |
Switch | onCheckedChange | (checked: boolean) => void | — | オン状態が変わったときに呼び出されます。 |
Switch | onChange | React.ChangeEventHandler<HTMLInputElement> | — | native checkbox の change event を受け取ります。 |
Switch | disabled | boolean | false | input とポインターによる操作を無効にします。 |
Switch | name / value / required | native input props | — | form 送信に使用する native checkbox 属性です。 |
アクセシビリティ
- 01
実際の checkbox に role=switch を適用することで、checked 状態を支援技術に伝え、Space キーの操作も維持します。
- 02
Label の htmlFor と Switch の id を関連付けるか、aria-label を指定してください。
- 03
即時適用されず、別途 save action が必要な boolean 選択には Checkbox の方が適している場合があります。