Checkbox
Choose any number of options: Material 3's checkbox on the native control.
Group
Show code
---
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
nameType:
stringDefault:
requiredSubmitted name.
valueType:
stringDefault:
unsetSubmitted value.
idType:
stringDefault:
generated, unique per controlControl id; set it only when something else must reference the control.
defaultCheckedType:
booleanDefault:
unsetInitially checked.
checkedType:
booleanDefault:
unsetControls the state from outside, for a box that acts at once (a checklist item); pair it with onChange.
onChangeType:
(event) => voidDefault:
unsetCalled when the box is toggled.
requiredType:
booleanDefault:
unsetNative required validation.
disabledType:
booleanDefault:
falseNative disabled control.
classNameType:
string | undefinedDefault:
unsetAdds 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
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.