Radio Group
A group that holds the shared state of a series of radio buttons.
<script setup lang="ts">
import { useId } from 'vue';
import { Radio } from '@vase-ui/vue/radio';
import { RadioGroup } from '@vase-ui/vue/radio-group';
import styles from './index.module.css';
const captionId = useId();
</script>
<template>
<RadioGroup :aria-labelledby="captionId" default-value="fuji-apple" :class="styles.RadioGroup">
<div :class="styles.Caption" :id="captionId">Best apple</div>
<label :class="styles.Item">
<Radio value="fuji-apple" :class="styles.Radio" />
Fuji
</label>
<label :class="styles.Item">
<Radio value="gala-apple" :class="styles.Radio" />
Gala
</label>
<label :class="styles.Item">
<Radio value="granny-smith-apple" :class="styles.Radio" />
Granny Smith
</label>
</RadioGroup>
</template>.RadioGroup {
display: flex;
flex-direction: column;
align-items: start;
gap: 0.25rem;
color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Caption {
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 700;
}
.Item {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 400;
}
.Radio {
appearance: none;
box-sizing: border-box;
flex-shrink: 0;
width: 1rem;
height: 1rem;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 100%;
background-color: white;
padding: 0;
margin: 0;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
}
/* An inner ring leaves a dot in the middle. */
&:checked {
background-color: oklch(14.5% 0 0deg);
box-shadow: inset 0 0 0 0.1875rem white;
@media (prefers-color-scheme: dark) {
background-color: white;
box-shadow: inset 0 0 0 0.1875rem oklch(14.5% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: 2px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { useId } from 'vue';
import { Radio } from '@vase-ui/vue/radio';
import { RadioGroup } from '@vase-ui/vue/radio-group';
const captionId = useId();
</script>
<template>
<RadioGroup
:aria-labelledby="captionId"
default-value="fuji-apple"
class="flex flex-col items-start gap-1 text-neutral-950 dark:text-white"
>
<div class="text-sm font-bold" :id="captionId">Best apple</div>
<label class="flex items-center gap-2 text-sm font-normal text-neutral-950 dark:text-white">
<Radio
value="fuji-apple"
class="m-0 size-4 shrink-0 appearance-none rounded-full border border-neutral-950 bg-white p-0 checked:bg-neutral-950 checked:shadow-[inset_0_0_0_0.1875rem_white] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950 dark:border-white dark:bg-neutral-950 dark:checked:bg-white dark:checked:shadow-[inset_0_0_0_0.1875rem_var(--color-neutral-950)] dark:focus-visible:outline-white"
/>
Fuji
</label>
<label class="flex items-center gap-2 text-sm font-normal text-neutral-950 dark:text-white">
<Radio
value="gala-apple"
class="m-0 size-4 shrink-0 appearance-none rounded-full border border-neutral-950 bg-white p-0 checked:bg-neutral-950 checked:shadow-[inset_0_0_0_0.1875rem_white] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950 dark:border-white dark:bg-neutral-950 dark:checked:bg-white dark:checked:shadow-[inset_0_0_0_0.1875rem_var(--color-neutral-950)] dark:focus-visible:outline-white"
/>
Gala
</label>
<label class="flex items-center gap-2 text-sm font-normal text-neutral-950 dark:text-white">
<Radio
value="granny-smith-apple"
class="m-0 size-4 shrink-0 appearance-none rounded-full border border-neutral-950 bg-white p-0 checked:bg-neutral-950 checked:shadow-[inset_0_0_0_0.1875rem_white] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950 dark:border-white dark:bg-neutral-950 dark:checked:bg-white dark:checked:shadow-[inset_0_0_0_0.1875rem_var(--color-neutral-950)] dark:focus-visible:outline-white"
/>
Granny Smith
</label>
</RadioGroup>
</template>Usage guidelines
- Form controls must have an accessible name: Name the group with
aria-labelledby, aField.Labelor aFieldset.Legend. Name each radio with a<label>element, or with aField.Labelin aField.Item. See Labeling a radio group and the forms guide.
Anatomy
Import the components and assemble their parts:
<script setup lang="ts">
import { Radio } from '@vase-ui/vue/radio';
import { RadioGroup } from '@vase-ui/vue/radio-group';
</script>
<template>
<RadioGroup>
<Radio value="a" />
</RadioGroup>
</template>Examples
Labeling a radio group
Label the group with aria-labelledby and a sibling label element:
<script setup lang="ts">
import { Radio } from '@vase-ui/vue/radio';
import { RadioGroup } from '@vase-ui/vue/radio-group';
</script>
<template>
<div id="storage-type-label">Storage type</div>
<RadioGroup aria-labelledby="storage-type-label">
<label>
<Radio value="ssd" />
SSD
</label>
<label>
<Radio value="hdd" />
HDD
</label>
</RadioGroup>
</template>A <label> around each radio is the simplest way to label it:
<script setup lang="ts">
import { Radio } from '@vase-ui/vue/radio';
import { RadioGroup } from '@vase-ui/vue/radio-group';
</script>
<template>
<RadioGroup aria-label="Storage type">
<label>
<Radio value="ssd" />
SSD
</label>
</RadioGroup>
</template>A Fieldset.Legend names the group in its Fieldset.Root. Put each radio in a Field.Item with its Field.Label:
<script setup lang="ts">
import { Field } from '@vase-ui/vue/field';
import { Fieldset } from '@vase-ui/vue/fieldset';
import { Radio } from '@vase-ui/vue/radio';
import { RadioGroup } from '@vase-ui/vue/radio-group';
</script>
<template>
<Field.Root name="storage">
<Fieldset.Root>
<Fieldset.Legend>Storage type</Fieldset.Legend>
<RadioGroup>
<Field.Item>
<Radio value="ssd" />
<Field.Label>SSD</Field.Label>
</Field.Item>
<Field.Item>
<Radio value="hdd" />
<Field.Label>HDD</Field.Label>
</Field.Item>
</RadioGroup>
</Fieldset.Root>
</Field.Root>
</template>Show a mark
Give the input appearance: none and draw the mark with CSS, as the demo does. Or render the input in the element slot, and render a mark from state.checked:
<script setup lang="ts">
import { Radio } from '@vase-ui/vue/radio';
import { RadioGroup } from '@vase-ui/vue/radio-group';
</script>
<template>
<RadioGroup aria-label="Storage type" default-value="ssd">
<label v-for="value in ['ssd', 'hdd']" :key="value">
<Radio :value="value">
<template #element="{ props, elementRef, state }">
<span class="box">
<input v-bind="props" :ref="elementRef" class="radio" />
<span v-if="state.checked" class="dot" />
</span>
</template>
</Radio>
{{ value.toUpperCase() }}
</label>
</RadioGroup>
</template>
<style scoped>
.box {
position: relative;
display: inline-flex;
}
.radio {
appearance: none;
width: 1rem;
height: 1rem;
margin: 0;
border: 1px solid;
border-radius: 50%;
}
.dot {
position: absolute;
inset: 0.25rem;
border-radius: 50%;
background: currentColor;
pointer-events: none;
}
</style>API reference
RadioGroup
Holds the shared state of a series of radio buttons.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultValue | Value | undefined | The value of a group that has no modelValue, at the mount and at a native reset of the
form. | |
disabled | boolean | undefined | false | Disables the group and each radio in it. |
form | string | undefined | The id of the form that owns the radios, for a group outside that form. | |
modelValue | Value | undefined | The value of the selected radio. Bind it with v-model. With no modelValue, the group keeps
its own value. | |
name | string | undefined | The name of the radios in the data of the form. The name of a Field.Root around the group
wins. | |
readonly | boolean | undefined | false | The user cannot change the value. The arrow keys move the focus and select no radio, and the group is not validated. |
required | boolean | undefined | false | The user must select a radio before a submit of the form. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: RadioGroupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: RadioGroupState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Events
| Event | Arguments | Description |
|---|---|---|
valueChange | [value: Value, details: RadioGroupChangeEventDetails] | The user selects a radio. details.event is the native click event of the press.
details.cancel() keeps the value as it was. |
update:modelValue | [value: Value | undefined] | Comes after the change event with the new value, unless a listener cancels the change. v-model uses it. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLDivElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[aria-disabled="true"] | The group is disabled. |
[aria-readonly="true"] | The group is read-only. |
[aria-required="true"] | The group or one of its radios is required. |
[data-valid] | The field of the group is valid. |
[aria-invalid="true"] | The field of the group is invalid. |
[data-touched] | In a field, the focus left the group. |
[data-dirty] | In a field, the value differs from the first value. |
[data-filled] | In a field, the group has a value. |
Radio
A radio button that the user can select, in a RadioGroup.
Renders an <input> element with the type radio.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | undefined | false | Disables the radio. The arrow keys skip it. A disabled group, field or fieldset also disables it. |
id | string | undefined | The id of the radio. With no id, the radio has an id of its own. | |
readonly | boolean | undefined | false | The user can reach the radio and cannot select it. |
required | boolean | undefined | false | Makes the group required, as required on one native radio does. |
value | Value | Required | The value that the group gets when the user selects this radio. |
Slots
| Slot | Slot props | Description |
|---|---|---|
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: RadioState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLInputElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
:checked | The radio is selected. |
:disabled | The radio is disabled. |
:required | The group is required, and the radio is not read-only. |
[data-readonly] | The radio is read-only. |
[data-valid] | The field of the group is valid. |
[data-invalid] | The field of the group is invalid. |
[data-touched] | In a field, the focus left the group. |
[data-dirty] | In a field, the value differs from the first value. |
Differences from Base UI
Vase UI ports Radio Group from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
RadioGroup | readonly is in place of readOnly, in the props and in the state. The element of the template ref of each Radio is its input, so the group has no inputRef. CSS selects :focus-within, so the state has no focused. Vue gives an absent Boolean prop the value false, so disabled and required need no default. |
Radio.Root | Radio is the part: one native <input type="radio">, so it needs no nativeButton. Its types are Radio.Props and Radio.State, and the flat names RadioProps and RadioState. readonly is in place of readOnly. The element of the template ref is the input, in place of inputRef. CSS selects :checked, :disabled, :required and :focus, so the radio has no data-checked, data-unchecked, data-disabled, data-required, data-filled and data-focused. |
Radio.Indicator | CSS on the input, an element of the developer beside the input, or the element slot with state gives the mark. |
Added in Vase UI
| Part | Member | Why |
|---|---|---|
RadioGroup | readonly prop | The native name of the attribute. A read-only group stays in the tab order, and the user cannot change its value. |
RadioGroup | readonly state field | The state has the name of the prop. |
Behavior
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.