Buttons that stay pressed: one on its own, or a toolbar group where one or several can be on at a time.
- Text formatting
- Alignment buttons
- View mode switch
- Filter by tag
Examples
Installation
npx knowns-ui@latest add toggle-groupPaste this into your coding agent (Claude Code, Cursor, Codex). It checks your project, sets up the registry, runs the install and verifies it.
Install Toggle group from Knowns UI into this project. Work step by step, show me each change, and ask before you edit files outside this project.
Context: Knowns UI (https://ui.knowns.dev) is a shadcn registry. Items land in the project as source files. Docs: https://ui.knowns.dev/docs/toggle-group
1. Check the project first. Stop and tell me if any of these is missing:
- React 19 and Tailwind CSS v4.
- A components.json at the project root. If there is none, ask me before running `npx knowns-ui@latest init --no-key` (it runs shadcn init when needed, then adds the registries).
2. Run:
```bash
npx knowns-ui@latest add toggle-group
```
It writes these files:
- lib/knowns/run.ts
- lib/knowns/json.tsx
- lib/knowns/status.ts
- lib/knowns/tone.ts
- lib/knowns/types.ts
- lib/knowns/use-abortable.ts
- lib/knowns/utils.ts
- lib/knowns/motion.tsx
- lib/knowns/motion-utils.ts
- components/ui/knowns/use-floating.ts
- components/ui/knowns/tooltip.tsx
- components/ui/knowns/tooltip-utils.ts
- components/ui/knowns/toggle-group.tsx
and installs these npm packages: clsx@^2.1.1, tailwind-merge@^3.7.0.
3. While it runs:
- It also adds a few @keyframes (all named kn-*) to the global CSS file (globals.css or the file components.json points at). Let it: they only add animations.
- If it asks to overwrite files under lib/knowns/: these are shared by every Knowns UI item. List them, say whether I changed them (check git), and ask me before you answer.
- If it asks to overwrite any other file, show me the diff and ask me.
4. Verify:
- Every file in the list above exists.
- The project type-checks (for example `npx tsc --noEmit`) and the dev build has no new errors.
5. Use it: follow the examples and props on https://ui.knowns.dev/docs/toggle-group, and ask me if anything about my data is unclear.
If the Knowns UI MCP server is connected (https://ui.knowns.dev/docs/ai-and-mcp), use get_component({ name: "toggle-group" }) or search_docs for details.
Finish with a short summary: the files you added or changed, the packages you installed, and anything I still need to do.Props
- type
"single"|"multiple"- single: value is string | null (at most one pressed). multiple: value is string[].
- value / defaultValue / onValueChange
string|null · string[]- Default
null / [] - Pressed items, controlled or not.
- required
boolean- Default
false - Single only: pressing the pressed item keeps it pressed.
- aria-label
string- Required name of the toolbar (role="toolbar", aria-orientation).
- options
{ value, label?, icon?, disabled?, tooltip?, aria-label? }[]- Items as data. Icon-only items use tooltip as their name when there's no aria-label.
- children
<ToggleGroupItem value icon? tooltip? disabled?>- Or items as direct children.
- variant
"outline"|"ghost"- Default
"outline" - Outline: card segments with hairlines. Ghost: quiet toggles (in a light tray when attached).
- size
"sm"|"md"- Default
"md" - 28 or 32px high (taller on touch screens).
- attached
boolean- Default
true - Joined segments, or separate toggles with a small gap.
- orientation
"horizontal"|"vertical"- Default
"horizontal" - Also picks the arrow keys: ← → or ↑ ↓.
- disabled
boolean- Default
false - Disables every item.
- Keyboard
-- One tab stop. Arrows / Home / End move focus (skipping disabled, wrapping); Space or Enter presses. Arrows never change the value.
- Toggle.pressed / defaultPressed / onPressedChange
boolean- Default
false - aria-pressed state, controlled or not.
- Toggle.icon / variant / size / tooltip
ReactNode / variant / size / ReactNode- Default
- / "ghost" / "md" - Without children it's square; give it an aria-label.
| Prop | Type | Default | Description |
|---|---|---|---|
| type | "single" | "multiple" | - | single: value is string | null (at most one pressed). multiple: value is string[]. |
| value / defaultValue / onValueChange | string | null · string[] | null / [] | Pressed items, controlled or not. |
| required | boolean | false | Single only: pressing the pressed item keeps it pressed. |
| aria-label | string | - | Required name of the toolbar (role="toolbar", aria-orientation). |
| options | { value, label?, icon?, disabled?, tooltip?, aria-label? }[] | - | Items as data. Icon-only items use tooltip as their name when there's no aria-label. |
| children | <ToggleGroupItem value icon? tooltip? disabled?> | - | Or items as direct children. |
| variant | "outline" | "ghost" | "outline" | Outline: card segments with hairlines. Ghost: quiet toggles (in a light tray when attached). |
| size | "sm" | "md" | "md" | 28 or 32px high (taller on touch screens). |
| attached | boolean | true | Joined segments, or separate toggles with a small gap. |
| orientation | "horizontal" | "vertical" | "horizontal" | Also picks the arrow keys: ← → or ↑ ↓. |
| disabled | boolean | false | Disables every item. |
| Keyboard | - | - | One tab stop. Arrows / Home / End move focus (skipping disabled, wrapping); Space or Enter presses. Arrows never change the value. |
| Toggle.pressed / defaultPressed / onPressedChange | boolean | false | aria-pressed state, controlled or not. |
| Toggle.icon / variant / size / tooltip | ReactNode / variant / size / ReactNode | - / "ghost" / "md" | Without children it's square; give it an aria-label. |
Picking one of a few modes, like a tab bar, where something is always selected? Use SegmentedControl: a radio group with a sliding thumb where arrows change the value. Use ToggleGroup when items are switches that can be on or off.
What it handles
- 01Renders independent aria-pressed buttons inside a role toolbar, from an options array or from ToggleGroupItem children.
- 02Runs in single mode, which swaps the pressed item and clears it on a second press unless required, or in multiple mode, which toggles each item.
- 03Gives the group one tab stop, moves focus with the arrows that match the orientation plus Home and End, and skips disabled items.
- 04Joins items into one segmented outline with shared hairlines by default, or spaces them with attached set to false, horizontally or vertically.
- 05Uses an item tooltip as the accessible name of an icon-only item when no aria-label is given.
- 06Exports a standalone Toggle for a single pin or mute button, plus the helpers nextPressed and rovingTarget for tests.
States
- First render
- Nothing. Pressed items paint in their pressed look
- none
- Press
- The item gains a soft tint, a firmer ring and full ink; the previous item in single mode returns to muted
- 120ms, ease out
- Hover
- An unpressed item takes a faint background and full ink
- 120ms, ease out
- Roving focus
- Focus moves to the next enabled item and the ring follows it
- none, at once
- Disabled
- The item dims and ignores the pointer
- none, at once
- Reduced motion
- Colour changes land at once
- 0ms
| State | What moves | Timing |
|---|---|---|
| First render | Nothing. Pressed items paint in their pressed look | none |
| Press | The item gains a soft tint, a firmer ring and full ink; the previous item in single mode returns to muted | 120ms, ease out |
| Hover | An unpressed item takes a faint background and full ink | 120ms, ease out |
| Roving focus | Focus moves to the next enabled item and the ring follows it | none, at once |
| Disabled | The item dims and ignores the pointer | none, at once |
| Reduced motion | Colour changes land at once | 0ms |
Works with
When to use
When to use
- Toolbars with pressable options, single or multiple, such as text formatting or filters.
When not to use
- Switching views: use Segmented Control.
- A form choice: use Radio Group.
Using with AI
Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:
- Add the Knowns UI Toggle Group to my app and wire it to my own data.
- I need toolbars with pressable options, single or multiple, such as text formatting or filters. Set that up with the Knowns UI Toggle Group.
- Which Toggle Group props do I need for a minimal setup? Show me a short example.
With the server connected, your assistant calls get_component({ name: "toggle-group" }) and search_docs({ query: "toggle group" }).