Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Toggle Group for Vue UI SDK

Toggle buttons grouped into one toolbar control for Vue on UI SDK, with one pressed at a time or several, and arrow keys between them.

On this page9 sections

A toggle group is a row of toggle buttons that act as one control: text alignment, where one is pressed at a time, or bold, italic and underline, where several can be. It belongs in a toolbar or above a list, where a form control would look out of place.

For a required choice in a form, with a visible label on every option, use a Radio Group. For one button on its own that stays pressed, use a Toggle.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Toggle Group anatomy: root, item

Usage

<script setup lang="ts">
import { BoldIcon, ItalicIcon, UnderlineIcon } from "lucide-vue-next";
import {
  ToggleGroup, ToggleGroupItem,
} from "@/components/ui/toggle-group";
</script>
<template>
  <ToggleGroup aria-label="Text formatting" multiple variant="outline">
    <ToggleGroupItem aria-label="Bold" value="bold">
      <BoldIcon aria-hidden="true" />
    </ToggleGroupItem>
    <ToggleGroupItem aria-label="Italic" value="italic">
      <ItalicIcon aria-hidden="true" />
    </ToggleGroupItem>
    <ToggleGroupItem aria-label="Underline" value="underline">
      <UnderlineIcon aria-hidden="true" />
    </ToggleGroupItem>
  </ToggleGroup>
</template>

Each item's value is what the group reports as pressed. variant and size set on the group apply to every item in it.

Accessibility

The group has the group role, and every item is a button with aria-pressed, whether one or several can be pressed. The group is one stop in the tab order, and the arrow keys follow orientation: Left and Right in a horizontal group, Up and Down in a vertical one, wrapping at the ends. Give an icon-only item an aria-label, and give the group one too.

Tab
Moves focus into the group, to the first pressed item or else the first one, and later to the item that last had focus. The next Tab leaves the group.
Space
Presses or releases the focused item.
Enter
Presses or releases the focused item.
Arrow Right
In a horizontal group, moves focus to the next item, and from the last one back to the first. In right-to-left text it goes the other way.
Arrow Left
In a horizontal group, moves focus to the previous item, and from the first one to the last. In right-to-left text it goes the other way.
Arrow Down
In a vertical group, moves focus to the next item, and from the last one back to the first.
Arrow Up
In a vertical group, moves focus to the previous item, and from the first one to the last.
Home
Moves focus to the first item.
End
Moves focus to the last item.

Demos

Outline

A two-button filter, All and Missed, like the one above a call log, with variant set to outline and All pressed to start. Only one can be pressed at a time, and pressing it again releases it, leaving neither. Use the outline when the group sits on a plain surface and needs an edge to read as one control.

Preview width

Sizes

Two rows of the same four positions, Top, Bottom, Left and Right, with Top pressed in each. The first row sets size to sm on the group and the second leaves it at the default md, and every item follows its group. Use the small row in a crowded toolbar, and the default where the group is the main control on the screen.

Preview width

Spacing

The four positions again, small and outlined, with spacing set to 0, so the buttons join into one segmented bar: they share their borders, and only the two ends keep rounded corners. Any other number is the gap between items in Tailwind spacing steps, and 2, the default, is what the Sizes rows use. Join them for a single choice such as text alignment.

Preview width

Vertical

Bold, italic and underline stacked in a column, with orientation set to vertical, a spacing of 1, and bold and italic pressed to start. Several can be on at once, the way text styles combine. Use a column for a narrow tool palette beside a canvas, where Up and Down move between the items.

Preview width

Disabled

The same three formatting buttons with disabled on the group, so every item fades and ignores clicks. Use it while formatting can't apply, such as in a read-only document, so the toolbar keeps its layout. To retire one action only, disable that item and leave the rest working.

Preview width

Font weight selector

Four tiles, Light, Normal, Medium and Bold, each showing Aa in its weight, with a line under them naming the Tailwind class for the pressed one. The group is controlled, with size set to lg, and its change handler falls back to Normal when the pressed tile is clicked again, so a weight is always chosen. Use tiles like these when seeing the option beats reading its name.

