Radio
A radio picks one option out of several that are all visible, so they can be compared; keep their number small. It renders a native <input type="radio"> inside a <label>. RadioButton draws the same choice as a button, and RadioGroup owns the value.
When to use
- Use radios to pick exactly one item from a few options that the user should see side by side.
- With many options, or little room, a Select is the better fit; to pick several items at once, use checkboxes.
- Prefer a
RadioGroupwithoptions; useoptionType="button"for a compact, button-like choice.
Examples
The radio is not selected no change yet
<script lang="ts">
import { Radio } from 'svelte-ant-design';
let selected = $state(false);
let last = $state('no change yet');
</script>
<div class="stack">
<!-- The text between the tags is the children of the radio, and its accessible name. -->
<Radio bind:checked={selected} onchange={(checked) => (last = `onchange(${checked})`)}>
Send me the newsletter
</Radio>
<span>The radio is {selected ? 'selected' : 'not selected'}</span>
<span role="status">{last}</span>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
<script lang="ts">
import { Button, Radio, RadioGroup } from 'svelte-ant-design';
let disabled = $state(true);
</script>
<div class="stack">
<div class="row">
<Button onclick={() => (disabled = !disabled)}>{disabled ? 'Enable' : 'Disable'}</Button>
<Radio {disabled}>Not selected</Radio>
<Radio {disabled} checked>Selected</Radio>
</div>
<!-- disabled disables the whole group. -->
<RadioGroup {disabled} value="Apple" options={['Apple', 'Pear']} aria-label="Fruits" />
<!-- An option can be disabled alone. -->
<RadioGroup
value="Apple"
aria-label="Stock"
options={['Apple', { label: 'Pear (out of stock)', value: 'Pear', disabled: true }, 'Orange']}
/>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Radio, RadioGroup } from 'svelte-ant-design';
let fruit = $state('Apple');
let last = $state('no change yet');
</script>
<div class="stack">
<!-- The children of the group are Radio elements; each one needs a value. -->
<RadioGroup
bind:value={fruit}
name="fruit"
aria-label="Fruit"
onchange={(value) => (last = `onchange(${value})`)}
>
<Radio value="Apple">Apple</Radio>
<Radio value="Pear">Pear</Radio>
<Radio value="Orange">Orange</Radio>
</RadioGroup>
<span>The value is {fruit}</span>
<span role="status">{last}</span>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
no change yet
<script lang="ts">
import { RadioGroup } from 'svelte-ant-design';
let fruit = $state<string | undefined>('Apple');
let size = $state<number | undefined>(2);
let agree = $state<boolean | undefined>(false);
let extra = $state<string | undefined>();
let last = $state('no change yet');
</script>
{#snippet cherry()}<strong>Cherry</strong>{/snippet}
<div class="stack">
<!-- Strings, numbers and booleans are both the value and the label. -->
<RadioGroup bind:value={fruit} options={['Apple', 'Pear', 'Orange']} aria-label="Fruit" />
<RadioGroup bind:value={size} options={[1, 2, 3]} aria-label="Size" />
<!-- A boolean value stays a boolean in onchange; the form gets its text. -->
<RadioGroup
bind:value={agree}
aria-label="Agree"
options={[
{ label: 'Yes', value: true },
{ label: 'No', value: false }
]}
/>
<!-- Objects add a snippet label, title, id, required, disabled and an onchange of the option. -->
<RadioGroup
bind:value={extra}
aria-label="Extras"
options={[
{ label: cherry, value: 'cherry', id: 'extra-cherry' },
{
label: 'Pear',
value: 'pear',
title: 'Juicy',
required: true,
onchange: (checked) => (last = `pear: ${checked}`)
},
{ label: 'Plum', value: 'plum', disabled: true }
]}
/>
<span role="status">{last}</span>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
<script lang="ts">
import { RadioButton, RadioGroup } from 'svelte-ant-design';
let city = $state('Hangzhou');
let solidCity = $state('Shanghai');
let mode = $state('day');
</script>
<div class="stack">
<!-- optionType="button" asks options for buttons instead of plain radios. -->
<RadioGroup
bind:value={city}
optionType="button"
options={['Hangzhou', 'Shanghai', { label: 'Beijing', value: 'Beijing', disabled: true }]}
aria-label="City"
/>
<!-- buttonStyle="solid" fills the checked button. -->
<RadioGroup
bind:value={solidCity}
optionType="button"
buttonStyle="solid"
options={['Hangzhou', 'Shanghai', 'Chengdu']}
aria-label="City, solid"
/>
<!-- Or put RadioButton elements in the group yourself. -->
<RadioGroup bind:value={mode} buttonStyle="solid" aria-label="Mode">
<RadioButton value="day">Day</RadioButton>
<RadioButton value="week">Week</RadioButton>
<RadioButton value="month">Month</RadioButton>
</RadioGroup>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
<script lang="ts">
import { RadioGroup } from 'svelte-ant-design';
const cities = ['Hangzhou', 'Shanghai', 'Beijing'];
</script>
<div class="stack">
<!-- size changes the buttons only; medium is the default, middle and default are aliases of it.
A plain value is the initial value, so each group keeps its own selection. -->
<RadioGroup value="Hangzhou" optionType="button" size="large" options={cities} aria-label="Large" />
<RadioGroup value="Hangzhou" optionType="button" size="medium" options={cities} aria-label="Medium" />
<RadioGroup value="Hangzhou" optionType="button" size="small" options={cities} aria-label="Small" />
<RadioGroup value="Hangzhou" optionType="button" size="default" options={cities} aria-label="Default (medium)" />
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
<script lang="ts">
import { Radio, RadioGroup } from 'svelte-ant-design';
let a = $state('Apple');
let b = $state('Pear');
let c = $state('Hangzhou');
let d = $state('x');
const fruits = ['Apple', 'Pear', 'Orange'];
</script>
<div class="layout">
<!-- orientation="vertical" puts one option on each line. -->
<RadioGroup bind:value={a} orientation="vertical" options={fruits} aria-label="Vertical" />
<!-- vertical is a shorthand for it; orientation wins when both are set. -->
<RadioGroup bind:value={b} vertical optionType="button" options={fruits} aria-label="Vertical buttons" />
<!-- block fills the width of the parent and shares it between the items. -->
<RadioGroup
bind:value={c}
block
optionType="button"
options={['Hangzhou', 'Shanghai', 'Beijing']}
aria-label="Block"
/>
<!-- Your own markup around the radios: they still share the group. -->
<RadioGroup bind:value={d} aria-label="Grid">
<div class="grid">
<Radio value="x">Top left</Radio>
<Radio value="y">Top right</Radio>
<Radio value="z">Bottom left</Radio>
<Radio value="w">Bottom right</Radio>
</div>
</RadioGroup>
</div>
<style>
.layout {
display: flex;
flex-direction: column;
gap: 16px;
align-items: stretch;
width: 100%;
}
.grid {
display: grid;
grid-template-columns: repeat(2, 140px);
gap: 8px;
}
</style>
Plan: free, refused 0 times
<script lang="ts">
import { Button, Radio, RadioGroup } from 'svelte-ant-design';
let plan = $state<string | undefined>('free');
let refused = $state(0);
let locked = $state(false);
let agreed = $state(false);
</script>
<div class="stack">
<!-- A function binding: the setter decides whether a choice is accepted. The previous radio is put back. -->
<RadioGroup
bind:value={
() => plan,
(next) => {
if (next === 'team' && !agreed) {
refused++;
return;
}
plan = next;
}
}
options={[
{ label: 'Free', value: 'free' },
{ label: 'Team (needs the terms)', value: 'team' }
]}
aria-label="Plan"
/>
<div class="row">
<Radio bind:checked={agreed}>I accept the terms</Radio>
<span role="status">Plan: {plan ?? 'none'}, refused {refused} times</span>
</div>
<!-- preventDefault() in a forwarded onclick cancels the selection and onchange. -->
<Radio
bind:checked={locked}
onclick={(event) => {
if (!agreed) event.preventDefault();
}}
>
Lock the plan
</Radio>
<!-- Setting the value to undefined clears the selection. -->
<Button onclick={() => (plan = undefined)}>Clear the plan</Button>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px 16px;
}
</style>
<script lang="ts">
import { Radio, RadioButton, RadioGroup } from 'svelte-ant-design';
let value = $state('b');
</script>
<div class="stack">
<div class="row">
<!-- Radio: the parts root, icon and label. -->
<Radio
checked
class="radio-demo-mine"
classNames={{ label: 'radio-demo-strong' }}
styles={{ icon: { '--ads-radio-checked-bg': '#52c41a' }, label: { 'font-weight': 600 } }}
>
Green mark
</Radio>
<!-- RadioButton: the parts root and label. -->
<RadioButton checked styles={{ root: { 'border-radius': '16px' } }}>Round</RadioButton>
</div>
<!-- RadioGroup: root, item, itemIcon and itemLabel reach every radio inside. -->
<RadioGroup
bind:value
options={[
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' }
]}
aria-label="Parts"
class="radio-demo-group"
style="padding: 4px 8px"
classNames={{ item: 'radio-demo-item', itemIcon: 'radio-demo-icon', itemLabel: 'radio-demo-label' }}
styles={{
root: { 'background-color': 'rgba(22, 119, 255, 0.08)', 'border-radius': '8px' },
itemLabel: { 'letter-spacing': '0.04em' }
}}
/>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px 24px;
}
:global(.radio-demo-strong) {
text-decoration: underline;
}
:global(.radio-demo-item) {
padding-block: 2px;
}
</style>
No visible text, named by aria-label
Named by a heading
Uses the radio of your device.
Pick a colour
<script lang="ts">
import { Radio, RadioGroup } from 'svelte-ant-design';
let color = $state('red');
</script>
<div class="stack">
<!-- children: the text of the radio is its accessible name. -->
<Radio>Visible text</Radio>
<!-- aria-label: for a radio without a visible text. -->
<div class="row">
<Radio aria-label="Hidden label" />
<span>No visible text, named by aria-label</span>
</div>
<!-- aria-labelledby: another element names it. -->
<div class="row">
<Radio aria-labelledby="radio-name" />
<span id="radio-name">Named by a heading</span>
</div>
<!-- An external <label for> works with the id of the input. -->
<div class="row">
<Radio id="radio-external" />
<label for="radio-external">External label</label>
</div>
<!-- aria-describedby and title. -->
<div class="row">
<Radio aria-describedby="radio-hint" title="Wireless">Wi-Fi</Radio>
<small id="radio-hint">Uses the radio of your device.</small>
</div>
<!-- A group is named with aria-labelledby (or aria-label), so a screen reader says what the choice is about. -->
<div class="field">
<span id="color-title">Pick a colour</span>
<RadioGroup bind:value={color} options={['red', 'green', 'blue']} aria-labelledby="color-title" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 4px 12px;
}
.field {
display: grid;
justify-items: start;
gap: 4px;
}
</style>
<script lang="ts">
import { Button, Radio, RadioGroup } from 'svelte-ant-design';
let size = $state<string | undefined>();
let gift = $state(false);
let result = $state('');
function submit(event: SubmitEvent) {
event.preventDefault();
const data = new FormData(event.currentTarget as HTMLFormElement);
result = `Submitted: ${JSON.stringify([...data.entries()])}`;
}
// A native reset does not change the state of the components: reset it here.
function reset() {
size = undefined;
gift = false;
result = '';
}
</script>
<form class="stack" onsubmit={submit} onreset={reset}>
<!-- A named group sends the value of the selected radio. -->
<RadioGroup bind:value={size} name="size" options={['S', 'M', 'L']} aria-label="Size" />
<Radio bind:checked={gift} name="gift" value="yes" required>Gift wrap</Radio>
<!-- A group without a name is never submitted, so a form stays free of made-up keys. -->
<RadioGroup value="a" options={['a', 'b']} aria-label="Not submitted" />
<div class="row">
<Button htmlType="submit" type="primary">Save</Button>
<Button htmlType="reset">Reset</Button>
</div>
<span role="status">{result}</span>
</form>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
gap: 8px;
}
</style>
no change yet
<script lang="ts">
import { RadioGroup } from 'svelte-ant-design';
let first = $state('Apple');
let second = $state<string | undefined>();
let last = $state('no change yet');
</script>
<!-- Tab enters a group once, on the selected radio (or the first when none is). Arrow keys move and select; Space
selects the focused one. The browser does it: the inputs of a group share one name. -->
<div class="stack">
<input aria-label="Before the groups" placeholder="Tab from here" />
<RadioGroup
bind:value={first}
options={['Apple', 'Pear', 'Orange']}
aria-label="First group"
onchange={(value) => (last = `first: ${value}`)}
/>
<RadioGroup
bind:value={second}
options={['Red', 'Green', 'Blue']}
aria-label="Second group"
onchange={(value) => (last = `second: ${value}`)}
/>
<span role="status">{last}</span>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
API
| Property | Description | Type | Default |
|---|---|---|---|
| checked | Whether the radio is selected. Use bind:checked; a plain checked={x} is only the initial value. Inside a group it is ignored. | boolean | false |
| disabled | Disables the radio. A group's disabled applies as well. | boolean | false |
| value | Identity inside a RadioGroup and the native value of the input, written as text. Required inside a group. | string | number | boolean | — |
| name | Native name of the input. Inside a group the group's name wins. Standalone radios that share a name are not kept in step: use a group. | string | — |
| children | Label content. It is the accessible name; without it, name the radio with aria-label or an external label. | Snippet | — |
| onchange | Called once when the user selects the radio, with true and the native change event. Not called for a change by the parent, nor while disabled. | (checked: boolean, event: Event) => void | — |
| classNames | Extra classes for the parts of the radio. | Partial<Record<'root' | 'icon' | 'label', string>> | — |
| styles | Inline styles for the parts of the radio; keys are kebab-case CSS names or custom properties, numbers are written as they are. | Partial<Record<'root' | 'icon' | 'label', Record<string, string | number>>> | — |
| class | Extra class of the root, after classNames.root. | string | — |
| style | Inline style of the root; styles.root is written after it. | string | — |
| title | Native title of the root label, so the tooltip covers the mark and the text. | string | — |
| native attributes | Everything else goes to the <input>: id, required, autofocus, tabindex, aria-*, data-*, onclick, onfocus, onblur... The component owns type, checked, value, disabled, name and onchange. | HTMLInputAttributes | — |
Differences from Ant Design:
onchange of a radio receives (checked, event) (checked is always true) and of a group (value, event) with the native event, not an event object; there is no defaultChecked or defaultValue, because bind:checked and bind:value cover them; RadioButton is a component of its own and optionType only asks options for it; value takes a string, a number or a boolean, not any type; an unnamed group is never submitted with a form; size also accepts middle and default.RadioButton API
| Property | Description | Type | Default |
|---|---|---|---|
| checked | Whether the radio is selected. Use bind:checked; a plain checked={x} is only the initial value. Inside a group it is ignored. | boolean | false |
| disabled | Disables the radio. A group's disabled applies as well. | boolean | false |
| value | Identity inside a RadioGroup and the native value of the input, written as text. Required inside a group. | string | number | boolean | — |
| name | Native name of the input. Inside a group the group's name wins. Standalone radios that share a name are not kept in step: use a group. | string | — |
| children | Label content. It is the accessible name; without it, name the radio with aria-label or an external label. | Snippet | — |
| onchange | Called once when the user selects the radio, with true and the native change event. Not called for a change by the parent, nor while disabled. | (checked: boolean, event: Event) => void | — |
| classNames | Extra classes for the parts of the button. | Partial<Record<'root' | 'label', string>> | — |
| styles | Inline styles for the parts of the button; keys are kebab-case CSS names or custom properties, numbers are written as they are. | Partial<Record<'root' | 'label', Record<string, string | number>>> | — |
| class | Extra class of the root, after classNames.root. | string | — |
| style | Inline style of the root; styles.root is written after it. | string | — |
| title | Native title of the root label, so the tooltip covers the mark and the text. | string | — |
| native attributes | Everything else goes to the <input>: id, required, autofocus, tabindex, aria-*, data-*, onclick, onfocus, onblur... The component owns type, checked, value, disabled, name and onchange. | HTMLInputAttributes | — |
RadioGroup API
| Property | Description | Type | Default |
|---|---|---|---|
| value | The selected value; a missing value or null means nothing is selected. Use bind:value; a plain value={x} is only the initial value. A value that matches no radio checks nothing and is kept. | string | number | boolean | null | — |
| options | Renders one radio per entry. A string, a number or a boolean is both the value and the label; an object is a RadioOption. Values must be unique. With options, children is ignored. | ReadonlyArray<string | number | boolean | RadioOption> | [] |
| optionType | What options render: plain radios or buttons. Children keep the type their author chose. | 'default' | 'button' | 'default' |
| buttonStyle | How a checked button is drawn: a coloured outline or a solid fill. Reaches every RadioButton inside. | 'outline' | 'solid' | 'outline' |
| size | Height of the buttons (24, 32 and 40 px); plain radios ignore it. middle and default are aliases of medium. | 'large' | 'medium' | 'middle' | 'small' | 'default' | 'medium' |
| orientation | Direction of the items. When it is set it wins over vertical; without both the items are horizontal. | 'horizontal' | 'vertical' | — |
| vertical | A shorthand for orientation="vertical". | boolean | false |
| block | The group fills the width of its parent and its items share it equally. | boolean | false |
| disabled | Disables every radio of the group; an item's own disabled disables that item alone. | boolean | false |
| name | Native name of every input; it wins over an item's own. Without one the group makes a unique name and is never submitted with a form. | string | — |
| children | Custom markup with Radio and RadioButton elements inside; each needs a value. Ignored when options has entries. | Snippet | — |
| onchange | Called once per user selection with the new value and the native change event of the selected input. Not called for a change by the parent. | (value: string | number | boolean, event: Event) => void | — |
| classNames | Extra classes: root is the group; item, itemIcon and itemLabel reach the matching part of every radio inside (itemIcon does nothing for buttons). | Partial<Record<'root' | 'item' | 'itemIcon' | 'itemLabel', string>> | — |
| styles | Inline styles for the same parts; the item's own styles are written after the group's. | Partial<Record<'root' | 'item' | 'itemIcon' | 'itemLabel', Record<string, string | number>>> | — |
| class | Extra class of the group root. | string | — |
| style | Inline style of the group root; styles.root is written after it. | string | — |
| native attributes | Everything else goes to the root <div>: id, aria-label, aria-labelledby, aria-describedby, aria-required, data-*, handlers. The component owns role="radiogroup". | HTMLAttributes<HTMLDivElement> | — |
RadioOption: an entry of options
| Property | Description | Type | Default |
|---|---|---|---|
| label | Label content: plain text, or a snippet for icons and markup. | string | Snippet | — |
| value | The value; it must be unique within the group. | string | number | boolean | — |
| disabled | Disables this option alone. | boolean | — |
| title | Native title of the option's root label. | string | — |
| id | Native id of the option's input, so an external <label for> or aria-labelledby can reach it. | string | — |
| required | Native required of the option's input; one required radio makes the whole native group required. | boolean | — |
| class | Extra class of the option's root. | string | — |
| style | Inline style of the option's root. | string | — |
| onchange | Called when this option is selected by the user, before the group's onchange. | (checked: boolean, event: Event) => void | — |
Methods
| Method | Description | Type |
|---|---|---|
| focus | Moves focus to the radio. Available on Radio and RadioButton through bind:this. | (options?: FocusOptions) => void |
| blur | Removes focus from the radio. | () => void |
Design tokens
| Token | Description | Default |
|---|---|---|
| --ads-radio-bg | Background of the mark. | #ffffff |
| --ads-radio-border | Border of an unchecked mark. | #d9d9d9 |
| --ads-radio-border-hover | Border of an unchecked mark on hover. | #1677ff |
| --ads-radio-border-width | Border width of the mark. | 1px |
| --ads-radio-button-bg | Background of a button. | #ffffff |
| --ads-radio-button-border | Border of an unselected button. | #d9d9d9 |
| --ads-radio-button-checked-bg | Background of a selected outline button. | #ffffff |
| --ads-radio-button-checked-disabled-bg | Background of a disabled, selected button. | rgba(0, 0, 0, 0.15) |
| --ads-radio-button-checked-disabled-color | Text colour of a disabled, selected button. | rgba(0, 0, 0, 0.25) |
| --ads-radio-button-color | Text colour of an unselected button. | rgba(0, 0, 0, 0.88) |
| --ads-radio-button-disabled-bg | Background of a disabled button. | rgba(0, 0, 0, 0.04) |
| --ads-radio-button-disabled-border | Border of a disabled button. | #d9d9d9 |
| --ads-radio-button-disabled-color | Text colour of a disabled button. | rgba(0, 0, 0, 0.25) |
| --ads-radio-button-padding-inline | Inline padding of a medium and a large button. | 15px |
| --ads-radio-button-padding-inline-sm | Inline padding of a small button. | 7px |
| --ads-radio-checked-bg | Fill and border of a selected mark. | #1677ff |
| --ads-radio-checked-bg-hover | Fill of a selected mark on hover. | #4096ff |
| --ads-radio-disabled-bg | Background of a disabled mark. | rgba(0, 0, 0, 0.04) |
| --ads-radio-disabled-border | Border of a disabled mark. | #d9d9d9 |
| --ads-radio-disabled-dot-color | Colour of the dot of a disabled radio. | rgba(0, 0, 0, 0.25) |
| --ads-radio-dot-color | Colour of the dot. | #ffffff |
| --ads-radio-dot-size | Diameter of the dot of a selected radio. | 6px |
| --ads-radio-font-size | Font size of the label and of the group. | 14px |
| --ads-radio-group-gap | Gap between the radios of a group. | 8px |
| --ads-radio-label-padding | Padding of the label on both sides. | 8px |
| --ads-radio-line-height | Line height of the label. | 1.5714 |
| --ads-radio-size | Diameter of the mark. | 16px |
Accessibility
- A radio is a native
<input type="radio">in a<label>. The inputs of a group share one name (a made-up one when you give none), so the browser gives the group the keyboard behavior of a radio group: one Tab stop on the selected radio, arrow keys that move and select (Chromium and Firefox wrap around at the ends, WebKit stops), and Space to select. - The group is
role="radiogroup". Name it witharia-labeloraria-labelledby(or a<fieldset>with a<legend>); name a radio with its text,aria-label,aria-labelledbyor an external<label for>. - A user can select a radio but cannot unselect it. In a group with no selected value the first radio is the Tab stop (some browsers use the last one when you tab backwards).
- By default the colours are Ant Design's own. With
tokens-accessible.css, and in the dark theme, the border, the dot, the button border and the focus ring keep a contrast of at least 3:1 in every state, and the text of a button 4.5:1. - In forced-colors modes such as Windows High Contrast a selected radio keeps its dot and a selected button is drawn in the selection colours, so the states stay apart; disabled controls take the disabled colour.
- Not checked yet: NVDA, JAWS, VoiceOver and a real touch device. A public report against Ant Design's own radio says VoiceOver may stop twice on a label that wraps the control and its text; this component has the same shape. Orca is checked by an automated test.