チェックボックス
stimeo--checkbox
ネイティブのチェックボックスに、3-state の親子連動(すべて選択)と change イベントを追加する。
stimeo--checkbox コントローラは、ネイティブの <input type="checkbox"> では表せない振る舞いを補います。子の状態から親の indeterminate プロパティ(AT には「mixed」と伝わる)を算定し、親操作で子を一括設定します。集約状態(all / partial / none)はルートの data-state に反映され、change ごとに stimeo--checkbox:change を発火します。チェックマークや「一部選択」の見た目はこの Playground 側が持ちます。
1 件を選択中
キーボード操作
| キー | 動作 |
|---|---|
| Space | フォーカス中のチェックボックスをトグル(ネイティブ)。 |
<%# Markup for the checkbox (APG Checkbox / 3-state) demo.
The parent ("select all") derives checked / indeterminate from the children's
state, and a parent toggle sets all children at once. The aggregate state is
reflected on the root's data-state (all/partial/none); the look is the consumer's CSS. %>
<div class="checkbox-demo" role="group" aria-labelledby="cb-group-label"
data-controller="stimeo--checkbox">
<span class="checkbox-demo__legend" id="cb-group-label">
<%= t("components.checkbox.demo.group") %>
</span>
<label class="checkbox-demo__row checkbox-demo__row--parent">
<input type="checkbox" data-stimeo--checkbox-target="parent"
data-action="change->stimeo--checkbox#onParentChange" />
<span><%= t("components.checkbox.demo.all") %></span>
</label>
<div class="checkbox-demo__children">
<label class="checkbox-demo__row">
<input type="checkbox" checked data-stimeo--checkbox-target="child"
data-action="change->stimeo--checkbox#onChildChange" />
<span><%= t("components.checkbox.demo.item_a") %></span>
</label>
<label class="checkbox-demo__row">
<input type="checkbox" data-stimeo--checkbox-target="child"
data-action="change->stimeo--checkbox#onChildChange" />
<span><%= t("components.checkbox.demo.item_b") %></span>
</label>
<label class="checkbox-demo__row">
<input type="checkbox" data-stimeo--checkbox-target="child"
data-action="change->stimeo--checkbox#onChildChange" />
<span><%= t("components.checkbox.demo.item_c") %></span>
</label>
</div>
<%# Subscribe to the change event and show the selected count (updated by demo.js). %>
<p class="checkbox-demo__status" aria-live="polite">
<span data-checkbox-count>1</span> <%= t("components.checkbox.demo.selected") %>
</p>
</div>
/*
* Presentation-only styles for the checkbox demo.
* The aggregate state is available on the root's data-state (all/partial/none).
*/
.checkbox-demo {
display: flex;
flex-direction: column;
gap: 0.4rem;
padding: 0;
border: 0;
max-width: 22rem;
}
.checkbox-demo__legend {
font-weight: 600;
margin-bottom: 0.25rem;
}
.checkbox-demo__row {
display: flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
}
.checkbox-demo__row input {
width: 1.1rem;
height: 1.1rem;
accent-color: var(--accent);
}
.checkbox-demo__row--parent {
font-weight: 600;
padding-bottom: 0.4rem;
border-bottom: 1px solid var(--border);
}
.checkbox-demo__children {
display: flex;
flex-direction: column;
gap: 0.35rem;
padding-left: 1.25rem;
}
.checkbox-demo__status {
margin: 0.5rem 0 0;
font-size: 0.85rem;
color: var(--color-text-muted);
}
// Demo script: subscribe to the checkbox change event and show how many children are checked.
document.addEventListener('stimeo--checkbox:change', function () {
const checked = document.querySelectorAll(
'.checkbox-demo [data-stimeo--checkbox-target="child"]:checked',
).length;
const out = document.querySelector('[data-checkbox-count]');
if (out) out.textContent = String(checked);
});
これらのスタイルは共通のデザイントークン(ライト/ダーク両対応)を使います。 共通スタイルも一緒にコピーし、ルート要素の data-theme を切り替えればダークになります。
このコンポーネントを動かすために HTML へ記述する data-* 属性です。ルート要素に下の data-controller を付け、その内側に各 target / value / action を配置します。
ルート要素に付与
data-controller="stimeo--checkbox"
ターゲット
| 名前 | 説明 | 属性 |
|---|---|---|
parent
必須
|
「すべて選択」チェックボックス。checked/indeterminate は子から導出される。 | data-stimeo--checkbox-target="parent" |
child
|
グループ内の個別チェックボックス。親の集約状態の算出に用いられる。 | data-stimeo--checkbox-target="child" |
アクション
| 名前 | 説明 | アクション |
|---|---|---|
onChildChange
|
子の変更時に、子から親の checked/indeterminate を再計算する。 | stimeo--checkbox#onChildChange |
onParentChange
|
親の checked 状態を全ての子へ伝播し、indeterminate を解除する。 | stimeo--checkbox#onParentChange |
イベント
| 名前 | 説明 | イベント |
|---|---|---|
change
|
状態変化のたびに発火。detail は { checked, indeterminate, state }(state は all/partial/none)。 |
stimeo--checkbox:change |
状態フック
ライブラリが操作するのはこれらの ARIA / data 属性、カスタムプロパティだけです。見た目は利用側 CSS がこれらに反応して作ります([aria-selected] / [aria-expanded] / var(--stimeo--…) などのセレクタでフックします)。
| フック | 対象 | 意味 |
|---|---|---|
indeterminate |
親 | 子が一部だけ選択のとき true(AT には「mixed」)。 |
data-state |
ルート | "all" / "partial" / "none"(親子連動の集約状態)。 |