Components
Toggle Group
A set of toggle buttons where one or many can be active.
Installation
$ npx veil-ui@latest add toggle-groupUsage
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
<ToggleGroup type="single" defaultValue="left" aria-label="Text alignment">
<ToggleGroupItem value="left" aria-label="Align left">
Left
</ToggleGroupItem>
<ToggleGroupItem value="right" aria-label="Align right">
Right
</ToggleGroupItem>
</ToggleGroup>;Examples
Single selection
Multiple selection
Outline
Sizes
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| type | "single" | "multiple" | — | Whether one or many items can be active. |
| value | string | string[] | — | Controlled active value(s). |
| defaultValue | string | string[] | — | Initial active value(s) when uncontrolled. |
| onValueChange | (value: string | string[]) => void | — | Called when the active value(s) change. |
| variant | "default" | "outline" | "default" | Shared style for every item in the group. |
| size | "sm" | "default" | "lg" | "default" | Shared size for every item in the group. |
| aria-label | string | — | Names the group for assistive technology. |
Accessibility
- Single mode renders a
radiogroup; items exposerole="radio"and arrow-key navigation. - Multiple mode renders a labeled group of toggle buttons with
aria-pressed. - Always provide an
aria-labeloraria-labelledbyon the group.
Vue and plain HTML
This component also ships for Vue (Reka UI) and plain HTML. Install it with npx veil-ui add toggle-group --framework vue or switch the framework in components.json.
<script setup lang="ts">
import { ToggleGroupRoot } from "reka-ui";
import { provide, toRef } from "vue";
import { cn } from "@/lib/utils";
import {
toggleGroupContextKey,
type ToggleGroupSize,
type ToggleGroupVariant,
} from "./context";
const props = withDefaults(
defineProps<{
type?: "single" | "multiple";
variant?: ToggleGroupVariant;
size?: ToggleGroupSize;
defaultValue?: string | string[];
disabled?: boolean;
rovingFocus?: boolean;
orientation?: "horizontal" | "vertical";
loop?: boolean;
class?: string;
}>(),
{ type: "single", disabled: false },
);
const model = defineModel<string | string[]>();
provide(toggleGroupContextKey, {
variant: toRef(props, "variant"),
size: toRef(props, "size"),
});
</script>
<template>
<ToggleGroupRoot
v-model="model"
data-slot="toggle-group"
:data-variant="variant ?? 'default'"
:data-size="size ?? 'default'"
:type="type"
:default-value="defaultValue"
:disabled="disabled"
:roving-focus="rovingFocus"
:orientation="orientation"
:loop="loop"
:class="
cn(
'inline-flex w-fit items-center rounded-md',
'[&>*]:rounded-none [&>*:first-child]:rounded-l-md [&>*:last-child]:rounded-r-md [&>*:not(:first-child)]:-ml-px',
props.class,
)
"
>
<slot />
</ToggleGroupRoot>
</template>