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
Name | Type | Default | Description |
---|---|---|---|
editor | Editor | null | undefined | The Tiptap editor instance |
node | Node | null | undefined | The node to apply trigger to |
nodePos | number | null | undefined | The position of the node in the document |
text | string | undefined | Optional text label for the button |
trigger | string | "/" | The trigger text to insert |
hideWhenUnavailable | boolean | false | Hides the button when trigger insertion is not available |
onTriggered | (trigger: string) => void | undefined | Callback fired after a successful trigger insertion |
showShortcut | boolean | false | Shows 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
Name | Type | Default | Description |
---|---|---|---|
editor | Editor | null | undefined | The Tiptap editor instance |
node | Node | null | undefined | The node to apply trigger to |
nodePos | number | null | undefined | The position of the node in the document |
trigger | string | "/" | The trigger text to insert |
hideWhenUnavailable | boolean | false | Hides the button if trigger insertion is not available |
onTriggered | (trigger: string) => void | undefined | Callback fired after a successful trigger insertion |
Return Values
Name | Type | Description |
---|---|---|
isVisible | boolean | Whether the button should be rendered |
canInsert | boolean | If trigger insertion is currently allowed |
handleSlashCommand | () => boolean | Function to insert the slash command trigger |
label | string | Accessible label text for the button |
shortcutKeys | string | Keyboard shortcut (Cmd/Ctrl + /) |
trigger | string | The trigger text that will be inserted |
Icon | React.FC | Icon 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 integrationreact-hotkeys-hook
- Keyboard shortcut management
Referenced Components
use-tiptap-editor
(hook)button
(primitive)badge
(primitive)tiptap-utils
(lib)plus-icon
(icon)