Skip to main content
Decoderen
Browse documentation

Checkbox

Choose any number of options: Material 3's checkbox on the native control.

Group

Services
Show code
Example.astroAstro
---
import Checkbox from '@dhekode/ui/primitives/Checkbox.tsx';
---

<fieldset>
  <legend>Services</legend>
  <Checkbox name="services" value="software" defaultChecked>Custom software</Checkbox>
  <Checkbox name="services" value="web">Websites</Checkbox>
  <Checkbox name="services" value="ai" disabled>AI automation</Checkbox>
</fieldset>
API reference
name

Type: string

Default: required

Submitted name.

value

Type: string

Default: unset

Submitted value.

id

Type: string

Default: generated, unique per control

Control id; set it only when something else must reference the control.

defaultChecked

Type: boolean

Default: unset

Initially checked.

checked

Type: boolean

Default: unset

Controls the state from outside, for a box that acts at once (a checklist item); pair it with onChange.

onChange

Type: (event) => void

Default: unset

Called when the box is toggled.

required

Type: boolean

Default: unset

Native required validation.

disabled

Type: boolean

Default: false

Native disabled control.

className

Type: string | undefined

Default: unset

Adds classes to the React component root, after its own utilities. React components take className, not class.

Slots: children: the visible label.

Group related checkboxes in a fieldset with a legend.

Original component source
packages/ui/src/primitives/Checkbox.tsxAstro
import { useId, type ChangeEvent, type ReactNode } from 'react';
import Icon from '@dhekode/icons/Icon.tsx';
import { classNames } from '@dhekode/utils/class-names';
import { stateLayer } from '@dhekode/utils/state-layer';

export interface CheckboxProps {
  name: string;
  id?: string | undefined;
  value?: string | undefined;
  defaultChecked?: boolean | undefined;
  /** Controlled state, with `onChange`, for a box that acts at once (a checklist item). */
  checked?: boolean | undefined;
  onChange?: ((event: ChangeEvent<HTMLInputElement>) => void) | undefined;
  required?: boolean | undefined;
  disabled?: boolean | undefined;
  className?: string | undefined;
  /** The visible label. */
  children: ReactNode;
}

/**
 * Material 3's checkbox on the native control: an 18px box that fills with the action colour and
 * a check when on, inside a round 40px state layer.
 */
export default function Checkbox({
  name,
  value,
  id,
  defaultChecked,
  checked,
  onChange,
  required,
  disabled = false,
  className,
  children,
}: CheckboxProps) {
  // Unique per render; Astro prefixes React ids per component, so repeated names cannot collide.
  const generatedId = useId();
  const controlId = id ?? generatedId;
  return (
    <label
      className={classNames(
        'dk-checkbox group inline-flex cursor-pointer items-center gap-2xs text-body text-surface-text has-disabled:cursor-not-allowed has-disabled:text-disabled-text',
        className,
      )}
      htmlFor={controlId}
    >
      <span
        className={`relative grid size-(--dk-layout-button-height) shrink-0 place-items-center text-surface-text before:rounded-full ${stateLayer} group-hover:before:opacity-(--dk-state-layer-hover) group-active:before:opacity-(--dk-state-layer-pressed) has-checked:text-surface-action has-focus-visible:before:opacity-(--dk-state-layer-focus) has-disabled:before:hidden`}
      >
        <input
          className="peer col-start-1 row-start-1 m-0 size-(--dk-layout-selection-control) cursor-pointer appearance-none rounded-xs border-(length:--dk-border-width-strong) border-solid border-surface-muted bg-transparent transition-colors duration-(--dk-motion-duration-quick) checked:border-surface-action checked:bg-surface-action disabled:cursor-not-allowed disabled:border-disabled-border disabled:checked:bg-disabled-border forced-colors:appearance-auto"
          type="checkbox"
          id={controlId}
          name={name}
          value={value}
          defaultChecked={defaultChecked}
          checked={checked}
          onChange={onChange}
          required={required}
          disabled={disabled}
        />
        <Icon
          name="check"
          size={14}
          className="pointer-events-none col-start-1 row-start-1 text-surface-action-text opacity-0 peer-checked:opacity-100 forced-colors:hidden"
        />
      </span>
      <span className="min-w-0">{children}</span>
    </label>
  );
}

Best practices

  • Use for independent options; each label states one choice.