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
import { Checkbox } from "@codeui/solid";
export default function Demo() {
return (
<Checkbox
label="Show line numbers"
description="Include line numbers in exported snippets."
defaultChecked
/>
);
}Anatomy
Tap a part to outline it in the demo.
- 01Checkbox
checkboxclasses.root
| Export | Renders | slotClasses key | data-cui | Role |
|---|---|---|---|---|
Checkbox | Root and hidden input | root | checkbox | checkbox on input |
Slots input, control, label, and errorLabel customize descendants. Those descendants have no data-cui name.
Variants
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
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>
</>
);
}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 />
</>
);
}import { Checkbox } from "@codeui/solid";
export default function Demo() {
return (
<Checkbox
label="Show line numbers"
description="Include line numbers in the export."
readOnly
defaultChecked
/>
);
}import { Checkbox } from "@codeui/solid";
export default function Demo() {
return (
<Checkbox
label="Show line numbers"
description="Include line numbers in the export."
required
defaultChecked
/>
);
}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>
);
}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
| Key | Action |
|---|---|
| Tab | Focus the control. |
| Space | Toggle 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
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".
Related
Use Switch for a setting that takes effect immediately. Checkbox also supports a mixed selection through indeterminate. See Control helpers for custom compositions.