Preview width

RTL

A view switcher, list, grid and cards, labeled in Arabic, with list pressed and dir set to rtl. The first item sits at the right end of the row, and the outlined buttons follow it. Set the direction on the group or a wrapper when the toolbar sits in a page that otherwise reads left to right.

Preview width

FAQ

How do I allow one or several selections in a Toggle Group in Vue?

One pressed item at a time is the default, and pressing the pressed item again releases it. Add multiple to ToggleGroup to let several be pressed together, as the Vertical demo does for bold, italic and underline.

Use one at a time for choices that exclude each other, like alignment, and several for styles that combine.

How do I control the value of a Toggle Group?

Bind v-model on ToggleGroup to a ref holding an array of the pressed values. It's an array even when only one item can be pressed, so a single group's choice is its first entry. Listen to @value-change when you need the details object.

The Font weight selector demo is controlled this way.

How do I keep at least one item pressed in a Toggle Group?

Set deselectable to false on a single-choice group, and pressing the pressed item leaves it pressed.

The Font weight selector demo handles it in its change handler, falling back to Normal when the pressed tile is clicked. That suits a setting where some value must always apply, like a font weight or a view mode.

How do I join Toggle Group items into one segmented control?

Set spacing to 0 on ToggleGroup. With variant set to outline, the items then share their borders and read as one bar, the way an alignment control in a text editor does.

The Spacing demo shows the joined bar. Any other number is the gap between the items in Tailwind spacing steps, and 2 is the default. variant and size on the group apply to every item, so you don't repeat them.

Is the Toggle Group accessible with the keyboard?

Yes. The group is one Tab stop, the arrow keys along its orientation move between items and wrap at the ends, Home and End jump to the first and last, and Space or Enter presses the focused one.

The Accessibility section lists each key. In a vertical group, use Up and Down. Left and Right do nothing there.

How do I label an icon-only Toggle Group?

Give every item an aria-label that names the action, such as Bold or Align left, and mark its icon aria-hidden="true". Give the group a name as well, with an aria-label such as Text formatting, so a screen reader says what the set of buttons is for.

Sighted people don't see any of that, so add a Tooltip to each item when the icons aren't standard. Bold and italic are, while a custom layout icon usually isn't.

Does a Toggle Group submit its value with a form?

No. UI SDK renders the items as plain buttons with no form input behind them, so a form never sees which ones are pressed.

Keep the value in state and send it yourself, or render a hidden input whose value you update from the change callback. For a choice that belongs in a form, a Radio Group or a set of Checkbox fields submits natively and reads as a form question.

Should I use a Toggle Group or a Radio Group?

Use a toggle group for toolbar buttons that change what's on screen right away, like text alignment, formatting or a list and grid switch. Use a Radio Group for a choice in a form, where each option needs a visible label and the answer is submitted.

UI SDK announces each item as a pressed or unpressed button, even in a single-choice group, so a screen reader doesn't present it as one question with one answer.

API Reference

ToggleGroup

<div> data-slot="toggle-group"

PropType
value?T[]

The pressed toggles' values. Pass it to control the group. Two-way with v-model.

defaultValue?T[]

The toggles pressed at first, when uncontrolled.

Default
[]
controller?{ /** The pressed toggles' values, in the order they appear. */ getValue: () → string[]; /** Sets the pressed toggles. Only the first counts unless `multiple` is set. Returns false when canceled. */ setValue: (value: string[], options?: ToggleGroupChangeOptions) → boolean; /** Whether a toggle is pressed. */ isPressed: (value: string) → boolean; /** Presses a toggle, or unpresses a pressed one, as a user would. Disabled toggles and `deselectable={false}` refuse it. */ toggle: (value: string, options?: ToggleGroupChangeOptions) → boolean; /** Moves focus to a toggle, or to the one that holds the group's tab stop. */ focus: (value?: string) → void; }

Presses toggles from anywhere. Create one with createToggleGroupController().

