Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Toggle Group for Astro

Astro toggle buttons that share one selection or several selections.

API Reference

Installation

bunx --bun shadcn@latest add @gnostic/toggle-group

Usage

---
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
---
<ToggleGroup
    variant="outline"
    defaultValue="center"
    aria-label="Text alignment"
  >
  <ToggleGroupItem
    value="left"
    aria-label="Align left"
  >
  <AlignLeftIcon />Left</ToggleGroupItem>
  <ToggleGroupItem
    value="center"
    aria-label="Align center"
  >
  <AlignCenterIcon />Center</ToggleGroupItem>
  <ToggleGroupItem
    value="right"
    aria-label="Align right"
    >
  <AlignRightIcon />Right</ToggleGroupItem>
</ToggleGroup>

Set multiple when more than one item can stay pressed. Pass several values as an array or as a space-separated string.

<ToggleGroup
  multiple
  defaultValue={["bold", "italic"]}
  aria-label="Text formatting"
  >
  <ToggleGroupItem value="bold">Bold</ToggleGroupItem>
  <ToggleGroupItem value="italic">Italic</ToggleGroupItem>
  <ToggleGroupItem value="underline">Underline</ToggleGroupItem>
</ToggleGroup>

variant and size on the group apply to each item that does not set its own value. outline joins the items. The default variant leaves a small gap. Each item uses the Toggle styles.

Composition

Place the parts directly inside ToggleGroup:

ToggleGroup
├── ToggleGroupItem
├── ToggleGroupSeparator
└── ToggleGroupItem

ToggleGroupSeparator is optional. Use orientation="horizontal" on the separator when the group is vertical.

Examples

Several selections

Disabled

disabled on the group blocks every item. disabled on one item blocks that item.

Sizes

Vertical

API Reference

ToggleGroupProps

PropType
variant?ToggleVariantProps['variant']

Visual style for every item that does not set its own `variant`. `outline` joins the items into one control. The default style leaves a small gap.

TypeToggleVariantProps['variant']
Default"default"
size?ToggleVariantProps['size']

Height and padding for every item that does not set its own `size`.

TypeToggleVariantProps['size']
Default"default"
orientation?'horizontal' | 'vertical'

Direction of the items and of the arrow keys.

Type'horizontal' | 'vertical'
Default"horizontal"
defaultValue?string | string[]

Value selected at first render. Pass one value, a space-separated list, or an array. A value matches an item `value`. Use a value with no spaces. In single selection, the first known value is selected.

Typestring | string[]
multiple?boolean

Allow more than one pressed item.

Typeboolean
Defaultfalse
loop?boolean

Move focus from the last item to the first item, and the reverse.

Typeboolean
Defaulttrue
disabled?boolean

Block changes to every item.

Typeboolean
Defaultfalse

Div attributes such as class, id, and aria-label pass through.

ToggleGroupItemProps

PropType
valuestring

Value stored for this item. The group matches this value against `defaultValue`. Use a value with no spaces.

Typestring
variant?ToggleVariantProps['variant']

Visual style for this item. When omitted, the item uses the group's `variant`.

TypeToggleVariantProps['variant']
size?ToggleVariantProps['size']

Control size for this item. When omitted, the item uses the group's `size`.

TypeToggleVariantProps['size']
disabled?boolean

Block changes to this item.

Typeboolean
Defaultfalse

Button attributes such as class and aria-label pass through. Give an icon-only item an accessible name.

ToggleGroupSeparatorProps

PropType
orientation?'horizontal' | 'vertical'

Direction of the divider line. Use `vertical` between items in a horizontal group. Use `horizontal` between items in a vertical group.

Type'horizontal' | 'vertical'
Default"vertical"

Separator attributes such as class pass through.

See the data-slot docs for the controller, events, and keyboard model.

Events

Dispatch these events on the toggle group root. The root emits the outbound events. detail.value is an array of strings, including when the group allows one selection.

Outbound events

Event Detail
toggle-group:change { value: string[] }

Inbound events

Event Detail
toggle-group:set { value: string | string[] }

Read the installed source for the component source.

When to use Toggle Group

Use Toggle Group when the visitor keeps one option pressed, or several options pressed, and every option stays visible. Put the group label in aria-label when nearby text does not name the group. Use one item value with no spaces.

Keyboard and behavior

Tab moves to the active item. Arrow keys move focus along orientation. Home moves to the first item. End moves to the last item. Enter and Space press the focused item. Set loop={false} to stop focus at the ends.

One item stays pressed at a time unless multiple is set. Press the active item again to clear it. A disabled group and a disabled item ignore pointer and keyboard changes.

Theme and layout

Pressed, hover, focus, and disabled styles use the Toggle variants and the current theme. Check an icon-only item with an accessible name. In an outline group, keep item sizes equal so the shared border stays even. Color should reinforce the pressed state, not be its only indication.