Skip to content

Radio Group ​

A group that holds the shared state of a series of radio buttons.

Best apple
vue
<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>
css
.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;
    }
  }
}
vue
<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, a Field.Label or a Fieldset.Legend. Name each radio with a <label> element, or with a Field.Label in a Field.Item. See Labeling a radio group and the forms guide.

Anatomy ​

Import the components and assemble their parts:

vue
<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:

vue
<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:

vue
<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:

vue
<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:

vue
<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

PropTypeDefaultDescription
defaultValueValue | undefinedThe value of a group that has no modelValue, at the mount and at a native reset of the form.
disabledboolean | undefinedfalseDisables the group and each radio in it.
formstring | undefinedThe id of the form that owns the radios, for a group outside that form.
modelValueValue | undefinedThe value of the selected radio. Bind it with v-model. With no modelValue, the group keeps its own value.
namestring | undefinedThe name of the radios in the data of the form. The name of a Field.Root around the group wins.
readonlyboolean | undefinedfalseThe user cannot change the value. The arrow keys move the focus and select no radio, and the group is not validated.
requiredboolean | undefinedfalseThe user must select a radio before a submit of the form.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: RadioGroupState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: RadioGroupState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Events

EventArgumentsDescription
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

MemberTypeDescription
elementHTMLDivElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
disabledboolean | undefinedfalseDisables the radio. The arrow keys skip it. A disabled group, field or fieldset also disables it.
idstring | undefinedThe id of the radio. With no id, the radio has an id of its own.
readonlyboolean | undefinedfalseThe user can reach the radio and cannot select it.
requiredboolean | undefinedfalseMakes the group required, as required on one native radio does.
valueValueRequiredThe value that the group gets when the user selects this radio.

Slots

SlotSlot propsDescription
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: RadioState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLInputElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
:checkedThe radio is selected.
:disabledThe radio is disabled.
:requiredThe 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 UIVase UI
RadioGroupreadonly 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.RootRadio 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.IndicatorCSS on the input, an element of the developer beside the input, or the element slot with state gives the mark.

Added in Vase UI ​

PartMemberWhy
RadioGroupreadonly propThe native name of the attribute. A read-only group stays in the tab order, and the user cannot change its value.
RadioGroupreadonly state fieldThe state has the name of the prop.

Behavior ​

Vase UIEvidence
When validationMode is 'blur', a required RadioGroup with no value is invalid after the focus leaves it. The group or its field needs a name. In that mode, a required Select.Root with no value is invalid after the focus leaves its trigger, and after an outside press closes its popup. A required Combobox.Root with a selection mode and no value, whose trigger is the control, is invalid after the focus leaves its trigger. It is also invalid after its popup closes at an outside press or when the focus leaves. A Combobox whose input is outside the popup keeps the rule of a text field. Base UI shows no error until the form is submitted.
The server HTML of RadioGroup has its role and the id of its label in aria-labelledby. It also has the checked radio, and the name and the value of each radio. The checked radio has the tab stop before and after the hydration.
A disabled <fieldset>, native or Fieldset.Root, disables each form part in it as its disabled prop does, also with no Field.Root. A part in the first <legend> child of a native fieldset stays enabled after the mount, as in HTML. Fieldset.Legend renders a <div>, so a part in it is disabled. A disabled part has data-disabled, the native disabled or aria-disabled, and the disabled slot state. A press, Space or a stepper button changes no value, and Tab and the arrow keys skip the part. The values of Form do not include its value. The parts follow each later change of a native fieldset. Each part that renders a native button follows the fieldset too, and a disabled trigger cannot open its popup on hover. A ToggleGroup in a disabled fieldset has data-disabled and the disabled slot state, as its native toggles do. Base UI follows only Fieldset.Root, through Field.Root. In a native disabled fieldset, its controls show no disabled state. Tab focuses a radio, a checkbox or a switch, and a press or Space changes its value. Its native buttons get no fieldset state, and its hover triggers can open their popups.
When the item that has the tab stop of a composite becomes disabled while the focus is outside the composite, and the arrow keys skip that item, the tab stop moves to the active item when the arrow keys reach it, or else to the first item that they reach. When the item becomes disabled while it has the focus, the tab stop moves when the focus leaves. At the mount, a checked radio that is a disabled native button also gives the tab stop to the first radio that the arrow keys reach. The arrow keys skip a natively disabled item. They also skip an aria-disabled item, unless the root gives disabledIndices and the item is not in it. Thus the Tab key does not focus a radio that the arrow keys skip in a Radio Group with no checked radio, and a Toggle Group or a Menubar stays in the order of the Tab key. Toolbar.Root keeps the tab stop on a disabled item that stays focusable. An active item that becomes disabled keeps the tab stop while it can get focus, as at the mount. In Base UI, an item that becomes disabled keeps the tab stop, and at the mount a checked radio keeps it also when it is a disabled native button. The Tab key then focuses a disabled radio, or it skips a whole Radio Group, Toggle Group or Menubar, because the item with the tab stop has the disabled attribute. A native control also follows its fieldset when it or its wrapper moves. Controls in the first legend keep the native exemption.
A field counts for a Form while the browser lists an input of its control in the elements of the <form> (element.form). A control that a Teleport moves out of the <form> counts when it has the form attribute with the id of the form, as in HTML. A group with no mounted input does not count. So the fields that the form validates and the values that the form submits (FormData) are the same set. Base UI counts each field below the Form in the React tree, also a control in a portal and a group with no input.
An arrow key in a radio group selects the next radio, and the browser sends a click event to that radio, as Chromium, Firefox and WebKit do for native radios. The event reaches the listeners of the radio and of its ancestors. Base UI sends no click for an arrow key.
A radio group and a checkbox group are one control each. The field becomes touched when the focus leaves the group. A move of the focus from one radio to another in the group, also by an arrow key that selects, is not a leave, so a field in blur mode does not validate in the middle of a selection by keyboard. Base UI marks the field touched at a move in the group.
A control in a field keeps the id of the developer in the server HTML when no Field.Label of the field renders before it, and a label that renders after the control has that id in for. When a label renders before the control, the control has the generated id of the field until the hydration, because that label names the generated id. Then the control takes its id, and the label follows. In each order, the label names the control in the server HTML. Base UI gives each control in a field the generated id until the hydration, also where no label needs it.

The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.