Radio Group
Provides shared state to a series of radio buttons.
Usage guidelines
- Form controls must have an accessible name: It can be created using
<label>elements, or theFieldandFieldsetcomponents. See Labeling a radio group and the forms guide.
Anatomy
Import the component and assemble its parts:
Examples
Labeling a radio group
Label the group with aria-labelledby and a sibling label element:
An enclosing <label> is the simplest labeling pattern for each radio:
Rendering as a native button
By default, <Radio.Root> renders a <span> element to support enclosing labels. Prefer rendering each radio as a native button when using sibling labels (for/id).
Native buttons with wrapping labels are supported by using the render snippet to avoid invalid HTML, so the hidden input is placed outside the label:
Form integration
Use Field and Fieldset for group labeling and form integration:
API reference
RadioGroup
value Value —
- Name
- Description
The selected radio's value. Use
bind:valueto share it with the parent. Hold the value witheventDetails.cancel()to keep a click from sticking. Object values are compared with===. Store them with$state.raw.- Type
Value- Binding
bind:value
defaultValue Value undefined
- Name
- Description
The value an empty
valuestarts from.- Type
Value- Default
undefined
disabled boolean | null false
- Name
- Description
Whether the group should ignore user interaction.
- Type
boolean | null- Default
false
readonly boolean | null false
- Name
- Description
Whether the user should be unable to select a different radio button in the group.
- Type
boolean | null- Default
false
required boolean | null false
- Name
- Description
Whether the user must choose a value before submitting a form.
- Type
boolean | null- Default
false
name string | null —
- Name
- Description
Identifies the field when a form is submitted.
- Type
string | null
form string | null —
- Name
- Description
Identifies the form that owns the radio inputs. Useful when the radio group is rendered outside the form.
- Type
string | null
onValueChange function —
- Name
- Description
Called before the value changes. Call
eventDetails.cancel()to veto it.- Type
(value: Value, eventDetails: RadioGroupChangeEventDetails) => void
render Snippet<[props: HTMLAttributes<HTMLDivElement>, state: RadioGroupState, children: Snippet | undefined]> —
- Name
- Description
Replace the default
<div>. Spreadpropsonto the host element.- Type
Snippet<[props: HTMLAttributes<HTMLDivElement>, state: RadioGroupState, children: Snippet | undefined]>
disabled boolean
- Name
disabled- Description
Whether the group should ignore user interaction.
- Type
boolean
readOnly boolean
- Name
readOnly- Description
Whether the user should be unable to select a different radio.
- Type
boolean
required boolean
- Name
required- Description
Whether the user must choose a value before submitting a form.
- Type
boolean
touched boolean
- Name
touched- Description
Whether the field has been touched.
- Type
boolean
dirty boolean
- Name
dirty- Description
Whether the field value has changed from its initial value.
- Type
boolean
valid boolean | null
- Name
valid- Description
Whether the field is valid.
nullmeans validity is not known yet.- Type
boolean | null
filled boolean
- Name
filled- Description
Whether the field has a value.
- Type
boolean
focused boolean
- Name
focused- Description
Whether the field is focused.
- Type
boolean
data-disabled
Root
Represents the radio button itself. Renders a <span> and a hidden radio beside it.
value* unknown —
- Name
- Description
The unique identifying value of the radio. Without a radio group, the radio is selected only when this is
''.- Type
unknown
disabled boolean | null false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean | null- Default
false
readonly boolean | null false
- Name
- Description
Whether the user should be unable to select the radio button.
- Type
boolean | null- Default
false
required boolean | null false
- Name
- Description
Whether the user must choose a value before submitting a form.
- Type
boolean | null- Default
false
nativeButton boolean false
- Name
- Description
Whether the host is a native
<button>. Settrueand render a<button>. Theidis then applied to that button instead of the hidden input.- Type
boolean- Default
false
id string | null —
- Name
- Description
The id of the hidden input. When
nativeButtonis true, the id is applied to the root element instead.- Type
string | null
onclick MouseEventHandler<HTMLElement> | null —
- Name
- Description
Runs before the radio's click handler. Call
event.preventDefault()to skip it.- Type
MouseEventHandler<HTMLElement> | null
render Snippet<[props: RadioHostProps, state: RadioRootState, children: Snippet | undefined]> —
- Name
- Description
Replace the default
<span>. Spreadpropsonto the host element.- Type
Snippet<[props: RadioHostProps, state: RadioRootState, children: Snippet | undefined]>
checked boolean
- Name
checked- Description
Whether the radio button is currently selected.
- Type
boolean
disabled boolean
- Name
disabled- Description
Whether the component should ignore user interaction.
- Type
boolean
readOnly boolean
- Name
readOnly- Description
Whether the user should be unable to select the radio button.
- Type
boolean
required boolean
- Name
required- Description
Whether the user must choose a value before submitting a form.
- Type
boolean
touched boolean
- Name
touched- Description
Whether the field has been touched.
- Type
boolean
dirty boolean
- Name
dirty- Description
Whether the field value has changed from its initial value.
- Type
boolean
valid boolean | null
- Name
valid- Description
Whether the field is valid.
nullmeans validity is not known yet.- Type
boolean | null
filled boolean
- Name
filled- Description
Whether the field has a value.
- Type
boolean
focused boolean
- Name
focused- Description
Whether the field is focused.
- Type
boolean
data-checked
data-unchecked
data-disabled
data-readonly
data-required
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
Indicator
Indicates whether the radio button is selected. Renders a <span> while selected or kept mounted.
keepMounted boolean false
- Name
- Description
Whether to keep the indicator in the DOM when the radio is not selected.
- Type
boolean- Default
false
render Snippet<[props: HTMLAttributes<HTMLSpanElement>, state: RadioIndicatorState, children: Snippet | undefined]> —
- Name
- Description
Replace the default
<span>. Spreadpropsonto the host element.- Type
Snippet<[props: HTMLAttributes<HTMLSpanElement>, state: RadioIndicatorState, children: Snippet | undefined]>
transitionStatus RadioIndicatorPhase
- Name
transitionStatus- Description
Indicator motion phase.
- Type
RadioIndicatorPhase
checked boolean
- Name
checked- Description
Whether the radio button is currently selected.
- Type
boolean
disabled boolean
- Name
disabled- Description
Whether the component should ignore user interaction.
- Type
boolean
readOnly boolean
- Name
readOnly- Description
Whether the user should be unable to select the radio button.
- Type
boolean
required boolean
- Name
required- Description
Whether the user must choose a value before submitting a form.
- Type
boolean
touched boolean
- Name
touched- Description
Whether the field has been touched.
- Type
boolean
dirty boolean
- Name
dirty- Description
Whether the field value has changed from its initial value.
- Type
boolean
valid boolean | null
- Name
valid- Description
Whether the field is valid.
nullmeans validity is not known yet.- Type
boolean | null
filled boolean
- Name
filled- Description
Whether the field has a value.
- Type
boolean
focused boolean
- Name
focused- Description
Whether the field is focused.
- Type
boolean