Switch
オプションのオン・オフを切り替えるコントロールです。
SwitchはSwitch WAI-ARIAデザインパターンに従います。
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>
);
});.switch-default-root {
/* HiddenInput is absolutely positioned inside Root */
position: relative;
display: inline-flex;
align-items: center;
justify-content: space-between;
gap: 12px;
min-width: 200px;
font-family: inherit;
}
.switch-default-label {
font-size: 14px;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.switch-default-control {
display: inline-flex;
align-items: center;
flex-shrink: 0;
width: 36px;
height: 20px;
padding: 2px;
border: none;
border-radius: 9999px;
background: light-dark(#cbd5e1, #ffffff3d);
cursor: pointer;
}
.switch-default-control[data-state="checked"] {
background: #009dfd;
}
.switch-default-control:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.switch-default-control:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.switch-default-thumb {
display: block;
width: 16px;
height: 16px;
border-radius: 9999px;
background: #ffffff;
box-shadow: 0 1px 2px rgb(15 23 42 / 0.2);
transition: transform 150ms ease;
}
.switch-default-thumb[data-state="checked"] {
transform: translateX(16px);
}インストール#
個別のパッケージをインストールするか、@leve-ui/coreを使用してすべてのコンポーネントを一度に取得します。
pnpm add @leve-ui/switchnpm install @leve-ui/switchyarn add @leve-ui/switchbun add @leve-ui/switchpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/core構造#
import { Switch } from '@leve-ui/switch'; // or '@leve-ui/core'
<Switch.Root>
<Switch.Label />
<Switch.Control>
<Switch.Thumb />
</Switch.Control>
<Switch.HiddenInput />
</Switch.Root>
| パーツ | 要素 | 説明 |
|---|---|---|
Switch.Root | <div> | 各パーツをラップし、オン/オフの状態を保持します。インタラクティブではありません。 |
Switch.Label | <label> | ラベル。クリックするとスイッチが切り替わります。 |
Switch.Control | <button> | スイッチ自体で、role="switch"を持ちます。 |
Switch.Thumb | <span> | コントロール内のサムです。装飾用です。 |
Switch.HiddenInput | <input type="checkbox"> | 省略可能。ネイティブの<form>で状態を送信します。 |
例#
Checked#
defaultCheckedでスイッチを初期状態でオンにします。
import { component$, useStyles$ } from '@builder.io/qwik';
import { Switch } from '@leve-ui/switch';
import styles from './checked.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Switch.Root defaultChecked class="switch-checked-root">
<Switch.Label class="switch-checked-label">Wi-Fi</Switch.Label>
<Switch.Control class="switch-checked-control">
<Switch.Thumb class="switch-checked-thumb" />
</Switch.Control>
</Switch.Root>
);
});.switch-checked-root {
/* HiddenInput is absolutely positioned inside Root */
position: relative;
display: inline-flex;
align-items: center;
justify-content: space-between;
gap: 12px;
min-width: 200px;
font-family: inherit;
}
.switch-checked-label {
font-size: 14px;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.switch-checked-control {
display: inline-flex;
align-items: center;
flex-shrink: 0;
width: 36px;
height: 20px;
padding: 2px;
border: none;
border-radius: 9999px;
background: light-dark(#cbd5e1, #ffffff3d);
cursor: pointer;
}
.switch-checked-control[data-state="checked"] {
background: #009dfd;
}
.switch-checked-control:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.switch-checked-control:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.switch-checked-thumb {
display: block;
width: 16px;
height: 16px;
border-radius: 9999px;
background: #ffffff;
box-shadow: 0 1px 2px rgb(15 23 42 / 0.2);
transition: transform 150ms ease;
}
.switch-checked-thumb[data-state="checked"] {
transform: translateX(16px);
}Disabled#
Rootのdisabledでスイッチを無効化します。
import { component$, useStyles$ } from '@builder.io/qwik';
import { Switch } from '@leve-ui/switch';
import styles from './disabled.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Switch.Root disabled class="switch-disabled-root">
<Switch.Label class="switch-disabled-label">Bluetooth</Switch.Label>
<Switch.Control class="switch-disabled-control">
<Switch.Thumb class="switch-disabled-thumb" />
</Switch.Control>
</Switch.Root>
);
});.switch-disabled-root {
font-family: inherit;
/* HiddenInput is absolutely positioned inside Root */
position: relative;
display: inline-flex;
align-items: center;
justify-content: space-between;
gap: 12px;
min-width: 200px;
}
.switch-disabled-label {
font-size: 14px;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.switch-disabled-control {
display: inline-flex;
align-items: center;
flex-shrink: 0;
width: 36px;
height: 20px;
padding: 2px;
border: none;
border-radius: 9999px;
background: light-dark(#cbd5e1, #ffffff3d);
cursor: pointer;
}
.switch-disabled-control[data-state="checked"] {
background: #009dfd;
}
.switch-disabled-control:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.switch-disabled-control:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.switch-disabled-thumb {
display: block;
width: 16px;
height: 16px;
border-radius: 9999px;
background: #ffffff;
box-shadow: 0 1px 2px rgb(15 23 42 / 0.2);
transition: transform 150ms ease;
}
.switch-disabled-thumb[data-state="checked"] {
transform: translateX(16px);
}
.switch-disabled-label[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}フォーム#
nameを指定したSwitch.HiddenInputを追加して、ネイティブの<form>で状態を送信します。値はスイッチがオンのときのみ送信されます。
import { $, component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Switch } from '@leve-ui/switch';
import styles from './form.css?inline';
export default component$(() => {
useStyles$(styles);
const submittedData = useSignal('');
const handleSubmit$ = $((_event: unknown, form: HTMLFormElement) => {
const formData = new FormData(form);
submittedData.value = JSON.stringify(Object.fromEntries(formData));
});
return (
<form class="switch-form-form" preventdefault:submit onSubmit$={handleSubmit$}>
<Switch.Root class="switch-form-root">
<Switch.Label class="switch-form-label">Notifications</Switch.Label>
<Switch.Control class="switch-form-control">
<Switch.Thumb class="switch-form-thumb" />
</Switch.Control>
<Switch.HiddenInput name="notifications" />
</Switch.Root>
<button type="submit" class="switch-form-submit">
Save
</button>
<output class="switch-form-output">{submittedData.value}</output>
</form>
);
});.switch-form-form {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 12px;
font-family: inherit;
}
.switch-form-root {
/* HiddenInput is absolutely positioned inside Root */
position: relative;
display: inline-flex;
align-items: center;
justify-content: space-between;
gap: 12px;
min-width: 200px;
}
.switch-form-label {
font-size: 14px;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.switch-form-control {
display: inline-flex;
align-items: center;
flex-shrink: 0;
width: 36px;
height: 20px;
padding: 2px;
border: none;
border-radius: 9999px;
background: light-dark(#cbd5e1, #ffffff3d);
cursor: pointer;
}
.switch-form-control[data-state="checked"] {
background: #009dfd;
}
.switch-form-control:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.switch-form-control:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.switch-form-thumb {
display: block;
width: 16px;
height: 16px;
border-radius: 9999px;
background: #ffffff;
box-shadow: 0 1px 2px rgb(15 23 42 / 0.2);
transition: transform 150ms ease;
}
.switch-form-thumb[data-state="checked"] {
transform: translateX(16px);
}
.switch-form-submit {
height: 32px;
padding: 0 12px;
border: none;
border-radius: 6px;
background: #009dfd;
color: #ffffff;
font: inherit;
font-size: 13px;
font-weight: 500;
cursor: pointer;
}
.switch-form-submit:hover {
background: #006ce9;
}
.switch-form-submit:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.switch-form-output {
margin: 0;
font-size: 13px;
color: light-dark(#475569, #c9ddf0);
}Fieldと併用#
Field.Root内では、Switch.Labelの代わりにField.Labelを使用してください。FieldがID、説明、およびdisabled/required/invalid状態を提供します。
import { component$, useStyles$ } from '@builder.io/qwik';
import { Field } from '@leve-ui/field';
import { Switch } from '@leve-ui/switch';
import styles from './with-field.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Field.Root hasDescription class="switch-with-field-field">
<div class="switch-with-field-row">
<Field.Label class="switch-with-field-label">Dark mode</Field.Label>
<Switch.Root class="switch-with-field-root">
<Switch.Control class="switch-with-field-control">
<Switch.Thumb class="switch-with-field-thumb" />
</Switch.Control>
</Switch.Root>
</div>
<Field.Description class="switch-with-field-description">
Use a dark color scheme.
</Field.Description>
</Field.Root>
);
});.switch-with-field-field {
display: flex;
flex-direction: column;
gap: 6px;
width: 280px;
font-family: inherit;
}
.switch-with-field-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.switch-with-field-label {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.switch-with-field-description {
margin: 0;
font-size: 13px;
color: light-dark(#475569, #c9ddf0);
}
.switch-with-field-root {
display: inline-flex;
}
.switch-with-field-control {
display: inline-flex;
align-items: center;
flex-shrink: 0;
width: 36px;
height: 20px;
padding: 2px;
border: none;
border-radius: 9999px;
background: light-dark(#cbd5e1, #ffffff3d);
cursor: pointer;
}
.switch-with-field-control[data-state="checked"] {
background: #009dfd;
}
.switch-with-field-control:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.switch-with-field-control:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.switch-with-field-thumb {
display: block;
width: 16px;
height: 16px;
border-radius: 9999px;
background: #ffffff;
box-shadow: 0 1px 2px rgb(15 23 42 / 0.2);
transition: transform 150ms ease;
}
.switch-with-field-thumb[data-state="checked"] {
transform: translateX(16px);
}APIリファレンス#
Root#
| Prop | デフォルト | 型 |
|---|---|---|
| ids | – | { control?: string; label?: string } パーツの要素IDをオーバーライドします。Fieldの外でのみ使用されます。 |
| defaultChecked | false | boolean スイッチが初期状態でオンかどうか(非制御)。 |
| bind:checked | – | Signal<boolean> オン/オフ状態を保持するシグナル(制御)。 |
| onCheckedChange$ | – | QRL<(checked: boolean) => void> ユーザーがスイッチをオンまたはオフにしたときに呼び出されます。 |
| disabled | false | boolean スイッチを無効化します。Fieldの内部では、Fieldの |
| required | false | boolean スイッチを必須としてマークします。Fieldの内部では、Fieldの |
すべての<div>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-switch | 常に存在 |
data-state | "checked" | "unchecked" |
data-disabled | 無効時に存在 |
状態はdefaultCheckedで非制御、またはbind:checkedで制御できます。onCheckedChange$は、ユーザーがスイッチをオンまたはオフにしたときに呼び出されます。
Fieldの内部では、IDはFieldから提供され、idsは無視されます(ADR 0024)。
Label#
独自のpropはありません。 すべての<label>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-switch-label | 常に存在 |
data-state | "checked" | "unchecked" |
data-disabled | 無効時に存在 |
Control#
独自のpropはありません。 すべての<button>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-switch-control | 常に存在 |
data-state | "checked" | "unchecked" |
data-disabled | 無効時に存在 |
data-invalid | Fieldが無効な場合に存在 |
サム#
独自のpropはありません。 すべての<span>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-switch-thumb | 常に存在 |
data-state | "checked" | "unchecked" |
data-disabled | 無効時に存在 |
[data-leve-switch-thumb][data-state="checked"]などのCSSでサムを移動します。
HiddenInput#
独自のpropはありません。 すべての<input>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-switch-hidden-input | 常に存在 |
data-state | "checked" | "unchecked" |
data-disabled | 無効時に存在 |
非表示のinputはchecked、required、disabledの状態に追従するため、ネイティブのフォーム送信とrequiredバリデーションが機能します(ADR 0026)。インラインスタイルで絶対配置されるため、Switch.Rootにposition: relativeを指定してください。
アクセシビリティ#
Switch WAI-ARIAデザインパターンに準拠しています。
Switch.Label、Field.Label、またはSwitch.Control上のaria-label/aria-labelledbyでスイッチに名前を付けます。Switch.Controlはrole="switch"およびaria-checked("true"または"false")、必須時はaria-requiredを持つ<button type="button">です。Fieldの内部ではaria-describedbyとaria-invalidもレンダリングします。無効時はネイティブのdisabled属性を使用するため、フォーカスを受け取りません。- 状態によってラベルのテキストを変更しないでください。状態は
aria-checkedによって伝達されます。 Switch.ThumbとSwitch.HiddenInputはaria-hidden="true"によって支援技術から隠されます。非表示のinputはタブシーケンスに含まれません。
キーボード操作#
| キー | 説明 |
|---|---|
Space | スイッチをオンまたはオフにします。 |
Enter | スイッチをオンまたはオフにします。 |