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.

Import
import { Radio, RadioButton, RadioGroup } from 'svelte-ant-design';
Links
src/lib/Radio Report an issue Component README

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 RadioGroup with options; use optionType="button" for a compact, button-like choice.

Examples

The radio is not selected no change yet

Basic

bind:checked keeps a value in sync with the radio; onchange receives true and the native change event. A user can select a radio but cannot unselect it: select another radio of the group for that.

Disabled

Native disabled: not focusable, and a click on the mark or on the text does nothing. The disabled of a group covers every radio; an option can be disabled alone.

The value is Apple no change yet

Radio group

RadioGroup owns the selected value (bind:value) and the shared name. Put Radio elements inside it, each with a value; onchange receives the new value and the native event.

no change yet

Options

Strings, numbers and booleans are both the value and the label. Objects add a snippet label, title, id, required, disabled and an onchange of the option. A boolean value stays a boolean in onchange.

Radio button

optionType="button" renders the options as buttons, or put RadioButton elements in the group. buttonStyle is outline (the default) or solid.

Size

size sets the height of the buttons: large, medium and small. middle and default are accepted as aliases of medium. Plain radios ignore it.

Layout

orientation (and its shorthand vertical; orientation wins), block to fill the parent, and your own markup around the radios.

Plan: free, refused 0 times

Controlled

A function binding lets the parent refuse a choice: the previous radio is put back. preventDefault() in a forwarded onclick cancels a selection at once. Assigning value = undefined clears the selection.

Custom semantic parts

classNames and styles address the parts: root, icon and label of a radio, root and label of a button, root, item, itemIcon and itemLabel of a group. class and style go to the root.

No visible text, named by aria-label
Named by a heading
Uses the radio of your device.
Pick a colour

Names and labels

Every radio and every group needs an accessible name: the text of the radio, aria-label, aria-labelledby or an external <label for>. A group is a radiogroup: name it with aria-labelledby or aria-label.

Forms and native attributes

Radios take part in forms: name, value and required reach the input. A group without a name is never submitted. A native reset does not change the state: reset it in onreset.

no change yet

Keyboard

A group is one Tab stop, on the selected radio. The arrow keys move the focus and select (Chromium and Firefox wrap around at the ends, WebKit stops there); Space selects the focused radio. Two groups never trade focus. This is the browser's own behavior for radios that share a name.

API

PropertyDescriptionTypeDefault
checkedWhether the radio is selected. Use bind:checked; a plain checked={x} is only the initial value. Inside a group it is ignored.booleanfalse
disabledDisables the radio. A group's disabled applies as well.booleanfalse
valueIdentity inside a RadioGroup and the native value of the input, written as text. Required inside a group.string | number | boolean—
nameNative 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—
childrenLabel content. It is the accessible name; without it, name the radio with aria-label or an external label.Snippet—
onchangeCalled 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—
classNamesExtra classes for the parts of the radio.Partial<Record<'root' | 'icon' | 'label', string>>—
stylesInline 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>>>—
classExtra class of the root, after classNames.root.string—
styleInline style of the root; styles.root is written after it.string—
titleNative title of the root label, so the tooltip covers the mark and the text.string—
native attributesEverything 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

PropertyDescriptionTypeDefault
checkedWhether the radio is selected. Use bind:checked; a plain checked={x} is only the initial value. Inside a group it is ignored.booleanfalse
disabledDisables the radio. A group's disabled applies as well.booleanfalse
valueIdentity inside a RadioGroup and the native value of the input, written as text. Required inside a group.string | number | boolean—
nameNative 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—
childrenLabel content. It is the accessible name; without it, name the radio with aria-label or an external label.Snippet—
onchangeCalled 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—
classNamesExtra classes for the parts of the button.Partial<Record<'root' | 'label', string>>—
stylesInline 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>>>—
classExtra class of the root, after classNames.root.string—
styleInline style of the root; styles.root is written after it.string—
titleNative title of the root label, so the tooltip covers the mark and the text.string—
native attributesEverything 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

