Skip to main content

Starwind UI v3.7 is here with stable Vue and Svelte, the new Chart component, and full RTL support. Read the release notes

Checkbox Primitive

Use Checkbox to let someone opt in or out of a choice. It shows the checked state and can show a mixed state for a group of choices.

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>

Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโ€™s types for the available props and events. Plain HTML uses DOM events and Runtime methods.

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.
  • Set the form field name on the root. Checkbox synchronizes its native input with the checked state. Set an unchecked value when the form also needs to submit a value for the unchecked state.

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

Prop Type Default Toggle
checked boolean -
Description
Controls the checked state when a framework adapter owns it.
Kind
control
Targets
-
Full type
boolean
React

Use checked for controlled state and defaultChecked for default state, and onCheckedChange for change proposals.

  • checked
  • defaultChecked
  • onCheckedChange
Astro

Use checked or defaultChecked for initial state, listen for starwind:checked-change, and call setChecked for later updates.

  • checked
  • defaultChecked
  • starwind:checked-change
  • setChecked
Runtime / HTML

Use data-default-checked for initial state, listen for starwind:checked-change, and call setChecked for later updates.

  • data-default-checked
  • starwind:checked-change
  • setChecked
defaultChecked boolean false
Description
Sets the initial uncontrolled checked state.
Kind
control
Targets
-
Full type
boolean
React

Use checked for controlled state and defaultChecked for default state, and onCheckedChange for change proposals.

  • checked
  • defaultChecked
  • onCheckedChange
Astro

Use checked or defaultChecked for initial state, listen for starwind:checked-change, and call setChecked for later updates.

  • checked
  • defaultChecked
  • starwind:checked-change
  • setChecked
Runtime / HTML

Use data-default-checked for initial state, listen for starwind:checked-change, and call setChecked for later updates.

  • data-default-checked
  • starwind:checked-change
  • setChecked
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
React

Use indeterminate for controlled state.

  • indeterminate
Astro

Use indeterminate for initial state and call setIndeterminate for later updates.

  • indeterminate
  • setIndeterminate
Runtime / HTML

Use data-indeterminate for initial state and call setIndeterminate for later updates.

  • data-indeterminate
  • setIndeterminate
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
React

Use checked for controlled state and defaultChecked for default state, and onCheckedChange for change proposals.

  • checked
  • defaultChecked
  • onCheckedChange
Astro

Use checked or defaultChecked for initial state, listen for starwind:checked-change, and call setChecked for later updates.

  • checked
  • defaultChecked
  • starwind:checked-change
  • setChecked
Runtime / HTML

Use data-default-checked for initial state, listen for starwind:checked-change, and call setChecked for later updates.

  • data-default-checked
  • starwind:checked-change
  • setChecked

Events

Event Callback Value Toggle
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
Cancellation sequence
  1. Check internal eligibility and intent.
  2. Create one details object for the proposal.
  3. Call the Runtime callback with the details object when the controller exposes one.
  4. Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
  5. Read details.isCanceled, including cancellation caused by preventDefault().
  6. Apply the accepted state.
  7. Notify Runtime subscribers and other accepted-only observers.

State

State Type Props Toggle
checked boolean checked / defaultChecked
Description
Tracks whether Checkbox is checked.
Initial attribute
data-default-checked
Runtime getter
getChecked
Runtime setter
setChecked
React

Use checked for controlled state and defaultChecked for default state, and onCheckedChange for change proposals.

  • checked
  • defaultChecked
  • onCheckedChange
Astro

Use checked or defaultChecked for initial state, listen for starwind:checked-change, and call setChecked for later updates.

  • checked
  • defaultChecked
  • starwind:checked-change
  • setChecked
Runtime / HTML

Use data-default-checked for initial state, listen for starwind:checked-change, and call setChecked for later updates.

  • data-default-checked
  • starwind:checked-change
  • setChecked
indeterminate boolean indeterminate
Description
Tracks whether Checkbox is in a mixed state.
Initial attribute
data-indeterminate
Runtime getter
-
Runtime setter
setIndeterminate
React

Use indeterminate for controlled state.

  • indeterminate
Astro

Use indeterminate for initial state and call setIndeterminate for later updates.

  • indeterminate
  • setIndeterminate
Runtime / HTML

