Components
Slash Input Element

Slash Input Element

Allows you to insert various elements into your editor using a slash command.

Installation

npx @udecode/plate-ui@latest add slash-input-element -r plate-ui

Examples

Slash Menu

The slash menu provides quick access to various formatting options and content types.
How to use the slash menu:
  • Type '/' anywhere in your document to open the slash menu.
  • Start typing to filter options or use arrow keys to navigate.
  • Press Enter or click to select an option.
  • Press Escape to close the menu without selecting.
Available options include:
  • Headings: Heading 1, Heading 2, Heading 3
  • Lists: Bulleted list, Numbered list
  • Inline Elements: Date
import React from 'react';
import { withRef } from '@udecode/cn';
import { AIChatPlugin } from '@udecode/plate-ai/react';
import { DatePlugin } from '@udecode/plate-date/react';
import { HEADING_KEYS } from '@udecode/plate-heading';
import { ListStyleType, toggleIndentList } from '@udecode/plate-indent-list';
 
import { Icons } from '@/components/icons';
 
import {
  InlineCombobox,
  InlineComboboxContent,
  InlineComboboxEmpty,
  InlineComboboxInput,
  InlineComboboxItem,
} from './inline-combobox';
import { PlateElement } from './plate-element';
 
import type { ComponentType, SVGProps } from 'react';
import type { PlateEditor } from '@udecode/plate-common/react';
 
interface SlashCommandRule {
  icon: ComponentType<SVGProps<SVGSVGElement>>;
  onSelect: (editor: PlateEditor) => void;
  value: string;
  className?: string;
  focusEditor?: boolean;
  keywords?: string[];
}
 
const rules: SlashCommandRule[] = [
  {
    focusEditor: false,
    icon: Icons.ai,
    value: 'AI',
    onSelect: (editor) => {
      editor.getApi(AIChatPlugin).aiChat.show();
    },
  },
  {
    icon: Icons.h1,
    value: 'Heading 1',
    onSelect: (editor) => {
      editor.tf.toggle.block({ type: HEADING_KEYS.h1 });
    },
  },
  {
    icon: Icons.h2,
    value: 'Heading 2',
    onSelect: (editor) => {
      editor.tf.toggle.block({ type: HEADING_KEYS.h2 });
    },
  },
  {
    icon: Icons.h3,
    value: 'Heading 3',
    onSelect: (editor) => {
      editor.tf.toggle.block({ type: HEADING_KEYS.h3 });
    },
  },
  {
    icon: Icons.ul,
    keywords: ['ul', 'unordered list'],
    value: 'Bulleted list',
    onSelect: (editor) => {
      toggleIndentList(editor, {
        listStyleType: ListStyleType.Disc,
      });
    },
  },
  {
    icon: Icons.ol,
    keywords: ['ol', 'ordered list'],
    value: 'Numbered list',
    onSelect: (editor) => {
      toggleIndentList(editor, {
        listStyleType: ListStyleType.Decimal,
      });
    },
  },
  {
    icon: Icons.add,
    keywords: ['inline', 'date'],
    value: 'Date',
    onSelect: (editor) => {
      editor.getTransforms(DatePlugin).insert.date();
    },
  },
];
 
export const SlashInputElement = withRef<typeof PlateElement>(
  ({ className, ...props }, ref) => {
    const { children, editor, element } = props;
 
    return (
      <PlateElement
        ref={ref}
        as="span"
        data-slate-value={element.value}
        {...props}
      >
        <InlineCombobox element={element} trigger="/">
          <InlineComboboxInput />
 
          <InlineComboboxContent>
            <InlineComboboxEmpty>
              No matching commands found
            </InlineComboboxEmpty>
 
            {rules.map(
              ({ focusEditor, icon: Icon, keywords, value, onSelect }) => (
                <InlineComboboxItem
                  key={value}
                  value={value}
                  onClick={() => onSelect(editor)}
                  focusEditor={focusEditor}
                  keywords={keywords}
                >
                  <Icon className="mr-2 size-4" aria-hidden />
                  {value}
                </InlineComboboxItem>
              )
            )}
          </InlineComboboxContent>
        </InlineCombobox>
 
        {children}
      </PlateElement>
    );
  }
);

Plate Plus

We offer an enhanced user interface design and a more comprehensive set of options, including premium plugins such as Math Callout and Media Upload. This provides a more robust and feature-rich editing experience for users who require advanced functionality.

Some key improvements in Plate Plus include:

  • Refined UI design for better usability and aesthetics
  • Extended set of slash menu options
  • Integration of premium plugins like Math Upload for specialized editing needs
  • No need to worry about the focus issue mentioned above.
  • Support grouping and Carefully selected keyword.
  • Trigger slash menu by click the puls button on the left of the paragraph.

You can try typing /``` to quickly insert a code block.

Feel free to experiment with different commands to see how the slash menu enhances your editing experience!