本文へ移動
日本語

仕組み

One file, two components. Checkbox is a <label> around a transparent native input that covers it, so clicks, Space, screen readers and form submission all work natively; the box, tick and dash are decoration behind it. The empty box is white with a soft inner ring; checked it fills with the accent and the tick draws from start to end, while the indeterminate dash grows from the middle. Pressing dips the box and release springs it back past 1×. Rings and the focus halo are box-shadows, so state never changes size. CheckboxGroup renders options and returns values in options order.

向いている場面

  • Choosing zero or more options: filters, permissions, notification preferences.
  • A single yes or no that takes effect on submit: agreeing to terms, remembering a login.
  • A select-all above a list, using indeterminate for a partial selection.

向かない場面

  • Only one choice allowed: use a radio group.
  • Settings that apply immediately, such as dark mode: use a switch; a checkbox means “applies on submit”.
  • More than about 8 options, or options that need search: use a multi-select dropdown.

使い方

import { useState } from 'react'
import { Checkbox, CheckboxGroup } from './Checkbox.jsx'

// Single: agree to terms (uncontrolled; submits agree=on with the form)
export function Agreement() {
  return <Checkbox name="agree" label="I have read and agree to the terms" required />
}

// A group with select-all: the master box controls only the enabled options
const TOPICS = [
  { value: 'news', label: 'Product news' },
  { value: 'tips', label: 'Tips and tutorials' },
  { value: 'beta', label: 'Beta programme', description: 'Currently full', disabled: true },
]
const OPEN = TOPICS.filter((t) => !t.disabled).map((t) => t.value)

export function Topics() {
  const [picked, setPicked] = useState(['news'])
  const count = OPEN.filter((v) => picked.includes(v)).length
  return (
    <>
      <Checkbox
        label="All topics"
        checked={count === OPEN.length}
        indeterminate={count > 0 && count < OPEN.length}
        onChange={(checked) => setPicked(checked ? OPEN : [])}
      />
      <CheckboxGroup label="Email topics" options={TOPICS} value={picked} onChange={setPicked} />
    </>
  )
}

プロパティ

プロパティ型既定値説明
checkedboolean—Checkbox: controlled checked state. Use it together with onChange.
defaultCheckedbooleanfalseCheckbox: initial state when uncontrolled.
onChange(checked, event) => void—Checkbox: called when the state changes; the second argument is the native event.
indeterminatebooleanfalseCheckbox: shows a dash and is announced as mixed. Visual only; it does not change checked.
labelReactNode—Checkbox: label text; clicking it also toggles. Give aria-label when omitted.
descriptionReactNode—Checkbox: help text under the label, read after the label.
size'sm' | 'md' | 'lg''md'Both components. Box 18 / 20 / 24px with 5 / 6 / 7px corners, label 13 / 14 / 16px. Use lg on mobile.
invalidbooleanfalseBoth components. Error: an unchecked box gets a danger ring and a faint danger tint, and aria-invalid is set.
disabledbooleanfalseBoth components. Disabled: 45% opacity and not clickable; on CheckboxGroup it disables the whole group.
className / stylestring / CSSProperties—Checkbox: applied to the root <label>.
...restInputHTMLAttributes—Checkbox: other attributes (name, value, required, aria-*) go to the native input; the ref points to it too.
options{ label, value, description?, disabled? }[]—CheckboxGroup: the options; value is a string or number.
valueany[]—CheckboxGroup: controlled array of checked values.
defaultValueany[][]CheckboxGroup: initially checked values when uncontrolled.
onChange(value) => void—CheckboxGroup: called with the checked values in options order; values missing from options are kept.
direction'vertical' | 'horizontal''vertical'CheckboxGroup: vertical (14px between rows) or horizontal (20px gap, wraps when full).
namestring—CheckboxGroup: form field name; each checked option submits name=value.
labelstring—CheckboxGroup: accessible name of the group. With a visible title, pass aria-labelledby instead.
...restHTMLAttributes—CheckboxGroup: other attributes go to the root <div role="group">.

アクセシビリティ

  • A native <input type="checkbox">: Tab to focus, Space to toggle, announced as checked or unchecked, and as mixed when indeterminate.
  • Label and description are linked with aria-labelledby and aria-describedby; clicking the label text toggles.
  • A focus ring shows on keyboard focus; in forced-colors mode it becomes an outline and checked uses the system highlight.
  • State is not colour alone: tick and dash are shapes, and invalid also sets aria-invalid.
  • CheckboxGroup is role="group" named by label or aria-labelledby; on touch screens each item is at least 44px tall.

モーション

The tick draws in 240ms ease-out (40ms after the fill) and retracts in 100ms; the dash grows from the middle in 160ms. The fill changes in 160ms and the focus halo grows in over 220ms. Pressing dips the box to 0.86 in 100ms; release springs it back in 380ms with a slight overshoot.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

Checkbox.jsxcheckbox.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。