Radio
Choose one option from a group.
Basic usage
Define choices with RadioGroup.options. Give the group a name with label, aria-label or aria-labelledby. Selected borders use the theme color and pressing keeps the control size unchanged; the selection dot uses a 300ms transition by default.
import { RadioGroup } from '@ifhover/leaf-ui';
export function RadioBasic() {
return (
<RadioGroup
label="Choose appearance"
name="appearance"
defaultValue="light"
options={[
{ label: 'Light', value: 'light' },
{ label: 'Dark', value: 'dark' },
{ label: 'System', value: 'system' },
]}
/>
);
}
Examples
Controlled selection
Control the selected string with value, and read event.target.value in onChange.
import { Flex, RadioGroup, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';
export function RadioControlled() {
const [value, setValue] = useState('daily');
return (
<Flex direction="column" gap="md" align="start">
<RadioGroup
label="Notification frequency"
value={value}
onChange={(event) => setValue(event.target.value)}
options={[
{ label: 'Daily', value: 'daily' },
{ label: 'Weekly', value: 'weekly' },
{ label: 'Important messages only', value: 'important' },
]}
/>
<Text tone="muted" role="status">
Selected: {value}
</Text>
</Flex>
);
}
Vertical layout
Set direction="vertical" for a vertical group.
import { RadioGroup } from '@ifhover/leaf-ui';
export function RadioVertical() {
return (
<RadioGroup
label="Notification frequency"
direction="vertical"
defaultValue="daily"
options={[
{ label: 'Daily', value: 'daily' },
{ label: 'Weekly', value: 'weekly' },
{ label: 'Important messages only', value: 'important' },
]}
/>
);
}
Card choices
Set variant="card" for choices with icons and descriptions.
import { RadioGroup } from '@ifhover/leaf-ui';
import { Cloud, HardDrive } from 'lucide-react';
export function RadioCards() {
return (
<RadioGroup
variant="card"
label={'Backup method'}
defaultValue="cloud"
options={[
{
value: 'cloud',
label: 'Cloud backup',
description: 'Sync across your devices',
icon: <Cloud />,
},
{
value: 'local',
label: 'Local backup',
description: 'Keep data on this device',
icon: <HardDrive />,
},
]}
/>
);
}
Grouped choices
Use variant="list" for a shared list. Keep extra to non-interactive status, not buttons or links.
import { RadioGroup, Tag } from '@ifhover/leaf-ui';
export function RadioList() {
return (
<RadioGroup
variant="list"
label={'Access policy'}
defaultValue="readonly"
options={[
{
value: 'readonly',
label: 'Read only',
description: 'Changes require approval',
extra: (
<Tag color="success" size="sm">
{'Recommended'}
</Tag>
),
},
{
value: 'review',
label: 'Approve every request',
description: 'Ask the owner to approve every action',
},
]}
/>
);
}
Set name to submit the selected option's value. Unselected and disabled options are not submitted. When composing individual Radio controls, use the same name and unique values for each group.
Uncontrolled groups return to defaultValue on form reset. For controlled groups, reset application state as well.
API
Radio API
Configure selection, submission value and disabled state for an individual radio.
Individual Radio controls support native radio attributes. className and style apply to the label container.
RadioGroup API
Manage the selected value and layout of a group of radio options.
RadioOption
One group option containing its submission value and displayed content.