Checkbox

A choice that waits for a Save, filled in ink when on and drawing a dash when only some of what it stands for is chosen.

01Preview

01
Full screen

02Installation

npx shadcn@latest add https://aiellie.app/r/checkbox.json

03API reference

02
From the Base UI documentation

Represents the checkbox itself. Renders a <span> element and a hidden <input> beside.

Props

PropTypeDefaultDescription
namestringundefinedIdentifies the field when a form is submitted.
defaultCheckedbooleanfalseWhether the checkbox is initially ticked. To render a controlled checkbox, use the checked prop instead.
checkedbooleanundefinedWhether the checkbox is currently ticked. To render an uncontrolled checkbox, use the defaultChecked prop instead.
onCheckedChange((checked: boolean, eventDetails: Checkbox.Root.ChangeEventDetails) => void)-Event handler called when the checkbox is ticked or unticked.
indeterminatebooleanfalseWhether the checkbox is in a mixed state: neither ticked, nor unticked.
valuestring-The checkbox's value. Identifies it within a [Checkbox Group](https://base-ui.com/react/components/checkbox-group), falling back to name when omitted. When submitting a form, a checked box submits value; with no value, it submits the native "on".
formstring-Identifies the form that owns the hidden input. Useful when the checkbox is rendered outside the form.
nativeButtonbooleanfalseWhether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.
parentbooleanfalseWhether the checkbox controls a group of child checkboxes. Must be used in a [Checkbox Group](https://base-ui.com/react/components/checkbox-group).
uncheckedValuestring-The value submitted with the form when the checkbox is unchecked. By default, unchecked checkboxes do not submit any value, matching native checkbox behavior.
disabledbooleanfalseWhether the component should ignore user interaction.
readOnlybooleanfalseWhether the user should be unable to tick or untick the checkbox.
requiredbooleanfalseWhether the user must tick the checkbox before submitting a form.
inputRefReact.Ref<HTMLInputElement>-A ref to access the hidden <input> element.
idstring-The id of the input element.
classNamestring | ((state: Checkbox.Root.State) => string | undefined)-CSS class applied to the element, or a function that returns a class based on the component's state.
styleReact.CSSProperties | ((state: Checkbox.Root.State) => React.CSSProperties | undefined)-Style applied to the element, or a function that returns a style object based on the component's state.
renderReactElement | ((props: HTMLProps, state: Checkbox.Root.State) => ReactElement)-Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Data Attributes

AttributeTypeDescription
data-checked-Present when the checkbox is checked.
data-unchecked-Present when the checkbox is not checked.
data-disabled-Present when the checkbox is disabled.
data-readonly-Present when the checkbox is readonly.
data-required-Present when the checkbox is required.
data-valid-Present when the checkbox is in a valid state (when wrapped in Field.Root).
data-invalid-Present when the checkbox is in an invalid state (when wrapped in Field.Root).
data-dirty-Present when the checkbox's value has changed (when wrapped in Field.Root).
data-touched-Present when the checkbox has been touched (when wrapped in Field.Root).
data-filled-Present when the checkbox is checked (when wrapped in Field.Root).
data-focused-Present when the checkbox is focused (when wrapped in Field.Root).
data-indeterminate-Present when the checkbox is in an indeterminate state.