No results found

Checkbox

A control that enables users to make binary (or ternary) choices through selection and deselection.

Features

  • WAI ARIA Checkbox design pattern
  • Indeterminate state support
  • Form binding with hidden native input
  • Error message handling and validation
  • Custom description text with screenreader support
  • Reactive state management with signals
  • Keyboard navigation with Enter key handling
  • Accessible labeling system
  • Disabled state management
  • Two-way data binding with bind:checked prop
  • Custom onChange event handling
  • Automatic ARIA state management
  • Visual indicator customization
  • Compound component architecture

Anatomy

PartDescription
<Checkbox.Root>Root component that provides context and state management for the checkbox
<Checkbox.Indicator>Visual indicator component showing the checkbox state
<Checkbox.Trigger>Interactive trigger component that handles checkbox toggling
<Checkbox.Label>Label component for the checkbox
<Checkbox.Description>A component that renders the description text for a checkbox
<Checkbox.HiddenInput>A hidden native checkbox input for form submission
<Checkbox.ErrorMessage>A component that displays error messages for a checkbox

Adding a label

To associate a label with a checkbox, use the Checkbox.Label component.

Adding a description

To add a description to a checkbox, add the isDescription prop in the Checkbox.Root component.

Then use the Checkbox.Description component to decide where to render the description.

By checking this box, you acknowledge that you have read, understood, and agree to our Terms of Service and Privacy Policy. This includes consent to process your personal data as described in our policies.

Note: Due to HTML streaming limitations, the isDescription prop must be passed to the root component to maintain consistent accessibility across different environments (especially server rendered content). The component will display a warning if no description is provided when this component is rendered.

Initially check a checkbox

To set a checkbox to its initial checked state, use the checked prop.

Reactive state

To make a checkbox reactive, use the bind:checked prop, and pass in a signal.

Checked: false

Programmatic state

To make programmatic changes to a checkbox, change a signal value passed to the bind:checked prop.

onChange$

To listen for changes when the checkbox state changes, use the onChange$ prop.

Times changed: 0

New value: false

Disabled

To disable a checkbox, use the disabled prop.

Mixed state

Checkboxes can also be in a third mixed or indeterminate state. This is often considered a "partially checked" state.

To set a mixed state, pass "mixed" to the bind:checked prop:

The mixed state can also be set reactively

When a checkbox is in a mixed state and the user clicks it:

  1. The first click changes the state from "mixed" to "checked"
  2. The next click changes the state from "checked" to "unchecked"

This follows the standard accessibility pattern where mixed → checked → unchecked → checked → unchecked, and so on.

Inside a form

To create a form with a checkbox, use the Checkbox.HiddenNativeInput component.

Then create a name for the checkbox in the Checkbox.Root component.

This will be used to associate the checkbox with the form.

Making a checkbox required

To make a checkbox required, pass required to the Checkbox.Root component.

Giving a checkbox a value

By default, the value of a checkbox is on when checked. To give a checkbox a distinct value, pass a string to the value prop.

Checkbox validation

To validate a checkbox, use the Checkbox.ErrorMessage component. Whenever this component is rendered, the checkbox will display a validation error message.

Screen readers will announce the error message when the component is rendered, along with an indication that the checkbox is invalid.

API Reference

Checkbox Root

Inherits from: <div />

Props

PropTypeDefaultDescription
"bind:checked"Signal<boolean | "mixed">-
checkedT-
onChange$QRL<(checked: T) => void>-
disabledboolean-
isDescriptionboolean-
namestring-
requiredboolean-
valuestring-

Data Attributes

AttributeDescription
data-disabledIndicates whether the checkbox is disabled
data-checkedIndicates whether the checkbox is checked
data-mixedIndicates whether the checkbox is in an indeterminate state

Checkbox Indicator

Inherits from: <span />

Data Attributes

AttributeDescription
data-hiddenIndicates whether the indicator should be hidden based on checkbox state
data-checkedIndicates whether the checkbox is in a checked state
data-mixedIndicates whether the checkbox is in an indeterminate state

Checkbox Trigger

Inherits from: <button />

Data Attributes

AttributeDescription
data-disabledIndicates whether the checkbox trigger is disabled
data-checkedIndicates whether the checkbox trigger is checked
data-mixedIndicates whether the checkbox trigger is in an indeterminate state

Checkbox Description

Inherits from: <div />

Checkbox Error Message

Inherits from: <div />

Checkbox Hidden Input

Inherits from: <input />

Checkbox Label

Inherits from: <label />

Accessibility

Keyboard Interactions

KeyDescription
SpaceWhen focus is on the checkbox trigger, toggles the checkbox state between checked and unchecked
EnterWhen focus is on the checkbox trigger, toggles the checkbox state between checked and unchecked (default behavior prevented)
TabMoves focus to the checkbox trigger or away from it following the document tab sequence