Skip to Content
CodeUI

Checkbox

@codeui/solid v0.3.0

A box that toggles one option on or off.

When to use

Select independent export options or collect agreement before form submission.

When to choose another component

Use Switch for a setting that takes effect immediately. Checkbox also supports a mixed selection through indeterminate.

Live demo

Include line numbers in exported snippets.
checkbox/basic.tsx
import { Checkbox } from "@codeui/solid";
export default function Demo() {
	return (
		<Checkbox
			label="Show line numbers"
			description="Include line numbers in exported snippets."
			defaultChecked
		/>
	);
}

Anatomy

Include line numbers in exported snippets.

Tap a part to outline it in the demo.

  1. 01Checkboxcheckboxclasses.root
ExportRendersslotClasses keydata-cuiRole
CheckboxRoot and hidden inputrootcheckboxcheckbox on input

Slots input, control, label, and errorLabel customize descendants. Those descendants have no data-cui name.

Variants

checkbox/sizes.tsx
import { Checkbox } from "@codeui/solid";
export default function Demo() {
	return (
		<div style={{ display: "grid", gap: "var(--cui-space-4)" }}>
			<Checkbox size="xs" label="Line numbers (xs)" defaultChecked />
			<Checkbox size="sm" label="Line numbers (sm)" defaultChecked />
			<Checkbox size="md" label="Line numbers (md)" defaultChecked />
			<Checkbox size="lg" label="Line numbers (lg)" defaultChecked />
			<Checkbox size="xl" label="Line numbers (xl)" defaultChecked />
		</div>
	);
}

variant accepts filled and inline through BaseFieldProps. inline, the default, draws the box as an outline on the surface. filled gives the box the field fill (--cui-surface-input) and no outline. The root carries data-field-variant.

States

Enabled
checkbox/controlled.tsx
import { Checkbox } from "@codeui/solid";
import { createSignal } from "solid-js";
export default function Demo() {
	const [checked, setChecked] = createSignal(true);
	return (
		<>
			<Checkbox label="Show line numbers" checked={checked()} onChange={setChecked} />
			<output>{checked() ? "Enabled" : "Disabled"}</output>
		</>
	);
}
Include line numbers in the export.
checkbox/disabled.tsx
import { Checkbox } from "@codeui/solid";
export default function Demo() {
	return (
		<>
			<Checkbox
				label="Show line numbers"
				description="Include line numbers in the export."
				disabled
				defaultChecked
			/>
			<Checkbox label="Show window header" disabled />
		</>
	);
}
Include line numbers in the export.
checkbox/read-only.tsx
import { Checkbox } from "@codeui/solid";
export default function Demo() {
	return (
		<Checkbox
			label="Show line numbers"
			description="Include line numbers in the export."
			readOnly
			defaultChecked
		/>
	);
}
Include line numbers in the export.
checkbox/required.tsx
import { Checkbox } from "@codeui/solid";
export default function Demo() {
	return (
		<Checkbox
			label="Show line numbers"
			description="Include line numbers in the export."
			required
			defaultChecked
		/>
	);
}
checkbox/invalid.tsx
import { Checkbox } from "@codeui/solid";
export default function Demo() {
	return (
		<div style={{ display: "grid", gap: "var(--cui-space-4)" }}>
			<Checkbox
				size="xs"
				label="Export options (xs)"
				validationState="invalid"
				errorMessage="Choose an export option."
			/>
			<Checkbox
				size="sm"
				label="Export options (sm)"
				validationState="invalid"
				errorMessage="Choose an export option."
			/>
			<Checkbox
				size="md"
				label="Export options (md)"
				validationState="invalid"
				errorMessage="Choose an export option."
			/>
			<Checkbox
				size="lg"
				label="Export options (lg)"
				validationState="invalid"
				errorMessage="Choose an export option."
			/>
			<Checkbox
				size="xl"
				label="Export options (xl)"
				validationState="invalid"
				errorMessage="Choose an export option."
			/>
		</div>
	);
}
checkbox/indeterminate.tsx
import { Checkbox } from "@codeui/solid";
export default function Demo() {
	return <Checkbox label="All export options" indeterminate />;
}

Ready API

Checkbox

Renders the complete field. CheckBoxProps is the public props type.

Prop

Type

Prop

Type

Inherited behavior follows the Kobalte checkbox contract.

Styled parts API

Use @codeui/solid/checkbox/parts to build a checkbox with a custom label or indicator. Place Input and Control inside Root. Place Indicator and its content inside Control. Use Content to arrange Label, Description, and ErrorMessage next to the checkbox. Keep Input in the composition so keyboard focus and form submission work.

Accessibility

Keyboard

KeyAction
TabFocus the control.
SpaceToggle the checked state.

ARIA

The input exposes checkbox semantics, including aria-checked="mixed" for indeterminate selection. Description and error components associate hint text with the field. validationState="invalid" exposes invalid state. Supply a visible label or an accessible name.

Focus

These controls do not open an overlay or move focus on mount. The native checkbox is visually hidden, so keyboard focus draws the 2px focus ring around the box on :focus-visible. Disabled controls cannot receive keyboard focus. Read-only controls remain focusable and do not change value.

SSR notes

The root, labels, controls, and form inputs render on the server. No part uses a portal. Keep initial values and component order stable between server and client so generated IDs hydrate consistently. Create signals per component instance.

Theming hooks

The component reads these foundation variables. Shared field labels and messages also use the field typography and validation tokens.

--cui-action-brand-base, --cui-action-brand-hover, --cui-action-brand-text, --cui-border-width-strong, --cui-entries, --cui-focus-color, --cui-focus-offset, --cui-focus-width, --cui-radius-md, --cui-space-2, --cui-surface-input, --cui-text-hint.

Composition examples

Submit to inspect
checkbox/form.tsx
import { Checkbox, Button } from "@codeui/solid";
import { createSignal, For } from "solid-js";
export default function Demo() {
	const [result, setResult] = createSignal("Submit to inspect");
	return (
		<form
			onSubmit={event => {
				event.preventDefault();
				setResult(
					new FormData(event.currentTarget).get("lineNumbers")?.toString() ?? "omitted",
				);
			}}
		>
			<Checkbox
				label="Show line numbers"
				name="lineNumbers"
				value="include"
				defaultChecked
			/>
			<Button type="submit">Inspect form value</Button>
			<output>{result()}</output>
		</form>
	);
}

The form demo reads the submitted value with FormData. Only checked, enabled inputs submit their value. Unchecked and disabled inputs are omitted. An omitted checkbox or switch value defaults to "on".

Use Switch for a setting that takes effect immediately. Checkbox also supports a mixed selection through indeterminate. See Control helpers for custom compositions.

On this page