Switch
Turn one setting on or off: Material 3's switch on a native checkbox with role="switch".
States
Show code
Example.astroAstro
---
import Switch from '@dhekode/ui/primitives/Switch.tsx';
---
<Switch name="updates" defaultChecked>Email updates</Switch>
<Switch name="digest">Weekly digest</Switch>
<Switch name="beta" disabled>Beta features</Switch>API reference
nameType:
stringDefault:
requiredSubmitted name.
idType:
stringDefault:
generated, unique per controlControl id; set it only when something else must reference the control.
valueType:
stringDefault:
unsetSubmitted value when on.
defaultCheckedType:
booleanDefault:
unsetInitially on.
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.
Use for settings that take effect at once. For a choice submitted with a form, use Checkbox.
Original component source
packages/ui/src/primitives/Switch.tsxAstro
import { useId, type ReactNode } from 'react';
import { classNames } from '@dhekode/utils/class-names';
export interface SwitchProps {
name: string;
id?: string | undefined;
value?: string | undefined;
defaultChecked?: boolean | undefined;
disabled?: boolean | undefined;
className?: string | undefined;
/** The visible label. */
children: ReactNode;
}
/**
* Material 3's switch on a native checkbox with role="switch", so it submits and toggles without
* script. The 52 by 32px track fills when on; its handle grows from 16 to 24px and slides across,
* and carries a round 40px state layer.
*/
export default function Switch({
name,
id,
value,
defaultChecked,
disabled = false,
className,
children,
}: SwitchProps) {
// 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-switch group inline-flex cursor-pointer items-center gap-md text-body text-surface-text has-disabled:cursor-not-allowed has-disabled:text-disabled-text',
className,
)}
htmlFor={controlId}
>
<span className="relative grid shrink-0">
<input
className="peer sr-only"
type="checkbox"
role="switch"
id={controlId}
name={name}
value={value}
defaultChecked={defaultChecked}
disabled={disabled}
/>
<span
className="dk-switch__track col-start-1 row-start-1 h-(--dk-layout-control-height-sm) w-(--dk-layout-switch-width) rounded-full border-(length:--dk-border-width-strong) border-solid border-surface-muted bg-surface-raised transition-colors duration-(--dk-motion-duration-quick) ease-out peer-checked:border-surface-action peer-checked:bg-surface-action peer-focus-visible:outline-(length:--dk-border-width-strong) peer-focus-visible:outline-offset-(--dk-layout-focus-offset) peer-focus-visible:outline-(color:--dk-surface-focus) peer-focus-visible:outline-solid peer-disabled:border-disabled-border peer-disabled:bg-disabled-background"
aria-hidden="true"
/>
<span
className="dk-switch__handle pointer-events-none relative col-start-1 row-start-1 ms-xs size-md self-center rounded-full bg-surface-muted text-surface-text transition-all duration-(--dk-motion-duration-quick) ease-smooth-out before:absolute before:top-1/2 before:left-1/2 before:size-(--dk-layout-button-height) before:-translate-1/2 before:rounded-full before:bg-current before:opacity-0 before:transition-opacity group-hover:before:opacity-(--dk-state-layer-hover) group-active:before:opacity-(--dk-state-layer-pressed) peer-checked:ms-lg peer-checked:size-lg peer-checked:bg-surface-action-text peer-checked:text-surface-action peer-focus-visible:before:opacity-(--dk-state-layer-focus) peer-disabled:bg-disabled-text peer-disabled:before:hidden"
aria-hidden="true"
/>
</span>
<span className="min-w-0">{children}</span>
</label>
);
}Best practices
- Name the setting, not the action, and apply it at once.