Components
Checkbox
An easily stylable checkbox component.
Usage guidelines#
- Form controls must have an accessible name. It can be created using a
<label>element or the Field component. See Labeling a checkbox.
Anatomy#
Import the component and assemble its parts:
import { Checkbox } from '@egoist/solid-base/checkbox';
<Checkbox.Root>
<Checkbox.Indicator />
</Checkbox.Root>;Examples#
Labeling a checkbox#
An enclosing <label> is the simplest labeling pattern:
<label>
<Checkbox.Root />
Accept terms and conditions
</label>Rendering as a native button#
By default, <Checkbox.Root> renders a <span> element to support enclosing labels. Prefer
rendering the checkbox as a native button when using sibling labels (for/id).
<div>
<label for="notifications-checkbox">Enable notifications</label>
<Checkbox.Root id="notifications-checkbox" nativeButton render="button">
<Checkbox.Indicator />
</Checkbox.Root>
</div>Native buttons with wrapping labels are supported by using the render callback to avoid invalid
HTML, so the hidden input is placed outside the label:
<Checkbox.Root
nativeButton
render={(buttonProps) => (
<label>
<button {...buttonProps} />
Enable notifications
</label>
)}
/>Indeterminate#
Set indeterminate to show a mixed state. <Checkbox.Indicator> stays mounted while the checkbox
is indeterminate, and its state.indeterminate tells you which icon to render:
Controlled#
Pass checked and update it from onCheckedChange. The second argument carries the event
details, including cancel():
const [accepted, setAccepted] = createSignal(false);
<Checkbox.Root checked={accepted()} onCheckedChange={(checked) => setAccepted(checked)}>
<Checkbox.Indicator />
</Checkbox.Root>;API reference#
<Checkbox.Root>
Represents the checkbox itself. Renders a <span> element and a hidden <input> beside.
idstring—The id of the input element.
namestringundefinedIdentifies the field when a form is submitted.
formstring—Identifies the form that owns the hidden input. Useful when the checkbox is rendered outside the form.
checkedbooleanundefinedWhether the checkbox is currently ticked.
To render an uncontrolled checkbox, use the defaultChecked prop instead.
defaultCheckedbooleanfalseWhether the checkbox is initially ticked.
To render a controlled checkbox, use the checked prop instead.
disabledbooleanfalseWhether the component should ignore user interaction.
onCheckedChange| ((checked: boolean, eventDetails: CheckboxRootChangeEventDetails) => void)—Event handler called when the checkbox is ticked or unticked.
readOnlybooleanfalseWhether the user should be unable to tick or untick the checkbox.
requiredbooleanfalseWhether the user must tick the checkbox before submitting a form.
indeterminatebooleanfalseWhether the checkbox is in a mixed state: neither ticked, nor unticked.
inputRefRefProp<HTMLInputElement>—A ref to access the hidden <input> element.
parentbooleanfalseWhether the checkbox controls a group of child checkboxes.
Must be used in a 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.
valuestring—The checkbox's value. Identifies it within a 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".
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.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-checkedPresent when the checkbox is checked.
data-uncheckedPresent when the checkbox is not checked.
data-indeterminatePresent when the checkbox is in an indeterminate state.
data-disabledPresent when the checkbox is disabled.
data-readonlyPresent when the checkbox is readonly.
data-requiredPresent when the checkbox is required.
data-validPresent when the checkbox is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the checkbox is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the checkbox has been touched (when wrapped in Field.Root).
data-dirtyPresent when the checkbox's value has changed (when wrapped in Field.Root).
data-filledPresent when the checkbox is checked (when wrapped in Field.Root).
data-focusedPresent when the checkbox is focused (when wrapped in Field.Root).
<Checkbox.Indicator>
Indicates whether the checkbox is ticked. Renders a <span> element.
keepMountedbooleanfalseWhether to keep the element in the DOM when the checkbox is not checked.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-checkedPresent when the checkbox is checked.
data-uncheckedPresent when the checkbox is not checked.
data-indeterminatePresent when the checkbox is in an indeterminate state.
data-disabledPresent when the checkbox is disabled.
data-readonlyPresent when the checkbox is readonly.
data-requiredPresent when the checkbox is required.
data-validPresent when the checkbox is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the checkbox is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the checkbox has been touched (when wrapped in Field.Root).
data-dirtyPresent when the checkbox's value has changed (when wrapped in Field.Root).
data-filledPresent when the checkbox is checked (when wrapped in Field.Root).
data-focusedPresent when the checkbox is focused (when wrapped in Field.Root).