Radio Group

updated

You have no choice. Well, one. A set of radio buttons where exactly one can be selected.

Radio demo

Open Props in the corner of the example to try color, size and disabled. Disable one option with its Field, or the whole group with a FieldSet.

color works as on the checkbox: Button's colours or your own, an --input ring until selected, and red when invalid. Set it on the RadioGroup for every radio, or on one Radio to single it out.

The same scale as the checkbox: xs, 14px, to xl, 24px, with md, 18px, as the default, so radios and checkboxes line up in one form.

Installation

Usage

<script setup lang="ts">
import { Field, FieldLabel } from '@/components/ui/field'
import { Radio, RadioGroup } from '@/components/ui/radio-group'

const density = ref('comfortable')
</script>

<template>
  <RadioGroup v-model="density" aria-label="Density">
    <Field v-for="option in ['default', 'comfortable', 'compact']" :key="option" orientation="horizontal">
      <Radio :value="option" />
      <FieldLabel>{{ option }}</FieldLabel>
    </Field>
  </RadioGroup>
</template>

Works like Checkbox: same sizes, touch targets and group layouts. Here's what's different.

When selected, the ring turns its colour, --primary by default, and a dot grows in from the centre (instantly, with reduced motion).

Unlike checkboxes, the whole group is one Tab stop. Tab lands on the selected radio, or the first one, and arrow keys move the selection.

Examples

Touch target

Smaller than a finger? Add touch-target: expand when it stands alone, wrapper when it sits close to others. How it works.

With wrapper, the label moves along with the radio.

Radio touch target

Description

Name the group with a FieldSet and its legend, and give each option a FieldContent for its label and description.

Radio description
Shipping

Arrives in 4 to 6 working days.

Arrives tomorrow if ordered before 2 pm.

Invalid

Radios are wrong together, not one by one. Put invalid, required and the FieldError on the FieldSet, and the whole group turns red.

Try it: press Continue without picking a size. Reset clears the choice, since a radio can't be unchecked by clicking it again.

Radio invalid
T-shirt size

API Reference

Both parts accept class, merged over their base classes.

Data attributes

data-slotAttributesDescription
radio-groupdata-variant, data-color, data-orientation, data-disabledThe group.
radiodata-state, data-color, data-size, data-disabled, data-touch-targetOne radio. data-state is checked or unchecked. data-color is its own color, or the group's.

Changelog

  1. registry 0.22.0

    ac0cc05 — Checkbox, Radio, Switch and Slider no longer declare as and asChild, as Rating already does: Reka's slot cloned the control's own indicator, thumb or track into the root, which left an inert control. The props are gone from their API tables too.

    f29a239 — A row of a disabled RadioGroup or CheckboxGroup, or of a group inside a disabled FieldSet, looks disabled: choice-row drops its hover tint and shows a not-allowed cursor when the row's checkbox or radio is disabled, not only when its Field is, and FieldLabel dims beside a disabled checkbox or radio.

  2. core 0.16.0

    f29a239 — A row of a disabled RadioGroup or CheckboxGroup, or of a group inside a disabled FieldSet, looks disabled: choice-row drops its hover tint and shows a not-allowed cursor when the row's checkbox or radio is disabled, not only when its Field is, and FieldLabel dims beside a disabled checkbox or radio.

  3. registry 0.21.0

    e5495ef — size works the same way everywhere. Every component exports its size type (AlertSize, BadgeSize, CardSize, CheckboxSize, RadioSize, SwitchSize, EmptySize, InputFloatingSize, ItemSize, ListboxSize, SeparatorSize, SliderSize, StepperSize, TabsSize, TextareaSize, TimelineSize) and the props take it without null; Button-based parts take ButtonSize, Input TextControlSize, SelectTrigger SelectSize and Tree TreeSize. ToggleGroup and ToolbarToggleGroup carry data-size on the group as well. Table's row heights moved into tableSize, so a class on DataTable's ui.table overrides --table-row-h at every size, not only at md.

  4. registry 0.13.0

    5a478d3 — Checkbox, Radio, Switch, Slider, Progress, Stepper and TabsList take color: primary (the default), neutral, destructive, success, warning, info, or any name you declare on [data-slot][data-color="<name>"], as on Button. The root carries data-color. Whatever they draw straight on the page takes the colour's --tone-text: a checked edge, the radio's dot, the default slider's range and thumb, the progress bar and the tab line. That keeps a bright success, warning or info at 3:1 against the page. Fills with something on them, like the checked box, the switch track and the active step, keep --tone. On the choice controls the unchecked edge stays --input and an invalid control still turns destructive. Keyboard focus rings a control in a colour other than primary in its --tone-text, red once it's invalid. CheckboxGroup and RadioGroup pass their color to every control in them without one, and ChoiceGroup takes color for the selected card's edge, the focused card's ring and the selected row's tint. Switch, Checkbox, Radio and Slider now set data-size, md by default. They need the @kappa-ui/core release that lets tone-control follow data-color.

    Recoloured Switch, Checkbox, Radio or Slider with class="[--tone:…] [--tone-foreground:…]", as the theming page used to suggest? Their edges and marks now read --tone-text, so add [--tone-text:…] to the class, or use color.

  5. core 0.10.0

    5a478d3 — tone-control works with a color prop. On an element with data-slot and data-color it leaves --tone, --tone-foreground and --tone-text to the [data-slot][data-color] rules, so the control takes that tone, and it keeps --input as the unchecked edge. A colour other than primary also rings the control in its --tone-text. Any other element gets primary exactly as before, now including --tone-text, so copies of Switch, Checkbox, Radio and Slider made before the prop look the same. tone-invalid now turns --tone-text destructive along with the rest of the tone, so edges, marks and rings drawn in it go red on an invalid control too. Copies made before the prop don't read --tone-text, so they look the same. choice-row tints a selected option in its group's tone when the group has data-slot and data-color, and in primary when it doesn't.

  6. registry 0.10.0

    0f38faa — Switch, Checkbox, Radio and Slider take their tone from core's tone-control and tone-invalid utilities instead of a dozen variable assignments each, so they need the @kappa-ui/core release that adds them. They look the same, and a [--tone:…] class still recolours them. Long class strings across the components are now wrapped by variant group.

  7. registry 0.6.0

    4e35ec3 — Card takes size from xs to xl on one --card-spacing (md is the default and replaces default) and variant: outline (unchanged, the default), solid with a small shadow and no border, soft tinted, subtle tinted with a border. ChoiceGroup is the layout behind CheckboxGroup and RadioGroup's variant and orientation, in its own choice-group item; radio-group depends on it instead of on checkbox.