PropertyDescriptionTypeDefault
valueThe 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—
optionsRenders 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>[]
optionTypeWhat options render: plain radios or buttons. Children keep the type their author chose.'default' | 'button''default'
buttonStyleHow a checked button is drawn: a coloured outline or a solid fill. Reaches every RadioButton inside.'outline' | 'solid''outline'
sizeHeight 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'
orientationDirection of the items. When it is set it wins over vertical; without both the items are horizontal.'horizontal' | 'vertical'—
verticalA shorthand for orientation="vertical".booleanfalse
blockThe group fills the width of its parent and its items share it equally.booleanfalse
disabledDisables every radio of the group; an item's own disabled disables that item alone.booleanfalse
nameNative 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—
childrenCustom markup with Radio and RadioButton elements inside; each needs a value. Ignored when options has entries.Snippet—
onchangeCalled 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—
classNamesExtra 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>>—
stylesInline 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>>>—
classExtra class of the group root.string—
styleInline style of the group root; styles.root is written after it.string—
native attributesEverything 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

PropertyDescriptionTypeDefault
labelLabel content: plain text, or a snippet for icons and markup.string | Snippet—
valueThe value; it must be unique within the group.string | number | boolean—
disabledDisables this option alone.boolean—
titleNative title of the option's root label.string—
idNative id of the option's input, so an external <label for> or aria-labelledby can reach it.string—
requiredNative required of the option's input; one required radio makes the whole native group required.boolean—
classExtra class of the option's root.string—
styleInline style of the option's root.string—
onchangeCalled when this option is selected by the user, before the group's onchange.(checked: boolean, event: Event) => void—

Methods

MethodDescriptionType
focusMoves focus to the radio. Available on Radio and RadioButton through bind:this.(options?: FocusOptions) => void
blurRemoves focus from the radio.() => void

Design tokens

TokenDescriptionDefault
--ads-radio-bgBackground of the mark.#ffffff
--ads-radio-borderBorder of an unchecked mark.#d9d9d9
--ads-radio-border-hoverBorder of an unchecked mark on hover.#1677ff
--ads-radio-border-widthBorder width of the mark.1px
--ads-radio-button-bgBackground of a button.#ffffff
--ads-radio-button-borderBorder of an unselected button.#d9d9d9
--ads-radio-button-checked-bgBackground of a selected outline button.#ffffff
--ads-radio-button-checked-disabled-bgBackground of a disabled, selected button.rgba(0, 0, 0, 0.15)
--ads-radio-button-checked-disabled-colorText colour of a disabled, selected button.rgba(0, 0, 0, 0.25)
--ads-radio-button-colorText colour of an unselected button.rgba(0, 0, 0, 0.88)
--ads-radio-button-disabled-bgBackground of a disabled button.rgba(0, 0, 0, 0.04)
--ads-radio-button-disabled-borderBorder of a disabled button.#d9d9d9
--ads-radio-button-disabled-colorText colour of a disabled button.rgba(0, 0, 0, 0.25)
--ads-radio-button-padding-inlineInline padding of a medium and a large button.15px
--ads-radio-button-padding-inline-smInline padding of a small button.7px
--ads-radio-checked-bgFill and border of a selected mark.#1677ff
--ads-radio-checked-bg-hoverFill of a selected mark on hover.#4096ff
--ads-radio-disabled-bgBackground of a disabled mark.rgba(0, 0, 0, 0.04)
--ads-radio-disabled-borderBorder of a disabled mark.#d9d9d9
--ads-radio-disabled-dot-colorColour of the dot of a disabled radio.rgba(0, 0, 0, 0.25)
--ads-radio-dot-colorColour of the dot.#ffffff
--ads-radio-dot-sizeDiameter of the dot of a selected radio.6px
--ads-radio-font-sizeFont size of the label and of the group.14px
--ads-radio-group-gapGap between the radios of a group.8px
--ads-radio-label-paddingPadding of the label on both sides.8px
--ads-radio-line-heightLine height of the label.1.5714
--ads-radio-sizeDiameter 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 with aria-label or aria-labelledby (or a <fieldset> with a <legend>); name a radio with its text, aria-label, aria-labelledby or 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.