Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Button Group examples · Patterns

Contextual Toolbar Button Group for React

A formatting toolbar that appears when text in the box is selected, above a labeled Bold, Italic and Underline group.

Framework
UI library

A toolbar that only shows up when there's something to format. Select part of the sentence in the box and an icon-only Bold, Italic and Underline group appears below it; clear the selection and it goes away. The labeled group underneath is always visible, for comparison.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

How does the toolbar know text is selected?

The demo listens for the document's selectionchange event. When the selection starts inside the paragraph and isn't empty, the toolbar shows; otherwise it hides.

Does it work with keyboard selection?

Yes. selectionchange fires for mouse, touch and keyboard selections alike, wherever text can be selected.

Is the listener cleaned up?

Yes. It's added when the demo mounts and removed when it unmounts.

How are the icon buttons named?

Each has an aria-label, Bold, Italic or Underline, and size="icon-sm". The group is labeled Format selection.

Do the buttons format the selection?

No. They have no handler. Call your editor's formatting command for the current selection.

How do I float the toolbar over the selection?

Read the selection's bounding rectangle and position the group there, or anchor a Popover to it. The demo places it under the paragraph.