@biem/ui

Checkbox

A control that lets users check or uncheck an option.

A checkbox lets users check or uncheck one option without changing any other. Its label names the option, and a click on the label changes the checkbox just as a click on the box does.

Usage

checkbox-default.tsx
import { Checkbox } from "@biem/ui/components/ui/checkbox";
import { Field, FieldLabel } from "@biem/ui/components/ui/field";

export function CheckboxDefault() {
	return (
		<Field orientation="horizontal">
			<Checkbox id="print-shelf-labels"/>
			<FieldLabel htmlFor="print-shelf-labels">Print shelf labels</FieldLabel>
		</Field>
	);
}

Examples

Checked by Default

Some options hold true for most users, such as keeping a filter for the next visit. With defaultChecked, the checkbox starts checked, so users only act when the option does not suit their case.

Controlled

Often the application needs the state of an option, for example to offer the next step only after users have confirmed it. Through checked and onCheckedChange, it holds that state itself, so the checkbox can also show a value that comes from saved data or from another part of the interface.

Indeterminate

A checkbox that stands for a whole selection is only partly true when users have chosen some of its options but not all of them. The indeterminate prop puts it in this mixed state and exposes it to assistive technology, and the checkbox stays mixed, even when users click it, until the application sets the prop to false.

Disabled

Not every option applies in every context, for example a report section without data for the chosen period. Where that is the case, disabled keeps the option in view while users can no longer check or uncheck it, and the checkbox exposes this state to assistive technology.

Read-Only

Users sometimes need to see a decision that was made elsewhere, such as a setting that headquarters defines for every branch. Once readOnly is set, users can no longer change the checkbox, yet unlike a disabled one it stays reachable with the keyboard and is submitted with the form.

Invalid

A form can require a confirmation before it accepts an entry, and users need to see at once which option is still missing. When validation fails, aria-invalid marks the checkbox as invalid, on screen as well as for assistive technology.

API Reference

Propchecked
Typeboolean
Default–
DescriptionSets whether the checkbox is checked and makes it controlled. Update it in onCheckedChange.
PropdefaultChecked
Typeboolean
Defaultfalse
DescriptionChecks the checkbox at first render. The checkbox then tracks its state itself.
ProponCheckedChange
Type(checked: boolean, eventDetails: CheckboxRootChangeEventDetails) => void
Default–
DescriptionRuns when users check or uncheck the checkbox. It receives the state that follows.
Propindeterminate
Typeboolean
Defaultfalse
DescriptionPuts the checkbox in a mixed state that is neither checked nor unchecked, and exposes that state to assistive technology. The checkbox stays mixed until the prop is false, even when users click it.
Propdisabled
Typeboolean
Defaultfalse
DescriptionPrevents users from checking or unchecking the checkbox and exposes the disabled state to assistive technology.
PropreadOnly
Typeboolean
Defaultfalse
DescriptionPrevents users from checking or unchecking the checkbox. Unlike a disabled checkbox, it stays focusable and is submitted with the form.
Proprequired
Typeboolean
Defaultfalse
DescriptionRequires users to check the checkbox before they can submit the form.
Propname
Typestring
Default–
DescriptionIdentifies the checkbox when the form is submitted.
Propvalue
Typestring
Default–
DescriptionSets the value that a checked checkbox submits with the form. Without it, a checked checkbox submits "on". Inside a CheckboxGroup, it also identifies the checkbox.
PropuncheckedValue
Typestring
Default–
DescriptionSets the value that an unchecked checkbox submits with the form. Without it, an unchecked checkbox submits nothing. It has no effect without name.
Propform
Typestring
Default–
DescriptionSets the ID of the form that the checkbox belongs to. Set it when the checkbox is rendered outside of that form.
Propid
Typestring
Default–
DescriptionSets the ID of the hidden input, so that a label can refer to the checkbox with htmlFor. With nativeButton, the ID goes to the rendered button instead.
PropinputRef
TypeRef<HTMLInputElement>
Default–
DescriptionReceives the hidden input element that carries the value of the checkbox in a form.
Propparent
Typeboolean
Defaultfalse
DescriptionMakes the checkbox control a group of child checkboxes. It only works inside a CheckboxGroup from @base-ui/react/checkbox-group.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the checkbox with another component.
PropnativeButton
Typeboolean
Defaultfalse
DescriptionDeclares whether the rendered element is a native button. Set it to true when render returns a button.