Kobalte.v0.13.4

Checkbox

A control that allows the user to toggle between checked and not checked.

Import

ts
import { Checkbox } from "@kobalte/core/checkbox";
// or
import { Root, Input, ... } from "@kobalte/core/checkbox";
// or (deprecated)
import { Checkbox } from "@kobalte/core";
ts
import { Checkbox } from "@kobalte/core/checkbox";
// or
import { Root, Input, ... } from "@kobalte/core/checkbox";
// or (deprecated)
import { Checkbox } from "@kobalte/core";

Features

  • Built with a native HTML <input> element, which is visually hidden to allow custom styling.
  • Syncs with form reset events.
  • Labeling support for assistive technology.
  • Support for description and error message help text linked to the input via ARIA.
  • Can be controlled or uncontrolled.

Anatomy

The checkbox consists of:

  • Checkbox: The root container for a checkbox.
  • Checkbox.Input: The native html input that is visually hidden in the checkbox.
  • Checkbox.Control: The element that visually represents a checkbox.
  • Checkbox.Indicator: The visual indicator rendered when the checkbox is in a checked or indeterminate state.
  • Checkbox.Label: The label that gives the user information on the checkbox.
  • Checkbox.Description: The description that gives the user more information on the checkbox.
  • Checkbox.ErrorMessage: The error message that gives the user information about how to fix a validation error on the checkbox.
tsx
<Checkbox>
<Checkbox.Input />
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
<Checkbox.Label />
<Checkbox.Description />
<Checkbox.ErrorMessage />
</Checkbox>
tsx
<Checkbox>
<Checkbox.Input />
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
<Checkbox.Label />
<Checkbox.Description />
<Checkbox.ErrorMessage />
</Checkbox>

Example

Usage

Default checked

An initial, uncontrolled value can be provided using the defaultChecked prop.

Check
tsx
<Checkbox defaultChecked>...</Checkbox>
tsx
<Checkbox defaultChecked>...</Checkbox>

Controlled checked

The checked prop can be used to make the checked state controlled. The onChange event is fired when the user presses the checkbox, and receives the new value.

You are unsubscribed.

tsx
import { createSignal } from "solid-js";
function ControlledExample() {
const [checked, setChecked] = createSignal(false);
return (
<>
<Checkbox checked={checked()} onChange={setChecked}>
...
</Checkbox>
<p>You are {checked() ? "subscribed" : "unsubscribed"}.</p>
</>
);
}
tsx
import { createSignal } from "solid-js";
function ControlledExample() {
const [checked, setChecked] = createSignal(false);
return (
<>
<Checkbox checked={checked()} onChange={setChecked}>
...
</Checkbox>
<p>You are {checked() ? "subscribed" : "unsubscribed"}.</p>
</>
);
}

Description

The Checkbox.Description component can be used to associate additional help text with a checkbox.

You will receive our weekly newsletter.
tsx
<Checkbox>
<Checkbox.Input />
<Checkbox.Control>
<Checkbox.Indicator>
<CheckIcon />
</Checkbox.Indicator>
</Checkbox.Control>
<Checkbox.Label>Subscribe</Checkbox.Label>
<Checkbox.Description>You will receive our weekly newsletter.</Checkbox.Description>
</Checkbox>
tsx
<Checkbox>
<Checkbox.Input />
<Checkbox.Control>
<Checkbox.Indicator>
<CheckIcon />
</Checkbox.Indicator>
</Checkbox.Control>
<Checkbox.Label>Subscribe</Checkbox.Label>
<Checkbox.Description>You will receive our weekly newsletter.</Checkbox.Description>
</Checkbox>

Error message

The Checkbox.ErrorMessage component can be used to help the user fix a validation error. It should be combined with the validationState prop to semantically mark the checkbox as invalid for assistive technologies.

By default, it will render only when the validationState prop is set to invalid, use the forceMount prop to always render the error message (ex: for usage with animation libraries).

