Checkbox
A control that allows the user to make a binary choice with support for indeterminate state.
Installation
npx vayu-ui-cli@latest add checkboxUsage
Checkbox Examples
Basic Checkbox
With Description
Receive updates about new products and features
Error State
You must accept to continue
Indeterminate State
Anatomy
import { Checkbox } from 'vayu-ui';
<Checkbox checked={checked} onChange={setChecked}>
<div className="flex items-start gap-3">
<Checkbox.Indicator />
<div className="flex flex-col gap-1">
<Checkbox.Label>Label</Checkbox.Label>
<Checkbox.Description>Description text</Checkbox.Description>
<Checkbox.Error>Error message</Checkbox.Error>
</div>
</div>
</Checkbox>;- Checkbox — Root container. Manages checked/indeterminate/disabled state and provides context to children.
- Checkbox.Indicator — Renders a hidden native
<input type="checkbox">and a custom visual checkbox with check or minus icon. - Checkbox.Label — Accessible label associated with the checkbox input via
htmlFor. - Checkbox.Description — Helper text below the label, linked via
aria-describedby. - Checkbox.Error — Error message with
role="alert"andaria-live="polite"for screen reader announcements.
Accessibility
- Keyboard Support:
Space— Toggle the checkbox checked state.Tab— Move focus to the next checkbox.Shift + Tab— Move focus to the previous checkbox.
- ARIA Attributes:
aria-checkedreflects checked, unchecked, or mixed (indeterminate) state on the native input.aria-invalid="true"set whenerrorprop is passed.aria-describedbylinks the input to its description and error elements when present.- Error message uses
role="alert"andaria-live="polite"for dynamic announcements.
- Focus Behavior:
- Visible focus ring (
focus-visible) on the checkbox indicator using thefocusdesign token. - Focus ring meets Accessible contrast requirements.
- Visible focus ring (
Screen reader behavior
When a user navigates to a checkbox, the screen reader announces the label text, the current state (checked or unchecked), and the name of the control. When in the indeterminate state, the screen reader announces "mixed" as the value. If an error is present, the screen reader announces the error message due to aria-live="polite" and role="alert" on the error element, which is linked to the input via aria-describedby. If a description is provided, it is also announced after the label. The native checkbox input ensures standard checkbox semantics are conveyed without additional ARIA role declarations.
Component Folder Structure
CheckBox/
├── CheckBox.tsx # Root component with context provider and state management
├── CheckBoxIndicator.tsx # Hidden native input + custom visual checkbox
├── CheckBoxLabel.tsx # Accessible label linked to the checkbox input
├── CheckBoxDescription.tsx # Helper description text below the label
├── CheckBoxError.tsx # Error message with aria-live for screen readers
├── hooks.ts # Context creation and useCheckboxContext hook
├── types.ts # TypeScript type definitions
├── index.ts # Re-exports Checkbox compound component and types
└── README.md # Component usage referenceProps
Checkbox (Root)
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | — | Controlled checked state. |
defaultChecked | boolean | false | Default checked state for uncontrolled mode. |
indeterminate | boolean | false | Shows indeterminate state (minus icon). |
disabled | boolean | false | Disables the checkbox. |
error | boolean | false | Shows error styling on the indicator. |
onChange | (checked: boolean) => void | — | Callback when checked state changes. |
name | string | — | Form field name for form submission. |
value | string | — | Form field value. |
required | boolean | — | Marks the field as required. |
Checkbox.Indicator
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Additional classes for the hidden input. |
Checkbox.Label
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | Label content. |
className | string | — | Additional classes for the label. |
Checkbox.Description
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | Description content. |
className | string | — | Additional classes for the description. |
Checkbox.Error
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | Error message content. |
className | string | — | Additional classes for the error message. |