Radio
Choose one option from a short list: Material 3's radio button on the native control.
Group
Show code
Example.astroAstro
---
import Radio from '@dhekode/ui/primitives/Radio.tsx';
---
<fieldset>
<legend>Start date</legend>
<Radio name="start" value="now" defaultChecked>This month</Radio>
<Radio name="start" value="quarter">This quarter</Radio>
<Radio name="start" value="later">Later</Radio>
</fieldset>API reference
nameType:
stringDefault:
requiredGroup name shared by the radios.
valueType:
stringDefault:
requiredSubmitted value.
idType:
stringDefault:
generated, unique per controlControl id; set it only when something else must reference the control.
defaultCheckedType:
booleanDefault:
unsetInitially chosen.
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.
Always wrap a group in a fieldset with a legend. Arrow keys move between radios natively.
Original component source
packages/ui/src/primitives/Radio.tsxAstro
import { useId, type ReactNode } from 'react';
import { classNames } from '@dhekode/utils/class-names';
import { stateLayer } from '@dhekode/utils/state-layer';
export interface RadioProps {
/** Radios sharing a name form one group; wrap the group in a <fieldset> with a <legend>. */
name: string;
value: string;
id?: string | undefined;
defaultChecked?: boolean | undefined;
required?: boolean | undefined;
disabled?: boolean | undefined;
className?: string | undefined;
/** The visible label. */
children: ReactNode;
}
/**
* Material 3's radio button on the native control: an 18px ring whose dot grows in when chosen,
* inside a round 40px state layer.
*/
export default function Radio({
name,
value,
id,
defaultChecked,
required,
disabled = false,
className,
children,
}: RadioProps) {
// 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-radio 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-full border-(length:--dk-border-width-strong) border-solid border-surface-muted bg-transparent transition-colors duration-(--dk-motion-duration-quick) checked:border-surface-action disabled:cursor-not-allowed disabled:border-disabled-border forced-colors:appearance-auto"
type="radio"
id={controlId}
name={name}
value={value}
defaultChecked={defaultChecked}
required={required}
disabled={disabled}
/>
<span
className="pointer-events-none col-start-1 row-start-1 size-xs scale-0 rounded-full bg-surface-action transition-transform duration-(--dk-motion-duration-quick) ease-smooth-out peer-checked:scale-100 peer-disabled:bg-disabled-border forced-colors:hidden"
aria-hidden="true"
/>
</span>
<span className="min-w-0">{children}</span>
</label>
);
}Best practices
- Offer two to five options and preselect a sensible default.