Checkbox Primitive
Checkbox coordinates a visible boolean control, indicator presence, and hidden form inputs for boolean form state.
Usage Guidelines
- Use Checkbox for one independent boolean value. Use Checkbox when users can turn a single option on or off without choosing from a required set.
- Use the hidden input parts for form submission. The runtime synchronizes the hidden native input and unchecked input so unchecked and checked states can submit predictable values.
Anatomy
Use the Astro primitive adapter to render Checkbox anatomy with the Runtime wiring included.
---import { Checkbox } from "@starwind-ui/astro/checkbox";---
<Checkbox.Root> <Checkbox.Indicator /></Checkbox.Root>Use the React primitive adapter when Checkbox state participates in React rendering.
import { Checkbox } from "@starwind-ui/react/checkbox";
export function Example() { return ( <Checkbox.Root> <Checkbox.Indicator /> </Checkbox.Root> );}Render the Checkbox data-sw-* contract yourself, then initialize createCheckbox.
<span data-sw-checkbox role="checkbox"> <span data-sw-checkbox-indicator></span> <input data-sw-checkbox-input type="checkbox" aria-hidden="true" tabindex="-1" /></span>
<script type="module"> import { createCheckbox } from "@starwind-ui/runtime/checkbox";
const root = document.querySelector("[data-sw-checkbox]"); if (root) { createCheckbox(root); }</script>API Reference
Root
The focusable checkbox control. It owns the Runtime instance and carries ARIA, checked, indeterminate, disabled, and form option state.
- Default element
- span
- Discovery hook
- data-sw-checkbox
- Role
- checkbox
Props
checked boolean -
- Description
- Controls the checked state when a framework adapter owns it.
- Kind
- control
- Targets
- -
- Full type
- boolean
defaultChecked boolean false
- Description
- Sets the initial uncontrolled checked state.
- Kind
- control
- Targets
- -
- Full type
- boolean
disabled boolean false
- Description
- Prevents interaction and marks the control disabled.
- Kind
- option
- Targets
- -
- Full type
- boolean
form string -
- Description
- Associates the hidden native input with an external form.
- Kind
- option
- Targets
- -
- Full type
- string
id string -
- Description
- Sets the hidden native input id.
- Kind
- option
- Targets
- -
- Full type
- string
indeterminate boolean false
- Description
- Sets the mixed visual state independently from checked state.
- Kind
- control
- Targets
- -
- Full type
- boolean
name string -
- Description
- Sets the submitted form field name.
- Kind
- option
- Targets
- -
- Full type
- string
nativeButton boolean false
- Description
- Renders the root as a native button instead of a span.
- Kind
- rendering
- Targets
- root
- Full type
- boolean
readOnly boolean false
- Description
- Marks the checkbox readonly for assistive technology.
- Kind
- option
- Targets
- -
- Full type
- boolean
required boolean false
- Description
- Marks the form control as required.
- Kind
- option
- Targets
- -
- Full type
- boolean
uncheckedValue string -
- Description
- Value submitted by the runtime-created unchecked input.
- Kind
- option
- Targets
- -
- Full type
- string
value string -
- Description
- Value submitted by the checked input.
- Kind
- option
- Targets
- -
- Full type
- string
onCheckedChange (checked: boolean, details: CheckboxCheckedChangeDetails) => void -
- Description
- Receives checked-change details before state commits.
- Kind
- callback
- Targets
- -
- Full type
- (checked: boolean, details: CheckboxCheckedChangeDetails) => void
Events
checkedChange onCheckedChange checked: boolean
- Description
- Fires when the checked state changes for Checkbox.
- DOM event
- starwind:checked-change
- Details type
- CheckboxCheckedChangeDetails
- Timing
- before-state-commit
- Cancelable
- Yes
State
checked boolean checked / defaultChecked
- Description
- Tracks whether Checkbox is checked.
- Initial attribute
- data-default-checked
- Runtime getter
- getChecked
- Runtime setter
- setChecked
- State control support
- React supports controlled and default state with checked and defaultChecked props. Runtime/HTML reads initial state from data-default-checked, emits starwind:checked-change, and updates with setChecked. Astro adapters render initial/default state, but do not expose reactive controlled-state props for this state.
indeterminate boolean indeterminate
- Description
- Tracks whether Checkbox is in a mixed state.
- Initial attribute
- data-indeterminate
- Runtime getter
- -
- Runtime setter
- setIndeterminate
- State control support
- React supports controlled state with the indeterminate prop. Runtime/HTML reads initial state from data-indeterminate and updates with setIndeterminate. Astro adapters render initial/default state, but do not expose reactive controlled-state props for this state.
Data Attributes
Runtime hooks
State
Metadata
Indicator
The visual indicator rendered inside the root for checked or mixed state.
- Default element
- span
- Discovery hook
- data-sw-checkbox-indicator
- Role
- -
Props
keepMounted boolean false
- Description
- Keeps the indicator element mounted when unchecked.
- Kind
- rendering
- Targets
- indicator
- Full type
- boolean
Data Attributes
Runtime hooks
State
Metadata
Input
The hidden native checkbox input synchronized by the Runtime for form submission.
- Default element
- input
- Discovery hook
- data-sw-checkbox-input
- Role
- -
Data Attributes
Runtime hooks
Unchecked Input
The hidden unchecked input created or synchronized by the Runtime when unchecked values are needed.
- Default element
- input
- Discovery hook
- data-sw-checkbox-unchecked-input
- Role
- -
Data Attributes
Runtime hooks
Runtime API
- Factory
createCheckbox- Import
@starwind-ui/runtime/checkbox- Root hook
- root
data-sw-checkbox - Option props
- checked, defaultChecked, disabled, form, id, indeterminate, name, readOnly, required, uncheckedValue, value
Runtime Setters
Form Participation
| Fact | Value |
|---|---|
| Form props | form, id, name, required, uncheckedValue, value |
| Hidden input | input (checkbox) |
| Field integration | Yes |