Toggle Group for Astro
Astro toggle buttons that share one selection or several selections.
Installation
bunx --bun shadcn@latest add @gnostic/toggle-groupnpx shadcn@latest add @gnostic/toggle-grouppnpm dlx shadcn@latest add @gnostic/toggle-groupInstall component packages
bun add @data-slot/toggle-groupnpm install @data-slot/toggle-grouppnpm add @data-slot/toggle-groupCopy the files
src/components/ui/toggle-group/index.tssrc/components/ui/toggle-group/toggle-group-item.astrosrc/components/ui/toggle-group/toggle-group-separator.astrosrc/components/ui/toggle-group/toggle-group-variants.tssrc/components/ui/toggle-group/toggle-group.astrosrc/components/ui/toggle-group/types.ts
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
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.
ToggleVariantProps['variant']"default"size?ToggleVariantProps['size']
Height and padding for every item that does not set its own `size`.
ToggleVariantProps['size']"default"orientation?'horizontal' | 'vertical'
Direction of the items and of the arrow keys.
'horizontal' | 'vertical'"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.
string | string[]multiple?boolean
Allow more than one pressed item.
booleanfalseloop?boolean
Move focus from the last item to the first item, and the reverse.
booleantruedisabled?boolean
Block changes to every item.
booleanfalseDiv attributes such as class, id, and aria-label pass through.
ToggleGroupItemProps
valuestring
Value stored for this item. The group matches this value against `defaultValue`. Use a value with no spaces.
stringvariant?ToggleVariantProps['variant']
Visual style for this item. When omitted, the item uses the group's `variant`.
ToggleVariantProps['variant']size?ToggleVariantProps['size']
Control size for this item. When omitted, the item uses the group's `size`.
ToggleVariantProps['size']disabled?boolean
Block changes to this item.
booleanfalseButton attributes such as class and aria-label pass through. Give an icon-only item an accessible name.
ToggleGroupSeparatorProps
orientation?'horizontal' | 'vertical'
Direction of the divider line. Use `vertical` between items in a horizontal group. Use `horizontal` between items in a vertical group.
'horizontal' | 'vertical'"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.