Skip to content

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-menu

Usage

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

NameTypeDefaultDescription
editorEditor | nullundefinedThe Tiptap editor instance
charstring"@"Character that triggers the suggestion menu
items(props) => Item[] | Promise<Item[]>() => []Function returning suggestion items
children(props) => ReactNodeRequiredRender function for menu content
floatingOptionsPartial<UseFloatingOptions>undefinedAdditional floating UI positioning options
selectorstring"tiptap-suggestion-menu"Value of the menu’s data-selector attribute
pluginKeystring | PluginKeySuggestionPluginKeyUnique identifier for the suggestion plugin
maxHeightnumber384Largest menu height in pixels. Sets --suggestion-menu-max-height, see Styling and accessibility
allowSpacesbooleanfalseAllow spaces in suggestion queries
allowToIncludeCharbooleanfalseInclude trigger character in query
allowedPrefixesstring[] | null[" "]Characters that can precede the trigger
startOfLinebooleanfalseOnly trigger at line start
decorationTagstring"span"HTML tag for decoration element
decorationClassstring"suggestion"CSS class for decoration styling
decorationContentstring""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.

FieldPurpose
titleRequired display text.
subtext, keywordsAdditional strings to match when filtering.
badge, groupOptional metadata for a custom renderer.
contextApplication data available to the selection handler.
onSelectCommand 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.