Skip to content

Mention menu

A mention menu is a popup of suggestion candidates triggered by typing @ (for users), # (for tags), or any other character you choose. The selected candidate is inserted as a mention node from the mention extension.

'use client'

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { tags } from '../../sample/sample-tag-data'
import { users } from '../../sample/sample-user-data'
import { TagMenu } from '../../ui/tag-menu'
import { UserMenu } from '../../ui/user-menu'

import { defineExtension } from './extension'

export default function Editor() {
  const editor = useMemo(() => {
    const extension = defineExtension()
    return createEditor({ extension })
  }, [])

  return (
    <ProseKit editor={editor}>
      <div className="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-[canvas] text-black dark:text-white">
        <div className="relative w-full flex-1 box-border overflow-y-auto">
          <div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
          <UserMenu users={users} />
          <TagMenu tags={tags} />
        </div>
      </div>
    </ProseKit>
  )
}
npx shadcn@latest add @prosekit/react-ui-user-menu

The mention menu uses the same autocomplete primitive as the Slash menu. The difference is the regex (matches @ or # instead of /) and the onSelect callback (inserts a mention node instead of running a command).

AutocompleteRoot                 # root component
└── AutocompletePositioner       # anchors the popup to the cursor
    └── AutocompletePopup        # the floating panel
        ├── AutocompleteEmpty    # "Loading…" while fetching, "No results" otherwise
        └── AutocompleteItem     # one per candidate; onSelect calls insertMention()

AutocompleteRoot exposes onQueryChange and onOpenChange so you can fetch candidates from a remote source and reset the list when the popup closes. The kind you pass to insertMention (e.g. 'user' or 'tag') lets the same component back several trigger characters.