Toggle Group

View as Markdown

Buttons that stay pressed and share one value, such as text styles.

When to use

Buttons that stay pressed and share one value, such as text styles. Use Segmented Control instead when exactly one choice is on and the row reads as a single control.

Usage

import { ToggleGroup } from "./ui/uicontext/components/ToggleGroup.ts"
const Message = UiSchema.TaggedEnum({ ChangedMarks: { marks: Schema.Array(Schema.String) } })export const Marks = (h: Html.H<typeof Message.Type>, marks: ReadonlyArray<string>) =>	ToggleGroup(h, {		label: "Text style",		multiple: true,		value: marks,		items: [			{ value: "bold", label: "Bold", shortcut: "mod+b" },			{ value: "italic", label: "Italic", shortcut: "mod+i" },		],		onChange: (next) => Message.ChangedMarks({ marks: next }),	})

Examples

Labels, several on

Without icons each button shows its label. A disabled one stays in place but can't be pressed.

Showing Organic search, Paid, and Email.

API reference

Options

ToggleGroup options
NameTypeDescription
keystringNames the group among others in the same view; needed for more than one.
labelrequiredstringThe group's accessible name: "Text style", "Columns".
itemsrequiredToggleGroupItem<Message>[]—
valuerequiredstring[]The pressed items' values. The caller keeps them.
onChangerequired(value: string[]) => MessageWhat a press asks for: the values to store and pass back as value.
multiplebooleanAny number on at once; at most one by default.
iconOnlybooleanIcons alone, each named by its label: an editor's toolbar.
barebooleanNo frame around the buttons, for a group inside a toolbar that has one.
orientation"horizontal" | "vertical"—
disabledboolean—
styleStyleStyleX styles applied after the group's own.

Keyboard and accessibility

KeysDoes
Right arrow Left arrowMove to the next or the previous button
Home EndGo to the first or the last button
Space EnterPress or release the button

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/toggle-group

Source