Radio Group
Pick exactly one option from a short list. Same markup and props in every style: switch the style in the header to see it change.
Basic
Native radios in a fieldset. The legend names the group, and ↑ ↓ move between options with no JavaScript. Uses the same label layout as Checkbox.
<fieldset class="vin-radio-group">
<legend class="vin-radio-group__legend">Email me</legend>
<div class="vin-choice">
<input id="freq-instant" class="vin-radio" type="radio" name="frequency" value="instant">
<div class="vin-choice__body"><label class="vin-choice__label" for="freq-instant">Instantly</label></div>
</div>
<div class="vin-choice">
<input id="freq-daily" class="vin-radio" type="radio" name="frequency" value="daily" checked>
<div class="vin-choice__body"><label class="vin-choice__label" for="freq-daily">Once a day</label></div>
</div>
<div class="vin-choice">
<input id="freq-weekly" class="vin-radio" type="radio" name="frequency" value="weekly">
<div class="vin-choice__body"><label class="vin-choice__label" for="freq-weekly">Once a week</label></div>
</div>
<div class="vin-choice">
<input id="freq-never" class="vin-radio" type="radio" name="frequency" value="never">
<div class="vin-choice__body"><label class="vin-choice__label" for="freq-never">Never</label></div>
</div>
</fieldset>import { RadioGroup } from "@/components/vin/radio-group";
const options = [
{ value: "instant", label: "Instantly" },
{ value: "daily", label: "Once a day" },
{ value: "weekly", label: "Once a week" },
{ value: "never", label: "Never" },
];
export function RadioGroupBasic() {
return <RadioGroup label="Email me" options={options} defaultValue="daily" />;
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import RadioGroup from "@/components/vin/radio-group.vue";
const options = [
{ value: "instant", label: "Instantly" },
{ value: "daily", label: "Once a day" },
{ value: "weekly", label: "Once a week" },
{ value: "never", label: "Never" },
];
</script>
<template>
<RadioGroup label="Email me" :options="options" default-value="daily" />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import RadioGroup from "$lib/components/vin/radio-group.svelte";
const options = [
{ value: "instant", label: "Instantly" },
{ value: "daily", label: "Once a day" },
{ value: "weekly", label: "Once a week" },
{ value: "never", label: "Never" },
];
</script>
<RadioGroup label="Email me" {options} value="daily" />Horizontal
Short options can sit in a row. A disabled option stays readable.
<fieldset class="vin-radio-group vin-radio-group--horizontal">
<legend class="vin-radio-group__legend">Size</legend>
<div class="vin-choice">
<input id="size-s" class="vin-radio" type="radio" name="size" value="s">
<div class="vin-choice__body"><label class="vin-choice__label" for="size-s">S</label></div>
</div>
<div class="vin-choice">
<input id="size-m" class="vin-radio" type="radio" name="size" value="m" checked>
<div class="vin-choice__body"><label class="vin-choice__label" for="size-m">M</label></div>
</div>
<div class="vin-choice">
<input id="size-l" class="vin-radio" type="radio" name="size" value="l">
<div class="vin-choice__body"><label class="vin-choice__label" for="size-l">L</label></div>
</div>
<div class="vin-choice">
<input id="size-xl" class="vin-radio" type="radio" name="size" value="xl" disabled>
<div class="vin-choice__body"><label class="vin-choice__label" for="size-xl">XL (sold out)</label></div>
</div>
</fieldset>import { RadioGroup } from "@/components/vin/radio-group";
const sizes = [
{ value: "s", label: "S" },
{ value: "m", label: "M" },
{ value: "l", label: "L" },
{ value: "xl", label: "XL (sold out)", disabled: true },
];
export function RadioGroupHorizontal() {
return <RadioGroup label="Size" options={sizes} defaultValue="m" orientation="horizontal" />;
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import RadioGroup from "@/components/vin/radio-group.vue";
const sizes = [
{ value: "s", label: "S" },
{ value: "m", label: "M" },
{ value: "l", label: "L" },
{ value: "xl", label: "XL (sold out)", disabled: true },
];
</script>
<template>
<RadioGroup label="Size" :options="sizes" default-value="m" orientation="horizontal" />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import RadioGroup from "$lib/components/vin/radio-group.svelte";
const sizes = [
{ value: "s", label: "S" },
{ value: "m", label: "M" },
{ value: "l", label: "L" },
{ value: "xl", label: "XL (sold out)", disabled: true },
];
</script>
<RadioGroup label="Size" options={sizes} value="m" orientation="horizontal" />Cards
Bigger targets with a description each, for plans and other important choices. The whole card is clickable.
<fieldset class="vin-radio-group vin-radio-group--cards" style="width: 100%; max-width: 360px">
<legend class="vin-radio-group__legend">Plan</legend>
<div class="vin-choice">
<input id="plan-hobby" class="vin-radio" type="radio" name="plan" value="hobby" aria-describedby="plan-hobby-desc">
<div class="vin-choice__body">
<label class="vin-choice__label" for="plan-hobby">Hobby</label>
<p class="vin-choice__desc" id="plan-hobby-desc">Free for personal projects.</p>
</div>
</div>
<div class="vin-choice">
<input id="plan-pro" class="vin-radio" type="radio" name="plan" value="pro" checked aria-describedby="plan-pro-desc">
<div class="vin-choice__body">
<label class="vin-choice__label" for="plan-pro">Pro</label>
<p class="vin-choice__desc" id="plan-pro-desc">For small teams, billed monthly.</p>
</div>
</div>
<div class="vin-choice">
<input id="plan-enterprise" class="vin-radio" type="radio" name="plan" value="enterprise" aria-describedby="plan-enterprise-desc">
<div class="vin-choice__body">
<label class="vin-choice__label" for="plan-enterprise">Enterprise</label>
<p class="vin-choice__desc" id="plan-enterprise-desc">Single sign-on and priority support.</p>
</div>
</div>
</fieldset>import { RadioGroup } from "@/components/vin/radio-group";
const plans = [
{ value: "hobby", label: "Hobby", description: "Free for personal projects." },
{ value: "pro", label: "Pro", description: "For small teams, billed monthly." },
{ value: "enterprise", label: "Enterprise", description: "Single sign-on and priority support." },
];
export function RadioGroupCards() {
return <RadioGroup label="Plan" options={plans} defaultValue="pro" variant="cards" style={{ width: "100%", maxWidth: 360 }} />;
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import RadioGroup from "@/components/vin/radio-group.vue";
const plans = [
{ value: "hobby", label: "Hobby", description: "Free for personal projects." },
{ value: "pro", label: "Pro", description: "For small teams, billed monthly." },
{ value: "enterprise", label: "Enterprise", description: "Single sign-on and priority support." },
];
</script>
<template>
<RadioGroup label="Plan" :options="plans" default-value="pro" variant="cards" style="width: 100%; max-width: 360px" />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import RadioGroup from "$lib/components/vin/radio-group.svelte";
const plans = [
{ value: "hobby", label: "Hobby", description: "Free for personal projects." },
{ value: "pro", label: "Pro", description: "For small teams, billed monthly." },
{ value: "enterprise", label: "Enterprise", description: "Single sign-on and priority support." },
];
</script>
<RadioGroup label="Plan" options={plans} value="pro" variant="cards" style="width: 100%; max-width: 360px" />Installation
React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:
npx @vinish/ui add radio-groupimport { RadioGroup } from "@/components/vin/radio-group";import RadioGroup from "@/components/vin/radio-group.vue";import RadioGroup from "$lib/components/vin/radio-group.svelte";The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.
Plain HTML: the classes come with the style's stylesheet. It needs no JavaScript.
API
From spec/radio-group.json, which every framework version is tested against.
| Prop | Values | Default | In HTML |
|---|---|---|---|
labelThe group's name, shown as the legend. | ReactNode | — | <legend class="vin-radio-group__legend"> |
optionsThe choices. | (string | { value, label, description?, disabled? })[] | — | .vin-choice > input.vin-radio per option |
value / defaultValueThe chosen value (controlled or not). Use onValueChange to hear about changes. | string | — | checked attribute |
nameForm field name. Generated if you leave it out. | string | — | name attribute (the same on every radio) |
orientation | verticalhorizontal | vertical | vin-radio-group--horizontal |
variant | defaultcards | default | vin-radio-group--cards |
errorAn error message. Also marks the radios invalid. | ReactNode | — | <p class="vin-error"> + aria-describedby on the fieldset |
disabled | boolean | false | disabled on the fieldset, or on one radio |
Parts
| React, Vue, Svelte | HTML classes |
|---|---|
<RadioGroup> | .vin-radio-group.vin-radio-group--horizontal.vin-radio-group--cards.vin-radio-group__legend.vin-radio |
Accessibility
- Native <input type="radio"> with one shared name: arrow keys move and select, Tab enters and leaves the group.
- A <fieldset> with a <legend> gives the group its name.
- Every radio has a <label for>; descriptions are linked with aria-describedby.
- In cards, the label covers the whole card, so the click target is large and still one labelled control.
- Visible focus ring in every style and mode.
Customize
Saved in this browser. Style and accent are in the header.