The fastest way to add slash command functionality is with the SlashKit, which includes a pre-configured SlashPlugin with its Plate UI popup.
'use client';
import {
CalendarIcon,
ChevronRightIcon,
Code2,
Columns3Icon,
Heading1Icon,
Heading2Icon,
Heading3Icon,
LightbulbIcon,
ListIcon,
ListOrdered,
PenToolIcon,
PilcrowIcon,
Quote,
RadicalIcon,
SparklesIcon,
Square,
SuperscriptIcon,
Table,
TableOfContentsIcon,
} from 'lucide-react';
import {
BaseBlockquotePlugin,
BaseCodeBlockPlugin,
BaseHeadingPlugin,
BaseListPlugin,
BaseParagraphPlugin,
type PluginTransaction,
} from 'platejs';
import { AIChatPlugin } from 'platejs/ai/react';
import { BaseCalloutPlugin } from 'platejs/callout';
import type { ComboboxState } from 'platejs/combobox';
import { BaseDatePlugin } from 'platejs/date';
import { BaseDetailsPlugin, BaseDetailsSummaryPlugin } from 'platejs/details';
import { BaseExcalidrawPlugin } from 'platejs/excalidraw';
import { BaseFootnotePlugin } from 'platejs/footnote';
import { BaseColumnGroupPlugin } from 'platejs/layout';
import { BaseEquationPlugin, BaseInlineEquationPlugin } from 'platejs/math';
import {
type EditableSiblingProps,
type Editor,
definePlugin,
useEditor,
usePluginStore,
} from 'platejs/react';
import { BaseTablePlugin } from 'platejs/table';
import { BaseTocPlugin } from 'platejs/toc';
import * as React from 'react';
import {
InlineCombobox,
InlineComboboxContent,
InlineComboboxEmpty,
InlineComboboxGroup,
InlineComboboxGroupLabel,
InlineComboboxItem,
rankOptions,
useInlineComboboxQuery,
} from './inline-combobox';
type Item = {
icon: React.ReactNode;
isAvailable: (editor: Editor) => boolean;
label: string;
onClick?: (editor: Editor) => void;
onSelect?: (editor: Editor, tx: PluginTransaction) => boolean | void;
focusEditor?: boolean;
keywords?: readonly string[];
};
type Group = {
group: string;
items: readonly Item[];
};
/**
* A slash menu item an app adds through `SlashPlugin`'s `items` state. It joins
* the group with the same name, or starts a new group after the built-in ones.
*/
export type SlashItem = Omit<Item, 'icon'> & {
group: string;
icon: () => React.ReactNode;
};
const isTitle = (editor: Editor, tx: PluginTransaction) => {
const summary = editor.plugin(BaseDetailsSummaryPlugin);
return (
summary.installed && tx.nodes.block()?.[0].type === summary.schema.type
);
};
const upsertHeading = (
editor: Editor,
tx: PluginTransaction,
level: 1 | 2 | 3
) => {
const heading = tx.plugin(BaseHeadingPlugin);
return isTitle(editor, tx)
? heading.setLevel(level)
: heading.upsert({ level }, { select: true });
};
const groups: Group[] = [
{
group: 'AI',
items: [
{
focusEditor: false,
icon: <SparklesIcon />,
isAvailable: (editor) => editor.plugin(AIChatPlugin).installed,
label: 'AI',
onClick: (editor) => {
const ai = editor.plugin(AIChatPlugin);
if (!ai.installed || editor.read.view.isReadOnly()) return;
ai.api.show();
},
},
],
},
{
group: 'Basic blocks',
items: [
{
icon: <PilcrowIcon />,
isAvailable: (editor) => editor.plugin(BaseParagraphPlugin).installed,
keywords: ['paragraph'],
label: 'Text',
onSelect: (editor, tx) =>
isTitle(editor, tx)
? tx.blocks.reset({ unset: ['level'] })
: tx.plugin(BaseParagraphPlugin).upsert({}, { select: true }),
},
{
icon: <Heading1Icon />,
isAvailable: (editor) => editor.plugin(BaseHeadingPlugin).installed,
keywords: ['title', 'h1'],
label: 'Heading 1',
onSelect: (editor, tx) => upsertHeading(editor, tx, 1),
},
{
icon: <Heading2Icon />,
isAvailable: (editor) => editor.plugin(BaseHeadingPlugin).installed,
keywords: ['subtitle', 'h2'],
label: 'Heading 2',
onSelect: (editor, tx) => upsertHeading(editor, tx, 2),
},
{
icon: <Heading3Icon />,
isAvailable: (editor) => editor.plugin(BaseHeadingPlugin).installed,
keywords: ['subtitle', 'h3'],
label: 'Heading 3',
onSelect: (editor, tx) => upsertHeading(editor, tx, 3),
},
{
icon: <ListIcon className="[&:dir(rtl)]:-scale-x-100" />,
isAvailable: (editor) => editor.plugin(BaseListPlugin).installed,
keywords: ['unordered', 'ul', '-'],
label: 'Bulleted list',
onSelect: (_editor, tx) =>
tx
.plugin(BaseListPlugin)
.upsert({ type: 'bulleted' }, { select: true }),
},
{
icon: <ListOrdered />,
isAvailable: (editor) => editor.plugin(BaseListPlugin).installed,
keywords: ['ordered', 'ol', '1'],
label: 'Numbered list',
onSelect: (_editor, tx) =>
tx
.plugin(BaseListPlugin)
.upsert({ type: 'numbered' }, { select: true }),
},
{
icon: <Square />,
isAvailable: (editor) => editor.plugin(BaseListPlugin).installed,
keywords: ['checklist', 'task', 'checkbox', 'todo', '[]'],
label: 'To-do list',
onSelect: (_editor, tx) =>
tx.plugin(BaseListPlugin).upsert({ type: 'task' }, { select: true }),
},
{
icon: <ChevronRightIcon className="[&:dir(rtl)]:rotate-180" />,
isAvailable: (editor) => editor.plugin(BaseDetailsPlugin).installed,
keywords: ['collapsible', 'expandable'],
label: 'Details',
onSelect: (_editor, tx) =>
tx.plugin(BaseDetailsPlugin).upsert({}, { select: true }),
},
{
icon: <Code2 />,
isAvailable: (editor) => editor.plugin(BaseCodeBlockPlugin).installed,
keywords: ['codeblock', '```'],
label: 'Code Block',
onSelect: (_editor, tx) =>
tx.plugin(BaseCodeBlockPlugin).upsert({}, { select: true }),
},
{
icon: <Table />,
isAvailable: (editor) => editor.plugin(BaseTablePlugin).installed,
label: 'Table',
onSelect: (_editor, tx) =>
tx.plugin(BaseTablePlugin).upsert({}, { select: true }),
},
{
icon: <Quote />,
isAvailable: (editor) => editor.plugin(BaseBlockquotePlugin).installed,
keywords: ['citation', 'blockquote', 'quote', '>'],
label: 'Blockquote',
onSelect: (_editor, tx) =>
tx.plugin(BaseBlockquotePlugin).upsert({}, { select: true }),
},
{
icon: <LightbulbIcon />,
isAvailable: (editor) => editor.plugin(BaseCalloutPlugin).installed,
keywords: ['note'],
label: 'Callout',
onSelect: (_editor, tx) =>
tx.plugin(BaseCalloutPlugin).upsert({}, { select: true }),
},
],
},
{
group: 'Advanced blocks',
items: [
{
icon: <TableOfContentsIcon />,
isAvailable: (editor) => editor.plugin(BaseTocPlugin).installed,
keywords: ['toc'],
label: 'Table of contents',
onSelect: (_editor, tx) =>
tx.plugin(BaseTocPlugin).upsert({}, { select: true }),
},
{
icon: <Columns3Icon />,
isAvailable: (editor) => editor.plugin(BaseColumnGroupPlugin).installed,
label: '3 columns',
onSelect: (_editor, tx) =>
tx
.plugin(BaseColumnGroupPlugin)
.upsert({ columns: 3 }, { select: true }),
},
{
focusEditor: false,
icon: <RadicalIcon />,
isAvailable: (editor) => editor.plugin(BaseEquationPlugin).installed,
label: 'Equation',
onSelect: (_editor, tx) =>
tx.plugin(BaseEquationPlugin).upsert({}, { select: true }),
},
{
icon: <PenToolIcon />,
isAvailable: (editor) => editor.plugin(BaseExcalidrawPlugin).installed,
keywords: ['excalidraw'],
label: 'Excalidraw',
onSelect: (_editor, tx) =>
tx.plugin(BaseExcalidrawPlugin).upsert({}, { select: true }),
},
],
},
{
group: 'Inline',
items: [
{
icon: <CalendarIcon />,
isAvailable: (editor) => editor.plugin(BaseDatePlugin).installed,
keywords: ['time'],
label: 'Date',
onSelect: (_editor, tx) =>
tx.plugin(BaseDatePlugin).insert({}, { select: true }),
},
{
icon: <SuperscriptIcon />,
isAvailable: (editor) => editor.plugin(BaseFootnotePlugin).installed,
keywords: ['citation', 'fn', 'footnote', '[^]'],
label: 'Footnote',
onSelect: (_editor, tx) =>
tx.plugin(BaseFootnotePlugin).insert({}, { select: true }),
},
{
focusEditor: false,
icon: <RadicalIcon />,
isAvailable: (editor) =>
editor.plugin(BaseInlineEquationPlugin).installed,
label: 'Inline Equation',
onSelect: (_editor, tx) =>
tx.plugin(BaseInlineEquationPlugin).insert({}, { select: true }),
},
],
},
];
export function SlashCombobox({ editableRef }: EditableSiblingProps) {
return (
<InlineCombobox editableRef={editableRef} plugin={SlashPlugin}>
<SlashComboboxContent />
</InlineCombobox>
);
}
function SlashComboboxContent() {
const editor = useEditor();
const query = useInlineComboboxQuery();
const appItems = usePluginStore(SlashPlugin, 'items');
const allGroups = [
...new Set([
...groups.map(({ group }) => group),
...appItems.map(({ group }) => group),
]),
].map((group) => ({
group,
items: [
...(groups.find((each) => each.group === group)?.items ?? []),
...appItems
.filter((item) => item.group === group)
.map(({ icon: Icon, ...item }) => ({ ...item, icon: <Icon /> })),
],
}));
const availableGroups = allGroups
.map(({ group, items }) => ({
group,
items: items.filter((item) => item.isAvailable(editor)),
}))
.filter(({ items }) => items.length > 0);
const rankedAcrossGroups = rankOptions(
availableGroups.flatMap(({ group, items }) =>
items.map((item) => ({ ...item, group }))
),
query
);
const sections = query.trim()
? [{ group: undefined, items: rankedAcrossGroups }]
: availableGroups;
return (
<InlineComboboxContent>
<InlineComboboxEmpty>No results</InlineComboboxEmpty>
{sections.map(({ group, items }) => (
<InlineComboboxGroup key={group ?? 'results'}>
{group ? (
<InlineComboboxGroupLabel>{group}</InlineComboboxGroupLabel>
) : null}
{items.map(({ focusEditor, icon, label, onClick, onSelect }) => (
<InlineComboboxItem
key={label}
onClick={() => {
onClick?.(editor);
}}
onSelect={(tx) => onSelect?.(editor, tx)}
focusEditor={focusEditor}
>
<div className="me-2 text-muted-foreground">{icon}</div>
{label}
</InlineComboboxItem>
))}
</InlineComboboxGroup>
))}
</InlineComboboxContent>
);
}
export const SlashPlugin = definePlugin('slashCommand', {
editOnly: true,
initialState: (): ComboboxState & { items: readonly SlashItem[] } => ({
items: [],
maxQueryLength: 75,
queryPattern: null,
trigger: '/',
triggerPreviousCharPattern: /^\s?$/,
triggerQuery: (editor) => {
const codeBlock = editor.plugin(BaseCodeBlockPlugin);
return (
!codeBlock.installed ||
!editor.read.nodes.some({
type: codeBlock.schema.type,
})
);
},
}),
});
export const SlashKit = [
SlashPlugin.configure({ slots: { afterEditable: SlashCombobox } }),
];'use client';
import {
CalendarIcon,
ChevronRightIcon,
Code2,
Columns3Icon,
Heading1Icon,
Heading2Icon,
Heading3Icon,
LightbulbIcon,
ListIcon,
ListOrdered,
PenToolIcon,
PilcrowIcon,
Quote,
RadicalIcon,
SparklesIcon,
Square,
SuperscriptIcon,
Table,
TableOfContentsIcon,
} from 'lucide-react';
import {
BaseBlockquotePlugin,
BaseCodeBlockPlugin,
BaseHeadingPlugin,
BaseListPlugin,
BaseParagraphPlugin,
type
SlashCombobox: Renders the slash command popup with predefined optionsimport { createEditor } from 'platejs/react';
import { SlashKit } from '@/components/editor/slash';
const editor = createEditor({
plugins: [
// ...otherPlugins,
...SlashKit,
],
});import { createEditor } from 'platejs/react';
import { SlashKit } from '@/components/editor/slash';
const editor = createEditor
The copied slash file defines SlashPlugin, which holds the trigger policy and the items your app adds. Mount the popup in its afterEditable slot:
import { createEditor } from 'platejs/react';
import { SlashCombobox, SlashPlugin } from '@/components/editor/slash';
const editor = createEditor({
plugins: [
// ...otherPlugins,
SlashPlugin.configure({ slots: { afterEditable: SlashCombobox } }),
],
});import { createEditor } from 'platejs/react';
import { SlashCombobox, SlashPlugin } from '@/components/editor/slash'
Its state is a ComboboxState plus items. A / opens the menu at the start of a line or after a space, and triggerQuery keeps a / typed in a code block as plain text. Edit SlashPlugin in the copied file to change either.
Pass your own items through initialState.items instead of editing the copied file. Each item names a group. It joins the built-in group with that name, such as Basic blocks, or starts a new group after the built-in ones.
import { PenLineIcon } from 'lucide-react';
import { createEditor } from 'platejs/react';
import { SlashCombobox, SlashPlugin } from '@/components/editor/slash';
const editor = createEditor({
plugins: [
// ...otherPlugins,
SlashPlugin.configure({
initialState: {
items: [
{
group: 'Basic blocks',
icon: () => <PenLineIcon />,
isAvailable: () => true,
keywords: ['sign'],
label: 'Signature',
onSelect: (_editor, tx) => {
tx.text.insert('Signed by the review team.');
},
},
],
},
slots: { afterEditable: SlashCombobox },
}),
],
});import { PenLineIcon } from 'lucide-react';
import { createEditor } from 'platejs/react';
import { SlashCombobox, SlashPlugin } from '@/components/editor/slash';
const editor = createEditor({
plugins: [
// ...otherPlugins,
SlashPlugin.configure({
initialState: {
items: [
{
group: 'Basic blocks',
icon: () => <PenLineIcon />,
isAvailable: () => true,
keywords: ['sign'],
label: 'Signature',
An item takes the fields a built-in one does: label, icon, isAvailable, optional keywords, and onSelect or onClick. Its icon is a function that returns the icon, such as () => <PenLineIcon />, because plugin state holds data and functions but not elements. onSelect runs inside the menu's transaction and replaces the typed / and query; return false to keep them.
How to use slash commands:
/ at the start of a line or after a space to open the slash menu/ and queryAvailable options include:
Use keywords to quickly find options. For example, type '/ul' for Bulleted List or '/h1' for Heading 1.
| API | Source | Use |
|---|---|---|
SlashPlugin | slash registry | Edit-only plugin whose state holds the / trigger policy, the code-block check and the app's items. |
SlashItem | slash registry | One app item: a built-in item's fields, with icon as a function, plus the group it joins. |
SlashKit | slash registry | SlashPlugin with SlashCombobox in its afterEditable slot. |
SlashCombobox | slash registry | The slash menu, built from the file's groups list and then the plugin's items. Edit groups to remove or reorder built-in options. While you type, the menu lists matches best first and keeps the list's order only between equal matches. |