スイッチ
stimeo--switch
aria-checked だけで状態を表す、アクセシブルなオン/オフ切り替えボタン。
stimeo--switch コントローラは WAI-ARIA の Switch パターンを実装します。状態は aria-checked のみで表現され、クリックまたは Space/Enter でトグルし、新しい真偽値を載せた stimeo--switch:changed イベントを発火します。ライブラリは挙動のみを提供し、見た目(aria-checked で切り替え)はこの Playground 側が持ちます。
実行中
キーボード操作
| キー | 動作 |
|---|---|
| Space / Enter | スイッチのオン / オフを切り替える。 |
<%# Markup for the switch (APG Switch) demo.
The controller element itself is a role="switch" button, with state expressed only via
aria-checked. Space/Enter and click toggle it. The look switches on aria-checked in
the Playground's CSS. %>
<button
type="button"
class="switch"
role="switch"
aria-checked="false"
data-controller="stimeo--switch"
data-action="click->stimeo--switch#toggle keydown->stimeo--switch#onKeydown">
<span class="switch__track" aria-hidden="true"><span class="switch__thumb"></span></span>
<span class="switch__label"><%= t("components.switch.demo.label") %></span>
</button>
/*
* Presentation-only styles for the switch demo.
* On/off is expressed via aria-checked (set by the library).
*/
.switch {
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 0;
background: transparent;
border: 0;
cursor: pointer;
font-size: 1rem;
}
.switch__track {
position: relative;
width: 2.75rem;
height: 1.5rem;
border-radius: 999px;
background: var(--border-strong);
transition: background 0.15s ease;
}
.switch[aria-checked="true"] .switch__track {
background: var(--accent);
}
.switch__thumb {
position: absolute;
top: 0.125rem;
left: 0.125rem;
width: 1.25rem;
height: 1.25rem;
border-radius: 50%;
background: var(--surface-card);
transition: transform 0.15s ease;
}
.switch[aria-checked="true"] .switch__thumb {
transform: translateX(1.25rem);
}
.switch__label {
color: var(--fg);
}
このデモに固有の消費側 JS はありません(挙動はコントローラが担います)。
これらのスタイルは共通のデザイントークン(ライト/ダーク両対応)を使います。 共通スタイルも一緒にコピーし、ルート要素の data-theme を切り替えればダークになります。
このコンポーネントを動かすために HTML へ記述する data-* 属性です。ルート要素に下の data-controller を付け、その内側に各 target / value / action を配置します。
ルート要素に付与
data-controller="stimeo--switch"
アクション
| 名前 | 説明 | アクション |
|---|---|---|
onKeydown
|
ボタン以外のホストで Space/Enter により切り替える(ネイティブ button は対象外)。 | stimeo--switch#onKeydown |
toggle
|
aria-checked のオン/オフ状態を切り替える。 |
stimeo--switch#toggle |
イベント
| 名前 | 説明 | イベント |
|---|---|---|
changed
|
切り替えごとに発火。detail に新しい checked 真偽値を載せる。 | stimeo--switch:changed |
状態フック
ライブラリが操作するのはこれらの ARIA / data 属性、カスタムプロパティだけです。見た目は利用側 CSS がこれらに反応して作ります([aria-selected] / [aria-expanded] / var(--stimeo--…) などのセレクタでフックします)。
| フック | 対象 | 意味 |
|---|---|---|
aria-checked |
ルート要素 | オンで "true"、オフで "false"。 |