You must agree to our Terms and Conditions.
tsx
import { createSignal } from "solid-js";
function ErrorMessageExample() {
const [checked, setChecked] = createSignal(false);
return (
<Checkbox
checked={checked()}
onChange={setChecked}
validationState={!checked() ? "invalid" : "valid"}
>
<Checkbox.Input />
<Checkbox.Control>
<Checkbox.Indicator>
<CheckIcon />
</Checkbox.Indicator>
</Checkbox.Control>
<Checkbox.Label>Agree</Checkbox.Label>
<Checkbox.ErrorMessage>You must agree to our Terms and Conditions.</Checkbox.ErrorMessage>
</Checkbox>
);
}
tsx
import { createSignal } from "solid-js";
function ErrorMessageExample() {
const [checked, setChecked] = createSignal(false);
return (
<Checkbox
checked={checked()}
onChange={setChecked}
validationState={!checked() ? "invalid" : "valid"}
>
<Checkbox.Input />
<Checkbox.Control>
<Checkbox.Indicator>
<CheckIcon />
</Checkbox.Indicator>
</Checkbox.Control>
<Checkbox.Label>Agree</Checkbox.Label>
<Checkbox.ErrorMessage>You must agree to our Terms and Conditions.</Checkbox.ErrorMessage>
</Checkbox>
);
}

HTML forms

The name and value props can be used for integration with HTML forms.

tsx
function HTMLFormExample() {
const onSubmit = (e: SubmitEvent) => {
// handle form submission.
};
return (
<form onSubmit={onSubmit}>
<Checkbox name="newsletter" value="subscribe">
...
</Checkbox>
<div>
<button type="reset">Reset</button>
<button type="submit">Submit</button>
</div>
</form>
);
}
tsx
function HTMLFormExample() {
const onSubmit = (e: SubmitEvent) => {
// handle form submission.
};
return (
<form onSubmit={onSubmit}>
<Checkbox name="newsletter" value="subscribe">
...
</Checkbox>
<div>
<button type="reset">Reset</button>
<button type="submit">Submit</button>
</div>
</form>
);
}

API Reference

Checkbox

Checkbox is equivalent to the Root import from @kobalte/core/checkbox (and deprecated Checkbox.Root).

PropDescription
checkedboolean
The controlled checked state of the checkbox.
defaultCheckedboolean
The default checked state when initially rendered. Useful when you do not need to control the checked state.
onChange(checked: boolean) => void
Event handler called when the checked state of the checkbox changes.
indeterminateboolean
Whether the checkbox is in an indeterminate state.
namestring
The name of the checkbox, used when submitting an HTML form. See MDN.
valuestring
The value of the checkbox, used when submitting an HTML form. See MDN.
validationState'valid' | 'invalid'
Whether the checkbox should display its "valid" or "invalid" visual styling.
requiredboolean
Whether the user must check the checkbox before the owning form can be submitted.
disabledboolean
Whether the checkbox is disabled.
readOnlyboolean
Whether the checkbox can be checked but not changed by the user.
childrenJSX.Element | (state: CheckboxState) => JSX.Element
The children of the checkbox. Can be a JSX.Element or a render prop for having access to the internal state.
Render PropDescription
checkedAccessor<boolean>
Whether the checkbox is checked or not.
indeterminateAccessor<boolean>
Whether the checkbox is in an indeterminate state.
Data attributeDescription
data-validPresent when the checkbox is valid according to the validation rules.
data-invalidPresent when the checkbox is invalid according to the validation rules.
data-requiredPresent when the checkbox is required.
data-disabledPresent when the checkbox is disabled.
data-readonlyPresent when the checkbox is read only.
data-checkedPresent when the checkbox is checked.
data-indeterminatePresent when the checkbox is in an indeterminate state.

Checkbox.Input, Checkbox.Control, Checkbox.Indicator, Checkbox.Label, Checkbox.Description and Checkbox.ErrorMessage share the same data-attributes.

Checkbox.Indicator

PropDescription
forceMountboolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.

Checkbox.ErrorMessage

PropDescription
forceMountboolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.

Rendered elements

ComponentDefault rendered element
Checkboxdiv
Checkbox.Inputinput
Checkbox.Controldiv
Checkbox.Indicatordiv
Checkbox.Labellabel
Checkbox.Descriptiondiv
Checkbox.ErrorMessagediv

Accessibility

Keyboard Interactions

KeyDescription
SpaceToggles the checkbox on and off.