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

Examples

⚡ Slash Command

The slash command provides quick access to various formatting options and content types. To use it, type '/' anywhere in your document and explore the available options.
Key features of the slash command:
  • Quick insertion of various block types
  • Easy access to inline formatting options
  • Integration with AI-assisted writing
  • Can be triggered anywhere in the document
  • Keyboard navigation support
import React, { type ComponentType, type SVGProps } from 'react';
 
import type { PlateEditor } from '@udecode/plate-common/react';
 
import { withRef } from '@udecode/cn';
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';
 
interface SlashCommandRule {
  icon: ComponentType<SVGProps<SVGSVGElement>>;
  onSelect: (editor: PlateEditor) => void;
  value: string;
  focusEditor?: boolean;
  keywords?: string[];
}
 
const rules: SlashCommandRule[] = [
  {
    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(({ icon: Icon, keywords, value, onSelect }) => (
              <InlineComboboxItem
                key={value}
                value={value}
                onClick={() => onSelect(editor)}
                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!