Radio Group
Radio Group
RadioGroup.vue<script setup lang="ts">
import { RadioGroup, RadioGroupItem } from 'beui-vue'
import { ref, onBeforeUnmount, onMounted, shallowRef } from 'vue'
function usePreviewVariant(alternate: string) {
const pinned = undefined as string | undefined
const fixed = Boolean(pinned)
const variant = shallowRef(pinned === alternate ? alternate : 'default')
function sync() {
const requested = new URLSearchParams(location.search).get('variant')
variant.value = requested === alternate ? alternate : 'default'
}
function select(name: string) {
if (fixed) return
const url = new URL(location.href)
url.searchParams.set('variant', name)
history.replaceState(history.state, '', url)
sync()
}
onMounted(() => { if (!fixed) { sync(); window.addEventListener('popstate', sync) } })
onBeforeUnmount(() => window.removeEventListener('popstate', sync))
return { variant, select, fixed }
}
// `?variant=description` shows the second upstream preview.
const { variant } = usePreviewVariant('description')
const plan = ref('pro')
const schedule = ref('once')
</script>
<template>
<RadioGroup v-if="variant === 'description'" v-model="schedule" class="description">
<RadioGroupItem value="once" label="Run once" description="Runs a single time, right now." />
<RadioGroupItem value="schedule" label="Run on a schedule" description="Repeats on a set interval until stopped." />
<RadioGroupItem value="trigger" label="Run on a trigger" description="Waits for an event before it starts." />
<RadioGroupItem value="manual" label="Manual approval" description="Not available on your plan." disabled />
</RadioGroup>
<RadioGroup v-else v-model="plan" class="plans">
<RadioGroupItem value="starter" label="Starter, free" />
<RadioGroupItem value="pro" label="Pro, $12/mo" />
<RadioGroupItem value="team" label="Team, $29/mo" />
<RadioGroupItem value="legacy" label="Legacy plan" disabled />
</RadioGroup>
</template>
<style scoped>
.description { width: 100%; max-width: 16rem; }
.plans { min-width: 12rem; }
</style><script setup lang="ts">
import { computed, provide, ref, shallowRef, useId, type VNode } from 'vue'
import RadioGroupItem from './RadioGroupItem.vue'
import { radioKey } from './radio/context'
import type { ChoiceOption } from './types'
// `aria-label` and `aria-labelledby` fall through to the group element, and so
// does `class`.
const props = withDefaults(defineProps<{
/** Initial value when `v-model` is not used. */
defaultValue?: string
orientation?: 'vertical' | 'horizontal'
/** Shorthand for a list of options; use `RadioGroupItem` children for full control. */
options?: readonly ChoiceOption[]
name?: string
/** Accessible name for the group when no visible label names it. Same as `aria-label`. */
label?: string
/** Id of a visible element that names the group. Same as `aria-labelledby`. */
labelledby?: string
disabled?: boolean
required?: boolean
}>(), { orientation: 'vertical' })
defineSlots<{
/** `RadioGroupItem` children. */
default?: () => VNode[]
/** Label of an option in `options` mode. */
label?: (props: { option: ChoiceOption }) => VNode[]
}>()
const model = defineModel<string>()
const local = ref(props.defaultValue ?? '')
const current = computed(() => model.value ?? local.value)
const layoutId = useId()
function setValue(next: string) {
if (model.value === undefined) local.value = next
model.value = next
}
const registered = shallowRef<ReadonlyMap<string, boolean>>(new Map())
function register(value: string, disabled: boolean) {
if (registered.value.get(value) === disabled) return
registered.value = new Map(registered.value).set(value, disabled)
}
function unregister(value: string) {
if (!registered.value.has(value)) return
const next = new Map(registered.value)
next.delete(value)
registered.value = next
}
// Resolved during render so the group has exactly one tab stop in the same
// commit that changes the selection or the options.
const tabStop = computed<string | null>(() => {
if (registered.value.size === 0) return null
const enabled = Array.from(registered.value).filter(([, disabled]) => !disabled && !props.disabled)
return enabled.some(([value]) => value === current.value) ? current.value : (enabled[0]?.[0] ?? '')
})
provide(radioKey, {
value: current,
setValue,
layoutId,
disabled: computed(() => Boolean(props.disabled)),
tabStop,
options: registered,
register,
unregister,
})
</script>
<template>
<div class="be-radio-group" role="radiogroup" :aria-label="label" :aria-labelledby="labelledby" :data-orientation="orientation">
<input v-if="name" type="hidden" :name="name" :value="current" :required="required" :disabled="disabled">
<slot />
<template v-if="options">
<RadioGroupItem v-for="option in options" :key="option.value" :value="option.value" :label="option.label" :description="option.detail" :disabled="option.disabled">
<template v-if="$slots.label" #label><slot name="label" :option="option">{{ option.label }}</slot></template>
</RadioGroupItem>
</template>
</div>
</template>
<style scoped>
.be-radio-group { display: flex; gap: 0.75rem; }
.be-radio-group[data-orientation='vertical'] { flex-direction: column; }
.be-radio-group[data-orientation='horizontal'] { flex-flow: row wrap; }
</style>With Description
RadioGroup.vue<script setup lang="ts">
import { RadioGroup, RadioGroupItem } from 'beui-vue'
import { ref, onBeforeUnmount, onMounted, shallowRef } from 'vue'
function usePreviewVariant(alternate: string) {
const pinned = undefined as string | undefined
const fixed = Boolean(pinned)
const variant = shallowRef(pinned === alternate ? alternate : 'default')
function sync() {
const requested = new URLSearchParams(location.search).get('variant')
variant.value = requested === alternate ? alternate : 'default'
}
function select(name: string) {
if (fixed) return
const url = new URL(location.href)
url.searchParams.set('variant', name)
history.replaceState(history.state, '', url)
sync()
}
onMounted(() => { if (!fixed) { sync(); window.addEventListener('popstate', sync) } })
onBeforeUnmount(() => window.removeEventListener('popstate', sync))
return { variant, select, fixed }
}
// `?variant=description` shows the second upstream preview.
const { variant } = usePreviewVariant('description')
const plan = ref('pro')
const schedule = ref('once')
</script>
<template>
<RadioGroup v-if="variant === 'description'" v-model="schedule" class="description">
<RadioGroupItem value="once" label="Run once" description="Runs a single time, right now." />
<RadioGroupItem value="schedule" label="Run on a schedule" description="Repeats on a set interval until stopped." />
<RadioGroupItem value="trigger" label="Run on a trigger" description="Waits for an event before it starts." />
<RadioGroupItem value="manual" label="Manual approval" description="Not available on your plan." disabled />
</RadioGroup>
<RadioGroup v-else v-model="plan" class="plans">
<RadioGroupItem value="starter" label="Starter, free" />
<RadioGroupItem value="pro" label="Pro, $12/mo" />
<RadioGroupItem value="team" label="Team, $29/mo" />
<RadioGroupItem value="legacy" label="Legacy plan" disabled />
</RadioGroup>
</template>
<style scoped>
.description { width: 100%; max-width: 16rem; }
.plans { min-width: 12rem; }
</style><script setup lang="ts">
import { computed, provide, ref, shallowRef, useId, type VNode } from 'vue'
import RadioGroupItem from './RadioGroupItem.vue'
import { radioKey } from './radio/context'
import type { ChoiceOption } from './types'
// `aria-label` and `aria-labelledby` fall through to the group element, and so
// does `class`.
const props = withDefaults(defineProps<{
/** Initial value when `v-model` is not used. */
defaultValue?: string
orientation?: 'vertical' | 'horizontal'
/** Shorthand for a list of options; use `RadioGroupItem` children for full control. */
options?: readonly ChoiceOption[]
name?: string
/** Accessible name for the group when no visible label names it. Same as `aria-label`. */
label?: string
/** Id of a visible element that names the group. Same as `aria-labelledby`. */
labelledby?: string
disabled?: boolean
required?: boolean
}>(), { orientation: 'vertical' })
defineSlots<{
/** `RadioGroupItem` children. */
default?: () => VNode[]
/** Label of an option in `options` mode. */
label?: (props: { option: ChoiceOption }) => VNode[]
}>()
const model = defineModel<string>()
const local = ref(props.defaultValue ?? '')
const current = computed(() => model.value ?? local.value)
const layoutId = useId()
function setValue(next: string) {
if (model.value === undefined) local.value = next
model.value = next
}
const registered = shallowRef<ReadonlyMap<string, boolean>>(new Map())
function register(value: string, disabled: boolean) {
if (registered.value.get(value) === disabled) return
registered.value = new Map(registered.value).set(value, disabled)
}
function unregister(value: string) {
if (!registered.value.has(value)) return
const next = new Map(registered.value)
next.delete(value)
registered.value = next
}
// Resolved during render so the group has exactly one tab stop in the same
// commit that changes the selection or the options.
const tabStop = computed<string | null>(() => {
if (registered.value.size === 0) return null
const enabled = Array.from(registered.value).filter(([, disabled]) => !disabled && !props.disabled)
return enabled.some(([value]) => value === current.value) ? current.value : (enabled[0]?.[0] ?? '')
})
provide(radioKey, {
value: current,
setValue,
layoutId,
disabled: computed(() => Boolean(props.disabled)),
tabStop,
options: registered,
register,
unregister,
})
</script>
<template>
<div class="be-radio-group" role="radiogroup" :aria-label="label" :aria-labelledby="labelledby" :data-orientation="orientation">
<input v-if="name" type="hidden" :name="name" :value="current" :required="required" :disabled="disabled">
<slot />
<template v-if="options">
<RadioGroupItem v-for="option in options" :key="option.value" :value="option.value" :label="option.label" :description="option.detail" :disabled="option.disabled">
<template v-if="$slots.label" #label><slot name="label" :option="option">{{ option.label }}</slot></template>
</RadioGroupItem>
</template>
</div>
</template>
<style scoped>
.be-radio-group { display: flex; gap: 0.75rem; }
.be-radio-group[data-orientation='vertical'] { flex-direction: column; }
.be-radio-group[data-orientation='horizontal'] { flex-flow: row wrap; }
</style>API Reference
RadioGroup
v-model?string—defaultValue?stringInitial value when `v-model` is not used.
—class?string—orientation?"vertical" | "horizontal"verticalaria-label?stringAccessible name for the group when no visible label names it.
—aria-labelledby?stringId of a visible element that names the group.
—options?readonly ChoiceOption[]Shorthand for a list of options; use `RadioGroupItem` children for full control.
—name?string—label?stringAccessible name for the group when no visible label names it. Same as `aria-label`.
—labelledby?stringId of a visible element that names the group. Same as `aria-labelledby`.
—disabled?boolean—required?boolean—#default?slot`RadioGroupItem` children.
—#label?slotLabel of an option in `options` mode.
—RadioGroupItem
valuestring—label?string—description?stringSupporting text under the label, announced as the description rather than the name.
—disabled?boolean—class?string—id?stringId of the radio button.
—#label?slotRich label; takes over from the `label` prop.
—Install
Add it with your package manager, or copy the source manually.
Run it in a project set up with shadcn-vue. The source is copied to components/beui and its dependencies are added to your package.json. Read the shadcn-vue guide
$ bunx --bun shadcn-vue@latest add @beui-vue/radio$ bun add beui-vueDownload the release archive to your project root, then install the local file.
$ bun add ./beui-vue-0.1.0.tgzImport the shared styles once in your application.
import RadioGroup from '@/components/beui/controls/RadioGroup.vue'
import '@/components/beui/styles.css'Related components
Checkbox
Form choice control with a draw-on checkmark, spring press feedback and indeterminate state support.
Select
Composable select primitives whose panel unfolds out of the trigger on one spring and pulls away into its own pill, plus a Morph variant where the trigger grows into the panel via shared layout.
Input
Text input with label, left/right icons, optional stable error row, error shake and success check draw.
Updated