This page as markdown: /caret-menu.md · Index for agents: /llms.txt · Everything in one file: /llms-full.txt

CaretMenu

Status: beta

Suggestions at the caret of a text field, for @mentions and /slash commands. useCaretTrigger watches a Textarea, Input or ChatComposer (through fieldRef) for a trigger character and reports the query; CaretMenu draws the list beside the trigger and drives it from the field's keys, so focus never leaves the field. The value stays plain text: a mention is inserted as @Sam Rivera, a command runs and clears its /query.

Keywords: mention, mentions, at mention, slash command, slash commands, autocomplete, typeahead, suggestions, emoji picker, hashtag, trigger

Usage

Use CaretMenu with useCaretTrigger when typing a character in running text should offer choices: @ for people, / for commands, # for channels, : for emoji. The app owns the items (filter or fetch them from trigger.match.query) and what choosing one does; Sable owns detecting the trigger, placing the list at the caret, the keys and the accessibility. Use Combobox when the whole field is a search for one value, and CommandMenu for a global palette.

Best practices

Anatomy

Part Required Description
useCaretTrigger yes The hook on the field: trigger detection, the query, the caret anchor, replace() and dismiss().
CaretMenu yes The list at the caret, its keys, and its announcements.
CaretMenuItem yes One suggestion row, with optional icon and description.

Subcomponents

Name Type Description
CaretMenuItem div props + onSelect: () => void + disabled?: boolean + icon?: ReactNode + description?: ReactNode One suggestion, styled as a Menu row. onSelect runs on Enter, Tab or click; icon and description work as on MenuItem. A disabled row shows but the arrow keys skip it.

Props

Name Type Default Description
trigger UseCaretTriggerResult — The result of useCaretTrigger({ triggers }), whose fieldRef is attached to the field. Each trigger is { char, atStart?, allowSpaces?, maxQueryLength? }; the result carries match ({ trigger, query, start, end } or null), replace(text), dismiss() and getAnchorRect().
label string — The list's accessible name, e.g. "People" or "Commands".
status ReactNode — A row under the items for what the list is doing: loading, nothing found, an error. Announced politely in place of the suggestion count. With no items and no status the list stays shut.
children ReactNode — CaretMenuItems, already filtered for the query.
className string — Layout classes for the popup, such as a width.

Tokens used

popover, popover-foreground, border-floating, accent, accent-foreground, muted-foreground

Motion

Accessibility