Message Highlight Toolbar
A small toolbar that floats over text selected in a thread, with actions that get the selected words, like quoting or explaining them.
Installation
npx shadcn@latest add "@aiellie/message-highlight-toolbar"shadcn needs the @aiellie registry in components.json first. npx aiellie add message-highlight-toolbar adds it on its first run, then installs the same way.
Message Highlight Toolbar floats a small toolbar over text someone selects in a thread, with actions that get the selected words: quote them into a reply, ask what they mean, copy them. It appears just above the selection, kept inside the thread's sides, and goes away on a press anywhere else or on Escape.
Getting started
Wrap the thread
MessageHighlightis the region selections are read in. Put the toolbar inside it, with an action for each thing to do with the words.components/thread-with-quotes.tsximport { QuoteDownIcon } from "@hugeicons/core-free-icons" import { HugeiconsIcon } from "@hugeicons/react" import { MessageHighlight, MessageHighlightAction, MessageHighlightToolbar, } from "@/components/aiellie/message-highlight-toolbar" export function ThreadWithQuotes({ children, onQuote, }: { children: React.ReactNode onQuote: (text: string) => void }) { return ( <MessageHighlight> {children} <MessageHighlightToolbar> <MessageHighlightAction tooltip="Quote" onAction={onQuote}> <HugeiconsIcon aria-hidden icon={QuoteDownIcon} /> </MessageHighlightAction> </MessageHighlightToolbar> </MessageHighlight> ) }
Anatomy
<div data-slot="message-highlight" data-highlighted>
{/* the thread */}
<div data-slot="message-highlight-toolbar" role="toolbar"> {/* while text is selected */}
<button data-slot="message-highlight-action" />
</div>
</div>The toolbar is the aiellie Toolbar floated as glass with a shadow, so its buttons are walked with the arrow keys and each names itself on a tooltip. It reads the selection when the pointer or a key lets go, and keeps the words, since pressing a button clears the selection before the button runs.
useMessageHighlight gives anything inside the region the selected words and a way to clear them, for an action of your own.
Examples
Ask about a phrase
An action can fill the composer with the words as a quote.
Select a phrase, then ask about it. The words go into the composer as a quote, ready for a question.
API reference
MessageHighlightAction
| Prop | Type | Default | Description |
|---|---|---|---|
tooltip | ReactNode | required | The button's name. |
onAction | (text: string) => void | Called with the selected words, after which the toolbar closes. |
All other ToolbarButton props are forwarded.
MessageHighlightToolbar
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | Merged onto the toolbar. |
All other Toolbar props are forwarded.
useMessageHighlight
| Field | Type | Default | Description |
|---|---|---|---|
highlight | { text: string; x: number; y: number } | null | The selected words and where the toolbar sits. | |
clear | () => void | Closes the toolbar and the selection. |