Checkbox
A control that allows the user to toggle between checked and not checked.
Checkbox is a styled checkbox. In PRISM it is a <button role="checkbox" data-ui="checkbox"> that keeps its state in data-state and aria-checked. The island toggles it on click. With name it renders a hidden <input> that posts value (default "on") while the box is checked and nothing while it is not, like a native checkbox.
Installation
$ bext ui add checkbox
Copy and paste the following into src/components/ui/checkbox.tsx.
This component is interactive. Make sure the shared island is loaded (see Islands). It reacts to data-ui="checkbox" hooks.
Usage
Anatomy
<Checkbox id="cb-id" name="terms" checked />
API Reference
Checkbox
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | — | Links the checkbox to its Label. |
| checked | boolean | false | Initial state. The island toggles it on click. |
| ariaLabel | string | — | aria-label of the <button>. Use it when there is no visible Label. |
| disabled | boolean | false | Disables the button: it cannot be focused or toggled, is dimmed, and posts nothing. |
| name | string | — | Form field name. Renders a hidden input that posts only while the box is checked. |
| value | string | "on" | Value posted under name when checked. |
| className | string | — | Extra classes on the <button>. |
Data Attributes
Attributes you can target in custom CSS to override the style for each state.
| Attribute | On | Values | Description |
|---|---|---|---|
| data-ui="checkbox" | root | — | The island toggles the state on click. |
| data-state | root | "checked" | "unchecked" | Reflects the current state. |
| aria-checked | root | "true" | "false" | Same state, for assistive technologies. Updated by the island. |
| data-ui="control-input" | hidden input | disabled present or absent | Rendered when name is set. Disabled while unchecked, so nothing posts. Emits a change event on every toggle. |
Keyboard
| Key | Action |
|---|---|
| Space / Enter | Toggles the checkbox (it is a <button>). |
| Tab | Moves focus in or out. |
Accessibility
Checkbox sets role="checkbox" and aria-checked on its <button type="button">, and the island keeps aria-checked in sync. Its name comes from a <Label htmlFor> pointing at its id, or from ariaLabel when there is no visible label: always provide one of the two. A disabled checkbox uses the native attribute, so it is announced as unavailable and skipped by Tab.
Examples
Checked by default
Edit this component live in the bext playground. The PRISM + signals version is the bext-native idiom: fine-grained reactivity, no virtual DOM.
On This Page