No results found

Checkbox

A clickable input that lets users make binary choices or select multiple options.

Features

  • WAI ARIA Checkbox design pattern
  • Indeterminate state support (tri-state)
  • Form integration with hidden native input
  • Error message handling and validation
  • Custom descriptions with ARIA support
  • Keyboard navigation and accessibility
  • Controllable checked state (controlled/uncontrolled)
  • Focus management with ref tracking
  • Disabled state handling
  • Enter key prevention for form submissions

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

Examples

Basic Usage

The most basic checkbox implementation requires a Checkbox.Root component with a Checkbox.Trigger and Checkbox.Indicator nested inside.

The checked prop on Checkbox.Root sets the initial checked state. When checked, the Checkbox.Indicator becomes visible.

Visual Features

Labels and Descriptions

Add descriptive text with Checkbox.Label and Checkbox.Description components. The isDescription prop on Checkbox.Root enables description support.

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.

Mixed State

Checkboxes support an indeterminate state using the "mixed" value. This is useful for parent/child checkbox relationships.

Advanced Usage

Form Integration

For form submissions, add Checkbox.HiddenInput to create a native checkbox input. Use name and value props to control form data.

The required prop enforces validation:

You can customize the submitted value using the value prop:

Error Handling

Display error messages using the Checkbox.ErrorMessage component. This automatically sets the appropriate ARIA attributes.

Mixed State Management

For complex selection scenarios, use the bind:checked prop to handle mixed states programmatically.

The checkbox cycles through mixed → checked → unchecked states when clicked.

Component State

Using Component State

The checkbox component provides several ways to control its state:

  1. Initial State As shown in the Initial example, you can set the initial checked state using the checked prop on Checkbox.Root:
<Checkbox.Root checked>
  <Checkbox.Trigger>
    <Checkbox.Indicator />
  </Checkbox.Trigger>
</Checkbox.Root>
  1. Controlled State For controlled state management, use the bind:checked prop as demonstrated in the Reactive example. This allows you to bind the checkbox state to your application's state.
  2. Indeterminate State The checkbox supports a third "mixed" or indeterminate state, as shown in the Mixed Initial example. This is useful for representing partially selected states, like in a tree structure or bulk selection interface.

State Interactions

  1. Change Events As shown in the Change example, use the onChange$ prop to handle state changes:
<Checkbox.Root
  onChange$={(checked) => {
    // Handle the new checked state
  }}
>
  1. Disabled State The Disabled example demonstrates how to disable the checkbox using the disabled prop. When disabled:
  1. Programmatic Control As shown in the Programmatic example, you can programmatically control the checkbox state from outside the component:
<button
  onClick$={() => {
    // Update the bound checked state
    isChecked.value = true;
  }}
>
  Check the checkbox
</button>
  1. Mixed State Transitions When in a mixed state, clicking the checkbox will first transition to a checked state, then follow the normal checked/unchecked cycle on subsequent clicks, as demonstrated in the Mixed Reactive example. The checkbox maintains a predictable state transition flow:

Based on the examples and API documentation provided, I'll document the configuration options for the Checkbox component.

Core Configuration

Initial State

The checkbox can be configured with an initial state using the checked prop on Checkbox.Root. As shown in the initial example above, this sets an uncontrolled initial value.

The default value is false if not specified.

Value Configuration

The checkbox supports three states:

Form Integration

The checkbox can be configured for form submission with these props:

Advanced Configuration

State Management

The checkbox supports both controlled and uncontrolled state management:

// Uncontrolled
<Checkbox.Root checked={true} />
// Controlled
<Checkbox.Root bind:checked={isCheckedSignal} />

Description Configuration

When using Checkbox.Description, you must set isDescription prop on Checkbox.Root:

<Checkbox.Root isDescription>
  <Checkbox.Description>...</Checkbox.Description>
</Checkbox.Root>

Failing to set isDescription will trigger a console warning.

Technical Constraints

  1. The bind:checked signal must be of type Signal<boolean | "mixed">
  2. The onChange$ handler receives the new state as its only argument
  3. When in mixed state, the first click will set the state to true These behaviors can be observed in the mixed-reactive example above.

Forms

The Checkbox component provides form integration through the <Checkbox.HiddenInput> component, which renders a native checkbox input that's visually hidden but still accessible for form submission.

The following props can be used to configure the form behavior:

Form Validation

The Checkbox supports form validation through the <Checkbox.ErrorMessage> component. When rendered, it automatically puts the checkbox in an invalid state and connects it with the error message via ARIA.

The error message is displayed when form validation fails, and the checkbox trigger receives the appropriate ARIA attributes:

Mixed State in Forms

For complex form scenarios, the checkbox supports a mixed (indeterminate) state that can be used when a form field represents a partial selection.

The mixed state is reflected in the hidden input's indeterminate property, ensuring proper form submission behavior.

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

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/unchecked/mixed
EnterWhen focus is on the checkbox trigger, toggles the checkbox state between checked/unchecked/mixed (default behavior prevented)
TabMoves focus to the checkbox trigger and follows the natural tab order