aiellie

Search

Find a component, template or page

GitHub
Components

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.

Select any of these words. A container query styles an element by the width of its parent, so a card can lay itself out differently in a sidebar and in the main column.
Open full screen
fig. 01 · open it full screen from the corner

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

  1. Wrap the thread

    MessageHighlight is 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.tsx
    import { 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

PropTypeDefaultDescription
tooltipReactNoderequiredThe button's name.
onAction(text: string) => voidCalled with the selected words, after which the toolbar closes.

All other ToolbarButton props are forwarded.

MessageHighlightToolbar

PropTypeDefaultDescription
classNamestringMerged onto the toolbar.

All other Toolbar props are forwarded.

useMessageHighlight

FieldTypeDefaultDescription
highlight{ text: string; x: number; y: number } | nullThe selected words and where the toolbar sits.
clear() => voidCloses the toolbar and the selection.