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:

Anatomytsx
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:

Wrapping a label around a checkboxtsx
<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).

Sibling label pattern with a native buttontsx
<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:

Render callbacktsx
<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.

PropTypeDefault
idstring—

The id of the input element.

namestringundefined

Identifies 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.

checkedbooleanundefined

Whether the checkbox is currently ticked.

To render an uncontrolled checkbox, use the defaultChecked prop instead.

defaultCheckedbooleanfalse

Whether the checkbox is initially ticked.

To render a controlled checkbox, use the checked prop instead.

disabledbooleanfalse

Whether the component should ignore user interaction.

onCheckedChange| ((checked: boolean, eventDetails: CheckboxRootChangeEventDetails) => void)—

Event handler called when the checkbox is ticked or unticked.

readOnlybooleanfalse

Whether the user should be unable to tick or untick the checkbox.

requiredbooleanfalse

Whether the user must tick the checkbox before submitting a form.

indeterminatebooleanfalse

Whether the checkbox is in a mixed state: neither ticked, nor unticked.

inputRefRefProp<HTMLInputElement>—

A ref to access the hidden <input> element.

parentbooleanfalse

Whether 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".

nativeButtonbooleanfalse

Whether 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 attributeDescription
data-checked

Present when the checkbox is checked.

data-unchecked

Present when the checkbox is not checked.

data-indeterminate

Present when the checkbox is in an indeterminate state.

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-touched

Present when the checkbox has been touched (when wrapped in Field.Root).

data-dirty

Present when the checkbox's value has changed (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).

<Checkbox.Indicator>

Indicates whether the checkbox is ticked. Renders a <span> element.

PropTypeDefault
keepMountedbooleanfalse

Whether 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 attributeDescription
data-checked

Present when the checkbox is checked.

data-unchecked

Present when the checkbox is not checked.

data-indeterminate

Present when the checkbox is in an indeterminate state.

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-touched

Present when the checkbox has been touched (when wrapped in Field.Root).

data-dirty

Present when the checkbox's value has changed (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).