RadioGroup
Usage
Use the v-model
directive to control the value of the RadioGroup or the default-value
prop to set the initial value when you do not need to control its state.
Items
Use the items
prop as an array of strings, numbers or booleans:
<script setup lang="ts">
const items = ref(['System', 'Light', 'Dark'])
const value = ref('System')
</script>
<template>
<URadioGroup v-model="value" :items="items" />
</template>
You can also pass an array of objects with the following properties:
label?: string
description?: string
value?: string
disabled?: boolean
<script setup lang="ts">
const items = ref([
{
label: 'System',
description: 'This is the first option.',
value: 'system'
},
{
label: 'Light',
description: 'This is the second option.',
value: 'light'
},
{
label: 'Dark',
description: 'This is the third option.',
value: 'dark'
}
])
const value = ref('system')
</script>
<template>
<URadioGroup v-model="value" :items="items" />
</template>
value
property of the object in the v-model
directive or the default-value
prop.Value Key
You can change the property that is used to set the value by using the value-key
prop. Defaults to value
.
<script setup lang="ts">
const items = ref([
{
label: 'System',
description: 'This is the first option.',
id: 'system'
},
{
label: 'Light',
description: 'This is the second option.',
id: 'light'
},
{
label: 'Dark',
description: 'This is the third option.',
id: 'dark'
}
])
const value = ref('light')
</script>
<template>
<URadioGroup v-model="value" value-key="id" :items="items" />
</template>
Variant
Use the variant
prop to change the variant of the RadioGroup.
<script setup lang="ts">
const items = ref([
{
label: 'Pro',
value: 'pro',
description: 'Tailored for indie hackers, freelancers and solo founders.'
},
{
label: 'Startup',
value: 'startup',
description: 'Best suited for small teams, startups and agencies.'
},
{
label: 'Enterprise',
value: 'enterprise',
description: 'Ideal for larger teams and organizations.'
}
])
</script>
<template>
<URadioGroup variant="table" default-value="pro" :items="items" />
</template>
Legend
Use the legend
prop to set the legend of the RadioGroup.
<script setup lang="ts">
const items = ref(['System', 'Light', 'Dark'])
</script>
<template>
<URadioGroup legend="Theme" default-value="System" :items="items" />
</template>
Orientation
Use the orientation
prop to change the orientation of the RadioGroup. Defaults to vertical
.
<script setup lang="ts">
const items = ref(['System', 'Light', 'Dark'])
</script>
<template>
<URadioGroup orientation="horizontal" default-value="System" :items="items" />
</template>
Color
Use the color
prop to change the color of the RadioGroup.
<script setup lang="ts">
const items = ref(['System', 'Light', 'Dark'])
</script>
<template>
<URadioGroup color="neutral" default-value="System" :items="items" />
</template>
Size
Use the size
prop to change the size of the RadioGroup.
<script setup lang="ts">
const items = ref(['System', 'Light', 'Dark'])
</script>
<template>
<URadioGroup size="xl" default-value="System" :items="items" />
</template>
Disabled
Use the disabled
prop to disable the RadioGroup.
<script setup lang="ts">
const items = ref(['System', 'Light', 'Dark'])
</script>
<template>
<URadioGroup disabled default-value="System" :items="items" />
</template>
API
Props
Prop | Default | Type |
---|---|---|
as |
|
The element or component this component should render as. |
modelValue |
The controlled value of the radio item to check. Can be binded as | |
legend |
| |
valueKey |
|
When |
labelKey |
|
When |
descriptionKey |
|
When |
items |
| |
size |
|
|
color |
|
|
orientation |
|
The orientation the radio buttons are laid out. |
variant |
|
|
defaultValue |
The value of the radio item that should be checked when initially rendered. Use when you do not need to control the state of the radio items. | |
disabled |
When | |
name |
The name of the field. Submitted with its owning form as part of a name/value pair. | |
loop |
When | |
required |
When | |
ui |
|
Slots
Slot | Type |
---|---|
legend |
|
label |
|
description |
|
Emits
Event | Type |
---|---|
change |
|
update:modelValue |
|
update:modelValue |
|
Theme
export default defineAppConfig({
ui: {
radioGroup: {
slots: {
root: 'relative',
fieldset: '',
legend: 'mb-1 block font-medium text-[var(--ui-text)]',
item: 'flex items-start',
itemWrapper: 'flex',
base: 'rounded-full ring ring-inset ring-[var(--ui-border-accented)] focus-visible:outline-2 focus-visible:outline-offset-2',
indicator: 'flex items-center justify-center size-full rounded-full after:bg-[var(--ui-bg)] after:rounded-full',
container: 'flex items-center',
wrapper: 'ms-2',
label: 'block font-medium text-[var(--ui-text)]',
description: 'text-[var(--ui-text-muted)]'
},
variants: {
color: {
primary: {
base: 'focus-visible:outline-[var(--ui-primary)]',
indicator: 'bg-[var(--ui-primary)]'
},
secondary: {
base: 'focus-visible:outline-[var(--ui-secondary)]',
indicator: 'bg-[var(--ui-secondary)]'
},
success: {
base: 'focus-visible:outline-[var(--ui-success)]',
indicator: 'bg-[var(--ui-success)]'
},
info: {
base: 'focus-visible:outline-[var(--ui-info)]',
indicator: 'bg-[var(--ui-info)]'
},
warning: {
base: 'focus-visible:outline-[var(--ui-warning)]',
indicator: 'bg-[var(--ui-warning)]'
},
error: {
base: 'focus-visible:outline-[var(--ui-error)]',
indicator: 'bg-[var(--ui-error)]'
},
neutral: {
base: 'focus-visible:outline-[var(--ui-border-inverted)]',
indicator: 'bg-[var(--ui-bg-inverted)]'
}
},
variant: {
radio: {},
card: {
base: 'ml-4',
item: 'flex-row-reverse items-center justify-between border-2 border-[var(--ui-border-muted)] rounded-lg'
},
table: {
item: 'border-[var(--ui-border-muted)]'
}
},
orientation: {
horizontal: {
itemWrapper: 'flex-row',
wrapper: 'me-2'
},
vertical: {
itemWrapper: 'flex-col'
}
},
size: {
xs: {
fieldset: 'gap-0.5',
legend: 'text-xs',
base: 'size-3',
item: 'text-xs',
container: 'h-4',
indicator: 'after:size-1'
},
sm: {
fieldset: 'gap-0.5',
legend: 'text-xs',
base: 'size-3.5',
item: 'text-xs',
container: 'h-4',
indicator: 'after:size-1'
},
md: {
fieldset: 'gap-1',
legend: 'text-sm',
base: 'size-4',
item: 'text-sm',
container: 'h-5',
indicator: 'after:size-1.5'
},
lg: {
fieldset: 'gap-1',
legend: 'text-sm',
base: 'size-4.5',
item: 'text-sm',
container: 'h-5',
indicator: 'after:size-1.5'
},
xl: {
fieldset: 'gap-1.5',
legend: 'text-base',
base: 'size-5',
item: 'text-base',
container: 'h-6',
indicator: 'after:size-2'
}
},
disabled: {
true: {
base: 'cursor-not-allowed opacity-75',
label: 'cursor-not-allowed opacity-75'
}
},
required: {
true: {
legend: "after:content-['*'] after:ms-0.5 after:text-[var(--ui-error)]"
}
}
},
compoundVariants: [
{
size: 'xs',
variant: 'card',
class: {
item: 'p-2.5',
itemWrapper: 'gap-2'
}
},
{
size: 'sm',
variant: 'card',
class: {
item: 'p-3',
itemWrapper: 'gap-2.5'
}
},
{
size: 'md',
variant: 'card',
class: {
item: 'p-3.5',
itemWrapper: 'gap-2.5'
}
},
{
size: 'lg',
variant: 'card',
class: {
item: 'p-4',
itemWrapper: 'gap-3.5'
}
},
{
size: 'xl',
variant: 'card',
class: {
item: 'p-4.5',
itemWrapper: 'gap-3.5'
}
},
{
size: 'xs',
variant: 'table',
class: {
item: 'p-2.5',
itemWrapper: 'gap-0'
}
},
{
size: 'sm',
variant: 'table',
class: {
item: 'p-3',
itemWrapper: 'gap-0'
}
},
{
size: 'md',
variant: 'table',
class: {
item: 'p-3.5',
itemWrapper: 'gap-0'
}
},
{
size: 'lg',
variant: 'table',
class: {
item: 'p-4',
itemWrapper: 'gap-0'
}
},
{
size: 'xl',
variant: 'table',
class: {
item: 'p-4.5',
itemWrapper: 'gap-0'
}
},
{
orientation: 'horizontal',
variant: 'table',
class: {
item: 'first:rounded-l-lg last:rounded-r-lg not-first:not-last:border-x-2 first:border-l-2 border-y-2 last:border-r-2'
}
},
{
orientation: 'vertical',
variant: 'table',
class: {
item: 'first:rounded-t-lg last:rounded-b-lg not-first:not-last:border-y-2 first:border-t-2 border-x-2 last:border-b-2'
}
},
{
color: 'primary',
variant: 'card',
class: {
item: 'data-[checked=true]:border-[var(--ui-primary)]'
}
},
{
color: 'neutral',
variant: 'card',
class: {
item: 'data-[checked=true]:border-[var(--ui-border-elevated)]'
}
},
{
color: 'primary',
variant: 'table',
class: {
item: 'data-[checked=true]:bg-[var(--ui-primary)]/20 data-[checked=true]:border-[var(--ui-primary)]/20'
}
}
],
defaultVariants: {
size: 'md',
color: 'primary',
variant: 'radio'
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
radioGroup: {
slots: {
root: 'relative',
fieldset: '',
legend: 'mb-1 block font-medium text-[var(--ui-text)]',
item: 'flex items-start',
itemWrapper: 'flex',
base: 'rounded-full ring ring-inset ring-[var(--ui-border-accented)] focus-visible:outline-2 focus-visible:outline-offset-2',
indicator: 'flex items-center justify-center size-full rounded-full after:bg-[var(--ui-bg)] after:rounded-full',
container: 'flex items-center',
wrapper: 'ms-2',
label: 'block font-medium text-[var(--ui-text)]',
description: 'text-[var(--ui-text-muted)]'
},
variants: {
color: {
primary: {
base: 'focus-visible:outline-[var(--ui-primary)]',
indicator: 'bg-[var(--ui-primary)]'
},
secondary: {
base: 'focus-visible:outline-[var(--ui-secondary)]',
indicator: 'bg-[var(--ui-secondary)]'
},
success: {
base: 'focus-visible:outline-[var(--ui-success)]',
indicator: 'bg-[var(--ui-success)]'
},
info: {
base: 'focus-visible:outline-[var(--ui-info)]',
indicator: 'bg-[var(--ui-info)]'
},
warning: {
base: 'focus-visible:outline-[var(--ui-warning)]',
indicator: 'bg-[var(--ui-warning)]'
},
error: {
base: 'focus-visible:outline-[var(--ui-error)]',
indicator: 'bg-[var(--ui-error)]'
},
neutral: {
base: 'focus-visible:outline-[var(--ui-border-inverted)]',
indicator: 'bg-[var(--ui-bg-inverted)]'
}
},
variant: {
radio: {},
card: {
base: 'ml-4',
item: 'flex-row-reverse items-center justify-between border-2 border-[var(--ui-border-muted)] rounded-lg'
},
table: {
item: 'border-[var(--ui-border-muted)]'
}
},
orientation: {
horizontal: {
itemWrapper: 'flex-row',
wrapper: 'me-2'
},
vertical: {
itemWrapper: 'flex-col'
}
},
size: {
xs: {
fieldset: 'gap-0.5',
legend: 'text-xs',
base: 'size-3',
item: 'text-xs',
container: 'h-4',
indicator: 'after:size-1'
},
sm: {
fieldset: 'gap-0.5',
legend: 'text-xs',
base: 'size-3.5',
item: 'text-xs',
container: 'h-4',
indicator: 'after:size-1'
},
md: {
fieldset: 'gap-1',
legend: 'text-sm',
base: 'size-4',
item: 'text-sm',
container: 'h-5',
indicator: 'after:size-1.5'
},
lg: {
fieldset: 'gap-1',
legend: 'text-sm',
base: 'size-4.5',
item: 'text-sm',
container: 'h-5',
indicator: 'after:size-1.5'
},
xl: {
fieldset: 'gap-1.5',
legend: 'text-base',
base: 'size-5',
item: 'text-base',
container: 'h-6',
indicator: 'after:size-2'
}
},
disabled: {
true: {
base: 'cursor-not-allowed opacity-75',
label: 'cursor-not-allowed opacity-75'
}
},
required: {
true: {
legend: "after:content-['*'] after:ms-0.5 after:text-[var(--ui-error)]"
}
}
},
compoundVariants: [
{
size: 'xs',
variant: 'card',
class: {
item: 'p-2.5',
itemWrapper: 'gap-2'
}
},
{
size: 'sm',
variant: 'card',
class: {
item: 'p-3',
itemWrapper: 'gap-2.5'
}
},
{
size: 'md',
variant: 'card',
class: {
item: 'p-3.5',
itemWrapper: 'gap-2.5'
}
},
{
size: 'lg',
variant: 'card',
class: {
item: 'p-4',
itemWrapper: 'gap-3.5'
}
},
{
size: 'xl',
variant: 'card',
class: {
item: 'p-4.5',
itemWrapper: 'gap-3.5'
}
},
{
size: 'xs',
variant: 'table',
class: {
item: 'p-2.5',
itemWrapper: 'gap-0'
}
},
{
size: 'sm',
variant: 'table',
class: {
item: 'p-3',
itemWrapper: 'gap-0'
}
},
{
size: 'md',
variant: 'table',
class: {
item: 'p-3.5',
itemWrapper: 'gap-0'
}
},
{
size: 'lg',
variant: 'table',
class: {
item: 'p-4',
itemWrapper: 'gap-0'
}
},
{
size: 'xl',
variant: 'table',
class: {
item: 'p-4.5',
itemWrapper: 'gap-0'
}
},
{
orientation: 'horizontal',
variant: 'table',
class: {
item: 'first:rounded-l-lg last:rounded-r-lg not-first:not-last:border-x-2 first:border-l-2 border-y-2 last:border-r-2'
}
},
{
orientation: 'vertical',
variant: 'table',
class: {
item: 'first:rounded-t-lg last:rounded-b-lg not-first:not-last:border-y-2 first:border-t-2 border-x-2 last:border-b-2'
}
},
{
color: 'primary',
variant: 'card',
class: {
item: 'data-[checked=true]:border-[var(--ui-primary)]'
}
},
{
color: 'neutral',
variant: 'card',
class: {
item: 'data-[checked=true]:border-[var(--ui-border-elevated)]'
}
},
{
color: 'primary',
variant: 'table',
class: {
item: 'data-[checked=true]:bg-[var(--ui-primary)]/20 data-[checked=true]:border-[var(--ui-primary)]/20'
}
}
],
defaultVariants: {
size: 'md',
color: 'primary',
variant: 'radio'
}
}
}
})
]
})