Use data-indeterminate for initial state and call setIndeterminate for later updates.

  • data-indeterminate
  • setIndeterminate

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-checkbox - Runtime discovery hook for the checkbox root.
State
Attribute Value Description
data-checked - Present when the checkbox is checked.
data-indeterminate - Present when the checkbox is in the mixed state.
data-unchecked - Present when the checkbox is unchecked.
Metadata
Attribute Value Description
data-default-checked - Initial uncontrolled checked state.
data-disabled - Present when the checkbox is disabled.
data-form - Reflects the form prop on the Root part.
data-id - Reflects the id prop on the Root part.
data-name - Reflects the name prop on the Root part.
data-readonly - Reflects the readonly prop on the Root part.
data-required - Reflects the required prop on the Root part.
data-unchecked-value - Reflects the unchecked value prop on the Root part.
data-value - Reflects the value prop on the Root part.

Indicator

The visual indicator rendered inside the root for checked or mixed state.

Default element
span
Discovery hook
data-sw-checkbox-indicator
Role
-

Props

Prop Type Default Toggle
keepMounted boolean false
Description
Keeps the indicator element mounted when unchecked.
Kind
rendering
Targets
indicator
Full type
boolean

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-checkbox-indicator - Runtime discovery hook for the indicator part.
State
Attribute Value Description
data-unchecked - Present while the checkbox is unchecked.
Metadata
Attribute Value Description
data-keep-mounted - Marks that the indicator should stay mounted when unchecked.

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
Attribute Value Description
data-sw-checkbox-input - Runtime discovery hook for the hidden checked input.

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
Attribute Value Description
data-sw-checkbox-unchecked-input - Runtime discovery hook for the hidden unchecked input.

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

Method Target Description
setChecked state: checked Updates whether Checkbox is checked from Runtime code.
setIndeterminate state: indeterminate Updates whether Checkbox is in a mixed state from Runtime code.
setDisabled prop: disabled Updates whether Checkbox is disabled from Runtime code.

Form Participation

FactValue
Form propsform, id, name, required, uncheckedValue, value
Hidden inputinput (checkbox)
Field integrationYes
Component Relationship
Checkbox Direct primitive

Changelog

View version history v1.2.0 9 releases

v1.2.0

  • In Svelte, `CheckboxGroupContext` and `CheckboxGroupContextValue` are no longer exported from `@starwind-ui/svelte/checkbox` or `@starwind-ui/svelte`. Use the Checkbox Group parts to group checkboxes.
  • In Vue, this component now needs Vue 3.5.21 or newer.

v1.1.0

  • In Astro, an attribute that you set on a Checkbox part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
  • In Svelte, a defined `checked` that you pass when Checkbox mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, Checkbox keeps its own state.
  • Inside a `Field` in Astro, Checkbox now clears when the form resets. Before, it stayed checked.

v1.0.3

  • Checkbox now gets its name from a `<label for>` that points at its `id`. Before, screen readers read it with no name.
  • In Astro, a required Checkbox in a plain HTML form now shows the browser message and gets focus. Before, the form did not submit, and no message showed.
  • When the browser flags a required Checkbox as invalid, focus now goes to the checkbox that the user sees.
  • In Svelte apps that render on the server, a form reset now puts Checkbox back to its default checked state. Before, a reset after the page loaded cleared it.

v1.0.2

  • In React, the initial native checkbox value now matches the checked value supplied by your app.
  • Vue Checkbox keeps its previous checked value when a change is canceled.
  • Fixed React and Vue form resets restoring the wrong value after a control is recreated. Resets now use its original default.
  • Reduced the JavaScript loaded by Checkbox when it is used without Field.
  • Reduced repeated JavaScript when Astro Checkbox is used with other Starwind components.
  • Reduced JavaScript used by Svelte Checkbox. Element props, event handlers, and refs keep the same behavior.
  • Reduced JavaScript used by Vue Checkbox. Props and defaults stay the same.

v1.0.1

  • Removed old comments from the Vue Primitive files that said the code was internal and not for release. The CLI also removes the blank line that these comments left in the Vue Primitive `index.ts` files that it copies into your project.

v1.0.0

  • This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.

v0.1.2

  • The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
  • In React, you can cancel a change from its change callback or DOM event before it applies. React then shows only the changes that apply.

v0.1.1

  • Fixed the React Checkbox indicator. It now renders when the checkbox is checked or when you keep it mounted. It does not render when it is inactive or hidden.

v0.1.0

  • First release. Checkbox supports checked and indeterminate states, sends its value with the form, and fires checked-change events.