Type
{ /** The pressed toggles' values, in the order they appear. */ getValue: () → string[]; /** Sets the pressed toggles. Only the first counts unless `multiple` is set. Returns false when canceled. */ setValue: (value: string[], options?: ToggleGroupChangeOptions) → boolean; /** Whether a toggle is pressed. */ isPressed: (value: string) → boolean; /** Presses a toggle, or unpresses a pressed one, as a user would. Disabled toggles and `deselectable={false}` refuse it. */ toggle: (value: string, options?: ToggleGroupChangeOptions) → boolean; /** Moves focus to a toggle, or to the one that holds the group's tab stop. */ focus: (value?: string) → void; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

deselectable?boolean

Whether the pressed toggle can be unpressed when only one may be pressed. With false, one toggle stays pressed once any is. Ignored with multiple.

Default
true
disabled?boolean

Stops every toggle from being pressed. They stay focusable with aria-disabled, so the group keeps its tab stop and stays discoverable.

Default
false
loop?boolean

Whether arrow keys wrap from the last toggle to the first. UI SDK's default: true.

Default
loopFocus
loopFocus?boolean

The other bases' name for loop.

Default
true
multiple?boolean

Whether several toggles can be pressed at once. Otherwise pressing one unpresses the others.

Default
false
orientation?"horizontal" | "vertical"

Horizontal or vertical: which arrow keys move between items.

Type
"horizontal" | "vertical"
Default
"horizontal"
spacing?number

The gap between items, in spacing units. At 0 they join into one bar.

Default
2
style?StyleValue
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

EventType
@value-change(value: T[], details: ChangeDetails<ToggleGroupValueChangeReason>) → void

Runs when a toggle wants to be pressed or unpressed. details.reason says why, and details.cancel() stops it.

Type
(value: T[], details: ChangeDetails<ToggleGroupValueChangeReason>) → void
Parameters

value – T[]

details – ChangeDetails<ToggleGroupValueChangeReason>

Returns
void
AttributeType
data-disabled?string

Present when the whole group is disabled.

data-has-icons?string

Present while any toggle inside holds an icon part, so toggles without one can line up.

data-orientation"horizontal" | "vertical"

Which arrow keys move between toggles.

Type
"horizontal" | "vertical"
CSS variableType
--gapstring

ToggleGroupItem

PropType
pressed?boolean

Whether it's pressed. Pass it to control the toggle. Inside a group, the group's value decides. Two-way with v-model or v-model:pressed.

defaultPressed?boolean

Whether it's pressed at first, when uncontrolled.

Default
false
controller?{ isPressed: () → boolean; setPressed: (pressed: boolean, options?: ToggleChangeOptions) → boolean; toggle: (options?: ToggleChangeOptions) → boolean; focus: () → void; }

Presses it from anywhere. Create one with createToggleController().

Type
{ isPressed: () → boolean; setPressed: (pressed: boolean, options?: ToggleChangeOptions) → boolean; toggle: (options?: ToggleChangeOptions) → boolean; focus: () → void; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops presses. Alone, the toggle is natively disabled, unless focusableWhenDisabled is set. Inside a group or a toolbar, it stays focusable with aria-disabled, so arrow keys still reach it.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled toggle in the tab order with aria-disabled instead of the native attribute. Toggles in a group or a toolbar always are.

Default
false
value?string

Identifies the toggle inside a group, whose value lists the pressed toggles' values. Defaults to a generated id.

#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

EventType
@pressed-change(pressed: boolean, details: ChangeDetails<TogglePressedChangeReason>) → void

Runs when a press wants to change it. details.reason says why, and details.cancel() stops it. Inside a group, it runs before the group's onValueChange, and canceling it cancels the group's change too.

Type
(pressed: boolean, details: ChangeDetails<TogglePressedChangeReason>) → void
Parameters

pressed – boolean

details – ChangeDetails<TogglePressedChangeReason>

Returns
void
AttributeType
data-size?string

Mirrors resolvedSize.

data-spacing?string

Mirrors spacing.

data-variant?string

Mirrors resolvedVariant.