Back to Plate

Mention

content/docs/(plugins)/(elements)/mention.mdx

53.0.86.8 KB
Original Source

Mention turns trigger text such as @ into an inline combobox input and inserts a markable void mention node when the user selects an item. The package owns trigger detection, input node creation, mention insertion, selection movement, and Markdown mention serialization. The registry owns the demo item list and the inline combobox UI.

<ComponentPreview name="mention-demo" /> <PackageInfo>

Features

  • Inline void mention nodes with value and optional key.
  • Inline void mention_input nodes created by trigger text.
  • Trigger combobox support from @platejs/combobox.
  • Configurable trigger strings, regexes, and trigger queries.
  • Markable void rendering so bold, italic, and underline can style a mention.
  • Optional trailing space after selected mention items.
  • Markdown format through [display text](mention:id) plus bare @name deserialization.
</PackageInfo>

Fast Path

<Steps>

Add The Kit

MentionKit installs MentionPlugin, MentionInputPlugin, the registry mention nodes, and a trigger rule that allows @ at the start of a line, after whitespace, or after quotes.

<ComponentSource name="mention-kit" />
tsx
import { createPlateEditor } from 'platejs/react';

import { MentionKit } from '@/components/editor/plugins/mention-kit';

export const editor = createPlateEditor({
  plugins: MentionKit,
});

Render Mentions

mention-node renders both the selected mention and the temporary combobox input. The demo data lives in that registry UI file, so replace it with your app users, pages, or records.

<ComponentSource name="mention-node" />

Add Static Rendering

mention-base-kit uses BaseMentionPlugin with the static mention node for read-only output.

<ComponentSource name="mention-base-kit" /> </Steps>

Ownership

LayerOwnerWhat It Does
@platejs/mentionPackageExports BaseMentionPlugin, BaseMentionInputPlugin, and getMentionOnSelectItem.
@platejs/mention/reactPackageExports MentionPlugin and MentionInputPlugin.
@platejs/comboboxPackageProvides withTriggerCombobox, trigger options, and combobox input hooks.
mention-kitRegistryAdds mention plugins with editable mention and input components.
mention-base-kitRegistryAdds BaseMentionPlugin.withComponent(MentionElementStatic).
mention-nodeRegistry UIRenders mention atoms, the inline combobox input, and demo items.
inline-comboboxRegistry UIRenders the Ariakit-backed popover, input, groups, items, and empty state.
@platejs/markdownPackageSerializes mentions as mention: links and deserializes mention links or bare mentions.

Manual Setup

<Steps>

Install Package

bash
npm install @platejs/mention

Add Plugins

Configure the mention plugin with the trigger behavior and render both node types.

tsx
import { MentionInputPlugin, MentionPlugin } from '@platejs/mention/react';
import { createPlateEditor } from 'platejs/react';

import {
  MentionElement,
  MentionInputElement,
} from '@/components/ui/mention-node';

export const editor = createPlateEditor({
  plugins: [
    MentionPlugin.configure({
      options: {
        triggerPreviousCharPattern: /^$|^[\s"']$/,
      },
    }).withComponent(MentionElement),
    MentionInputPlugin.withComponent(MentionInputElement),
  ],
});

Select An Item

Use getMentionOnSelectItem from your combobox item renderer. It inserts the mention, moves the cursor after it, and inserts a trailing space only when insertSpaceAfterMention is enabled and the mention lands at the end of the block.

tsx
import { getMentionOnSelectItem } from '@platejs/mention';

const onSelectItem = getMentionOnSelectItem();

<InlineComboboxItem
  value={item.text}
  onClick={() => onSelectItem(editor, item, search)}
>
  {item.text}
</InlineComboboxItem>;
</Steps>

Value Shape

BaseMentionPlugin uses KEYS.mention, which resolves to the mention node type. The input plugin uses KEYS.mentionInput, which resolves to mention_input.

tsx
const value = [
  {
    children: [
      { text: 'Assigned to ' },
      {
        children: [{ text: '' }],
        key: 'user_123',
        type: 'mention',
        value: 'Jane Smith',
      },
      { text: '.' },
    ],
    type: 'p',
  },
];
FieldTypeNotes
type'mention'Inline void mention node.
valuestringDisplay text rendered by the registry node.
keyunknownOptional stable id used by selection handlers and Markdown serialization.
children[{ text: '' }]Empty child required for Slate inline void nodes.

The mention node is isMarkableVoid, so marks on its empty child can style the rendered mention.

Trigger Flow

withTriggerCombobox overrides insertText. It creates a combobox input only when every gate passes.

GateSource
Inserted text matches triggerstring, string[], or RegExp.
Insert is not using options.atProgrammatic text insertion bypasses the trigger.
Editor has a selectionNo selection means no inline input target.
triggerQuery(editor) returns trueOptional app veto for custom contexts.
Previous character matches triggerPreviousCharPatternDefaults to /^\s?$/; registry kit uses /^$|^[\s"']$/.

The default createComboboxInput creates:

tsx
{
  children: [{ text: '' }],
  trigger: '@',
  type: KEYS.mentionInput,
}

If editor.meta.userId exists, the combobox input stores that userId so only the creator sees the transient input in collaborative editors.

Markdown

@platejs/markdown serializes mentions as link-style mention: URLs. It uses key for the URL when present and value for the visible text.

md
Hello [Jane Smith](mention:user_123).

Deserialization supports link-style mentions and bare @alice text. Normal links such as [@docs](/docs/mention) stay links.

API Reference

APIPackageUse
BaseMentionPlugin@platejs/mentionHeadless inline markable void mention plugin with trigger-combobox behavior.
BaseMentionInputPlugin@platejs/mentionInline void input node inserted while the combobox is active.
MentionPlugin@platejs/mention/reactReact mention plugin.
MentionInputPlugin@platejs/mention/reactReact mention input plugin.
editor.tf.insert.mention({ key, value })BaseMentionPlugin transformInserts the mention node at the current selection.
getMentionOnSelectItem({ key? })@platejs/mentionReturns an item handler for mention combobox selection.
withTriggerCombobox@platejs/comboboxCreates temporary combobox input nodes from trigger text.
useComboboxInput@platejs/combobox/reactHandles focus, cancellation, arrow/backspace/escape behavior, and undo/redo forwarding for custom input UI.