Radio Group
A set of options where only one can be picked.
Native <input type="radio">
<script setup lang="ts">
import { Label } from '@/components/ui/label';
import * as RadioGroup from '@/components/ui/radio-group';
import { ref } from 'vue';
const plan = ref('comfortable');
</script>
<template>
<RadioGroup.Root v-model="plan" aria-label="Spacing">
<Label><RadioGroup.Item value="default" /> Default</Label>
<Label><RadioGroup.Item value="comfortable" /> Comfortable</Label>
<Label><RadioGroup.Item value="compact" /> Compact</Label>
</RadioGroup.Root>
<p>Selected: {{ plan }}</p>
</template>
<style scoped>
p {
margin: var(--dui-space-3) 0 0;
color: var(--dui-color-muted-foreground);
font-size: var(--dui-text-sm);
}
</style>
Installation
npx distill-ui add radio-group- Set up the tokens once, if you haven't: see manual installation.
- Copy these files into your project:
<script setup lang="ts">
import { computed, useId } from 'vue';
import { provideRadioGroup } from './context.js';
const {
name,
disabled = false,
orientation = 'vertical'
} = defineProps<{ name?: string; disabled?: boolean; orientation?: 'horizontal' | 'vertical' }>();
const value = defineModel<string>({ default: '' });
const id = useId();
provideRadioGroup({
name: computed(() => name ?? `radio-group-${id}`),
value,
disabled: computed(() => disabled)
});
</script>
<template>
<div
role="radiogroup"
class="radio-group"
:aria-disabled="disabled || undefined"
:aria-orientation="orientation"
:data-orientation="orientation"
>
<slot />
</div>
</template>
<!--
Options you can set from a parent or on the radio group itself:
--dui-radio-group-gap
-->
<style scoped>
.radio-group {
display: grid;
gap: var(--dui-radio-group-gap, var(--dui-space-3));
&[data-orientation='horizontal'] {
display: flex;
flex-wrap: wrap;
}
}
</style>
<script setup lang="ts">
import { injectRadioGroup } from './context.js';
const { value, disabled = false } = defineProps<{ value: string; disabled?: boolean }>();
const group = injectRadioGroup();
</script>
<template>
<input
type="radio"
class="item"
:name="group.name.value"
:value="value"
:checked="group.value.value === value"
:disabled="group.disabled.value || disabled"
@change="group.value.value = value"
/>
</template>
<!--
Options you can set from a parent or on the radio group item itself:
--dui-radio-size, --dui-radio-border, --dui-radio-bg, --dui-radio-checked-fg, --dui-radio-checked-bg
-->
<style scoped>
.item {
/* The size is reused below for the mark, so it is set once here. */
--size: var(--dui-radio-size, 1rem);
appearance: none;
display: inline-grid;
place-content: center;
flex-shrink: 0;
width: var(--size);
height: var(--size);
margin: 0;
border: 1px solid var(--dui-radio-border, var(--dui-color-input));
border-radius: var(--dui-radius-full);
background: var(--dui-radio-bg, var(--dui-color-control));
box-shadow: var(--dui-shadow-xs);
cursor: pointer;
outline: none;
transition: box-shadow var(--dui-duration-fast) var(--dui-ease-out);
/* The dot. */
&::before {
content: '';
width: calc(var(--size) / 2);
height: calc(var(--size) / 2);
border-radius: var(--dui-radius-full);
background: var(--dui-radio-checked-fg, var(--dui-color-primary-foreground));
scale: 0;
}
&:checked {
border-color: var(--dui-radio-checked-bg, var(--dui-color-primary));
background: var(--dui-radio-checked-bg, var(--dui-color-primary));
&::before {
scale: 1;
}
}
&:focus-visible {
border-color: var(--dui-color-ring);
box-shadow: 0 0 0 var(--dui-ring-width)
color-mix(in oklch, var(--dui-color-ring) 50%, transparent);
}
&[aria-invalid='true'] {
border-color: var(--dui-color-destructive);
box-shadow: 0 0 0 var(--dui-ring-width)
color-mix(in oklch, var(--dui-color-destructive) 20%, transparent);
}
&:disabled {
cursor: not-allowed;
opacity: 0.5;
}
}
</style>
import { inject, provide, type Ref } from 'vue';
// The group's shared state. Items are native radio inputs that share one `name`, so the
// browser handles arrow keys and tab order; the context only keeps `value` in sync.
type RadioGroupContext = {
name: Readonly<Ref<string>>;
value: Ref<string>;
disabled: Readonly<Ref<boolean>>;
};
const key = Symbol('distill-radio-group');
export const provideRadioGroup = (ctx: RadioGroupContext) => provide(key, ctx);
export const injectRadioGroup = () => inject<RadioGroupContext>(key)!;
export { default as Root } from './RadioGroup.vue';
export { default as Item } from './RadioGroupItem.vue';
Customizing
Set any of these on the component or on a parent element. See Customizing for how they work.
| Custom property | Default |
|---|---|
--dui-radio-size | 1rem |
--dui-radio-border | var(--dui-color-input) |
--dui-radio-bg | var(--dui-color-control) |
--dui-radio-checked-fg | var(--dui-color-primary-foreground) |
--dui-radio-checked-bg | var(--dui-color-primary) |
--dui-radio-group-gap | var(--dui-space-3) |