Suggestion Menu
Show a list of matching commands or people when a user types a trigger character.
Installation
Run this command in your project.
npx @tiptap/cli@latest add suggestion-menuUsage
Pass an editor instance and return the matching items from items. Each item supplies an onSelect handler. This example replaces the trigger and query with a name.
'use client'
import type { Editor } from '@tiptap/react'
import {
SuggestionMenu,
filterSuggestionItems,
type SuggestionItem,
type SuggestionMenuRenderProps,
} from '@/components/tiptap-ui-utils/suggestion-menu'
const people: SuggestionItem[] = ['Alex Chen', 'Sam Lee'].map((name) => ({
title: name,
onSelect: ({ editor, range }) => {
editor.chain().focus().insertContentAt(range, name).run()
},
}))
function Options({ items, selectedIndex, onSelect }: SuggestionMenuRenderProps) {
return items.map((item, index) => (
<button
key={item.title}
type="button"
role="option"
aria-selected={index === selectedIndex}
onClick={() => onSelect(item)}
>
{item.title}
</button>
))
}
export function PeopleMenu({ editor }: { editor: Editor | null }) {
return (
<SuggestionMenu
editor={editor}
char="@"
pluginKey="peopleMenu"
items={({ query }) => filterSuggestionItems(people, query)}
>
{(props) => <Options {...props} />}
</SuggestionMenu>
)
}Props
| Name | Type | Default | Description |
|---|---|---|---|
editor | Editor | null | undefined | The Tiptap editor instance |
char | string | "@" | Character that triggers the suggestion menu |
items | (props) => Item[] | Promise<Item[]> | () => [] | Function returning suggestion items |
children | (props) => ReactNode | Required | Render function for menu content |
floatingOptions | Partial<UseFloatingOptions> | undefined | Additional floating UI positioning options |
selector | string | "tiptap-suggestion-menu" | Value of the menu’s data-selector attribute |
pluginKey | string | PluginKey | SuggestionPluginKey | Unique identifier for the suggestion plugin |
maxHeight | number | 384 | Largest menu height in pixels. Sets --suggestion-menu-max-height, see Styling and accessibility |
allowSpaces | boolean | false | Allow spaces in suggestion queries |
allowToIncludeChar | boolean | false | Include trigger character in query |
allowedPrefixes | string[] | null | [" "] | Characters that can precede the trigger |
startOfLine | boolean | false | Only trigger at line start |
decorationTag | string | "span" | HTML tag for decoration element |
decorationClass | string | "suggestion" | CSS class for decoration styling |
decorationContent | string | "" | Placeholder text in decoration |
Styling and accessibility
The menu renders your list inside a container with the class tiptap-suggestion-menu and the data-selector value. It sets --suggestion-menu-max-height on that container to the smaller of maxHeight and the space left in the viewport. The container does not apply the height itself, so use the variable in your list styles to make long lists scroll.
.tiptap-suggestion-menu {
max-height: var(--suggestion-menu-max-height);
overflow-y: auto;
}The container has role="listbox" and aria-label="Suggestions". Give each item role="option" and aria-selected, as in the usage example. Arrow keys move the selection, Enter selects the item and Escape closes the menu. The menu keeps the focus in the editor while you click an item.
Suggestion items
Import SuggestionItem<T> to type your items and SuggestionMenuRenderProps<T> to type your list component. Pass application data through context. The selection handler receives that context along with the editor and the range that covers the trigger and query.
| Field | Purpose |
|---|---|
title | Required display text. |
subtext, keywords | Additional strings to match when filtering. |
badge, group | Optional metadata for a custom renderer. |
context | Application data available to the selection handler. |
onSelect | Command to run when the user selects the item. |
Filtering and grouping
filterSuggestionItems(items, query) checks the title, subtext, and keywords without case sensitivity. It ranks exact title matches first, followed by titles that start with the query. An empty query returns the original items.
To group results, use each item's group in your renderer. Compare each item with items[selectedIndex] when marking the active option. Keyboard navigation counts positions across the whole list.
Loading remote results
The items callback also accepts a promise. For example, adapt a directory search to return suggestion items and pass the result as the menu's items prop.
import type { SuggestionItem } from '@/components/tiptap-ui-utils/suggestion-menu'
type Person = { id: string; name: string }
export function directoryItems(search: (query: string) => Promise<Person[]>) {
return async ({ query }: { query: string }): Promise<SuggestionItem[]> => {
const people = await search(query)
return people.map(({ name }) => ({
title: name,
onSelect: ({ editor, range }) => {
editor.chain().focus().insertContentAt(range, { type: 'text', text: name }).run()
},
}))
}
}Finding the start of a query
calculateStartPosition(cursorPosition, previousNode, triggerChar) finds the last trigger character in the preceding text node. It returns the cursor position if the node has no text or the trigger is missing.