Find out what's new in Tiptap Editor 3.0

Slash Command Trigger Button

A fully accessible slash command trigger button for Tiptap editors. Easily insert slash command triggers to initiate block-level command menus with keyboard shortcut support and flexible customization options.

Installation

Add the component via the Tiptap CLI:

npx @tiptap/cli@latest add slash-command-trigger-button

Components

<SlashCommandTriggerButton />

A prebuilt React component that inserts slash command triggers into the editor.

Usage

<SlashCommandTriggerButton
  editor={editor}
  text="Insert Block"
  trigger="/"
  hideWhenUnavailable={true}
  showShortcut={true}
  onTriggered={(trigger) => console.log('Inserted:', trigger)}
/>

Props

NameTypeDefaultDescription
editorEditor | nullundefinedThe Tiptap editor instance
nodeNode | nullundefinedThe node to apply trigger to
nodePosnumber | nullundefinedThe position of the node in the document
textstringundefinedOptional text label for the button
triggerstring"/"The trigger text to insert
hideWhenUnavailablebooleanfalseHides the button when trigger insertion is not available
onTriggered(trigger: string) => voidundefinedCallback fired after a successful trigger insertion
showShortcutbooleanfalseShows a keyboard shortcut badge (if available)

Hooks

useSlashCommandTrigger()

A custom hook to build your own slash command trigger button with full control over rendering and behavior.

Usage

function MySlashCommandButton() {
  const { isVisible, canInsert, handleSlashCommand, label, shortcutKeys, trigger, Icon } =
    useSlashCommandTrigger({
      editor: myEditor,
      trigger: '/',
      hideWhenUnavailable: true,
      onTriggered: (trigger) => console.log('Inserted:', trigger),
    })

  if (!isVisible) return null

  return (
    <button onClick={handleSlashCommand} disabled={!canInsert} aria-label={label}>
      <Icon />
      {label}
      {shortcutKeys && <Badge>{parseShortcutKeys({ shortcutKeys })}</Badge>}
    </button>
  )
}

Props

NameTypeDefaultDescription
editorEditor | nullundefinedThe Tiptap editor instance
nodeNode | nullundefinedThe node to apply trigger to
nodePosnumber | nullundefinedThe position of the node in the document
triggerstring"/"The trigger text to insert
hideWhenUnavailablebooleanfalseHides the button if trigger insertion is not available
onTriggered(trigger: string) => voidundefinedCallback fired after a successful trigger insertion

Return Values

NameTypeDescription
isVisiblebooleanWhether the button should be rendered
canInsertbooleanIf trigger insertion is currently allowed
handleSlashCommand() => booleanFunction to insert the slash command trigger
labelstringAccessible label text for the button
shortcutKeysstringKeyboard shortcut (Cmd/Ctrl + /)
triggerstringThe trigger text that will be inserted
IconReact.FCIcon component for the slash command button

Utilities

canInsertSlashCommand(editor, node?, nodePos?)

Checks if a slash command trigger can be inserted in the current editor state or at a specific position.

import { canInsertSlashCommand } from '@/registry/tiptap-ui/slash-command-trigger-button'

const canInsert = canInsertSlashCommand(editor) // Check current position
const canInsertAtNode = canInsertSlashCommand(editor, node, nodePos) // Check specific position

insertSlashCommand(editor, trigger?, node?, nodePos?)

Programmatically inserts a slash command trigger in the editor.

import { insertSlashCommand } from '@/registry/tiptap-ui/slash-command-trigger-button'

// Insert at current position
const success = insertSlashCommand(editor, '/')

// Insert at specific node/position
const success2 = insertSlashCommand(editor, '+', node, nodePos)

// Insert with custom trigger
const success3 = insertSlashCommand(editor, '\\')

if (success) {
  console.log('Slash command trigger inserted successfully!')
}

Keyboard Shortcuts

The slash command trigger button supports the following keyboard shortcut:

  • Cmd/Ctrl + /: Insert slash command trigger

The shortcut is automatically registered when using either the <SlashCommandTriggerButton /> component or the useSlashCommandTrigger() hook.

Requirements

Dependencies

  • @tiptap/react - Core Tiptap React integration
  • react-hotkeys-hook - Keyboard shortcut management

Referenced Components

  • use-tiptap-editor (hook)
  • button (primitive)
  • badge (primitive)
  • tiptap-utils (lib)
  • plus-icon (icon)