Typix LogoTypix
CoreComponents

EditorCommand

Slash command menu for inserting blocks and executing actions

EditorCommand provides a typeahead command menu (often used as a slash / menu). It filters options based on user input and handles keyboard navigation.

Import

import {
  EditorCommand,
  EditorCommandList,
  EditorCommandItem,
  EditorCommandEmpty,
  CommandMenuOption,
} from '@typix-editor/react';

Usage

import { $createHeadingNode, CommandMenuOption } from '@typix-editor/react';

const items = [
  new CommandMenuOption('Heading 1', {
    icon: <span>H1</span>,
    keywords: ['title', 'heading'],
    onSelect: (_, editor) => {
      editor.update(() => {
        const selection = $getSelection();
        if ($isRangeSelection(selection)) {
          $setBlocksType(selection, () => $createHeadingNode('h1'));
        }
      });
    },
  }),
  new CommandMenuOption('Bullet List', {
    icon: <span>-</span>,
    keywords: ['unordered', 'list'],
    onSelect: (_, editor) => {
      // Insert bullet list
    },
  }),
];

function MyEditor() {
  return (
    <EditorRoot>
      <EditorContent />
      <EditorCommand items={items}>
        <EditorCommandList>
          <EditorCommandEmpty>No results</EditorCommandEmpty>
          {items.map((item, index) => (
            <EditorCommandItem key={item.title} item={item} index={index}>
              {item.icon}
              <span>{item.title}</span>
            </EditorCommandItem>
          ))}
        </EditorCommandList>
      </EditorCommand>
    </EditorRoot>
  );
}

EditorCommand Props

PropTypeDefaultDescription
childrenReactNodeCommand list UI
itemsArray<CommandMenuOption>Available command options
triggerstring"/"Character that triggers the menu
classNamestringCSS class for the menu container
allowWhitespacebooleanfalseAllow spaces in the query string
minLengthnumber0Minimum query length before showing results
maxLengthnumber50Maximum query length

CommandMenuOption

Create command options with the CommandMenuOption class:

const option = new CommandMenuOption('Option Title', {
  icon: <Icon />,            // Optional icon element
  keywords: ['alias'],       // Extra search terms
  keyboardShortcut: 'Mod+1', // Display-only shortcut hint
  shortDescription: 'Desc',  // Brief description
  onSelect: (query, editor) => {
    // Execute when selected
  },
});

Sub-components

EditorCommandList

Wrapper for the list of command items. Renders inside the portal created by EditorCommand.

EditorCommandItem

Individual command item. Receives the item and index to handle highlighting and selection.

EditorCommandEmpty

Shown when no items match the current query.

See also

On this page