组件

Toggle Group

A row of toggles that belong together: pick one (alignment) or several (bold, italic, underline). Joined outline items share one hand-drawn box with a pencil seam between them; the highlighter body marks what is on.

安装

terminal
npx shadcn@latest add @wobko/toggle-group

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Pick one

type=single: one item is on, clicking it again clears it. Joined outline items share one box; the seam between two items is a short pencil stroke at the edge they share.

Pick several

type=multiple: any of the items can be on at once, and each one knows its own state. Icon items need names; hand-lettered items are just text in the script font. The sample line follows the group.

Joined, separate, vertical

Outline items that touch share one box. Give them spacing and each keeps its own. Borderless items only whisper their seams. Vertical groups stack and use the up and down arrows.

A view switcher

The group in a card header, driving what the card shows. Every item has a name; the current view is also written out in the description.

In a field

A group of days inside a Field keeps the width of its buttons, and `aria-invalid` (a Form's FormControl sets it) draws the one shared box in red pencil, like any invalid field. Pick a day and it turns back to ink.

Demo

Official `toggle-group-demo`: three outline toggles, any number of them on (`type="multiple"`). Joined outline items share one felt-tip box with a drawn seam between them. One label differs from the original, which calls the underline "strikethrough".

Disabled

Official `toggle-group-disabled`: `disabled` on the group disables every item. The items are drawn as dashed pencil gray, and none of them can be focused or toggled.

Large

Official `toggle-group-lg`: `size="lg"` on the group sizes every item. The default variant has no box, so the items are only seams and a highlighter body when on.

Outline

Official `toggle-group-outline`: `variant="outline"` with `type="multiple"`. The group draws one box around all its items, so the three read as a single control.

Small

Official `toggle-group-sm`: `size="sm"` with `type="single"`, so turning one on turns the others off. Arrow keys move between items and the group is one tab stop.

Single

Official `toggle-group-single`: `type="single"`, at most one item on; pressing the one that is on turns it off again, so the group can also hold nothing.

Spacing

Official `toggle-group-spacing`: `spacing={2}` pulls the items apart, so each keeps a box of its own. shadcn fills the icon with a color when an item is on; here each item keeps its highlighter body and writes its icon in that color instead (a drawn line has no fill to change).

API 参考

ToggleGroup

A Radix ToggleGroup (single: a radio group; multiple: a toolbar). With variant outline and spacing 0 the group is the ink host and draws the one box around every item; otherwise the items draw themselves. It passes variant, size, spacing and tone down to its items.

属性类型默认值说明
type"single" | "multiple"-single = at most one item is on (radio semantics); multiple = any number (aria-pressed buttons).
variant"default" | "outline""default"Same as Toggle.
size"default" | "sm" | "lg""default"Same as Toggle.
aria-invalidboolean-Marks the choice as wrong (a Form's `FormControl` sets it). The shared box of an outline group turns red pencil; separate toggles each draw a red box. An item's own `aria-invalid={false}` wins; a disabled group stays graphite. Put the group in a `Field` and it keeps the width of its buttons (a vertical Field stretches every other child).
spacingnumber0Gap between items in the spacing scale (4 px units). 0 joins them: outline items share one box and the seams between them.
tone"hi" | "marker""hi"The color of an item's body when it is on.
orientation"horizontal" | "vertical"-Vertical stacks the items, stretches them to one width, and moves the roving focus to the up and down arrows.
value / defaultValue / onValueChangeRadix-Controlled and uncontrolled state: a string for single, a string[] for multiple.
disabledbooleanfalseDisables every item; the joined box turns into a dashed pencil box.
seedstring | number-Pins the hand of the group's box.

ToggleGroupItem

A Radix ToggleGroup item: a toggle that reads variant and size from its group. In a joined group it draws only its seam (at the edge it shares with the item before it), its body and its focus ring.

属性类型默认值说明
valuestring-The item's value in the group.
variant / sizeas Toggle-Used only when the group does not set them. An item that asks for outline in a group with no variant draws a box of its own (the group draws none); only a group with variant outline and spacing 0 shares one box.
asChildbooleanfalseRender your own element (Radix Slot).
seedstring | number-Pins the hand of this item.

无障碍

  • Roving focus by Radix: the group is one tab stop, the arrow keys (left and right, or up and down when vertical) move between items and loop, Home and End jump to the ends, Space and Enter press.
  • single is announced as a radio group with checked items; multiple as a toolbar of pressed buttons. Give the group an aria-label and every icon-only item its own.
  • On is never told by hue alone: a filled body sits under the item, and (separate items) a solid box replaces the whisper box.
  • The focus ring is drawn on the item and lifted above its neighbors; Windows high contrast falls back to real borders and the browser's focus ring.
  • In right-to-left layouts the seams move to the other edge; the drawing is mirrored by the recipe, not by CSS.

源码

CLI 会复制进你项目的文件。

正在加载源码