Back to Plate

Keyboard Input

content/docs/(plugins)/(marks)/kbd.mdx

53.0.83.4 KB
Original Source

Keyboard Input applies the kbd leaf mark to selected text. Use it for shortcuts, key names, and command hints that should render as <kbd>.

<ComponentPreview name="basic-marks-demo" /> <PackageInfo>

Features

  • KEYS.kbd leaf mark.
  • Hard selection affinity.
  • HTML deserialization from kbd.
  • <kbd> rendering by default.
  • Registry KbdLeaf for styled keyboard input.
  • Static rendering support through KbdLeafStatic.
</PackageInfo>

Kit Usage

<Steps>

Add Basic Marks

BasicMarksKit includes KbdPlugin.withComponent(KbdLeaf). It does not add a shortcut or input rule for keyboard input.

<ComponentSource name="basic-marks-kit" />
tsx
import { createPlateEditor } from 'platejs/react';

import { BasicMarksKit } from '@/components/editor/plugins/basic-marks-kit';

export const editor = createPlateEditor({
  plugins: [...BasicMarksKit],
});

Add the Leaf

KbdLeaf provides the registry styling for the <kbd> element.

<ComponentSource name="kbd-node" />

Add a Toolbar Control

The registry MoreToolbarButton toggles keyboard input from the More menu.

tsx
import { MoreToolbarButton } from '@/components/ui/more-toolbar-button';

export function FormattingMoreMenu() {
  return <MoreToolbarButton />;
}
</Steps>

Manual Usage

Install the mark package.

bash
npm install @platejs/basic-nodes

Add KbdPlugin directly when you want the default <kbd> render.

tsx
import { KbdPlugin } from '@platejs/basic-nodes/react';
import { createPlateEditor } from 'platejs/react';

export const editor = createPlateEditor({
  plugins: [KbdPlugin],
});

Attach the registry leaf when you want the same visual treatment as the kit.

tsx
import { KbdPlugin } from '@platejs/basic-nodes/react';

import { KbdLeaf } from '@/components/ui/kbd-node';

export const kbdPlugin = KbdPlugin.withComponent(KbdLeaf);

Ownership

SurfaceOwnerWhat It Does
BaseKbdPlugin@platejs/basic-nodesHeadless keyboard-input mark, HTML parser, render tag, selection rule, and toggle transform.
KbdPlugin@platejs/basic-nodes/reactReact wrapper for the headless keyboard-input mark.
KbdLeafRegistry UIStyled client <kbd> leaf using PlateLeaf.
KbdLeafStaticRegistry UIStatic rendering version using SlateLeaf.
BasicMarksKitRegistryAdds KbdPlugin.withComponent(KbdLeaf).
MoreToolbarButtonRegistry UICalls editor.tf.toggleMark(KEYS.kbd) from the More menu.

The package owns the mark. The registry owns keycap styling and toolbar placement.

Behavior

BehaviorSource
Mark keyKEYS.kbd
Leaf behaviornode.isLeaf: true
Toggle transformeditor.tf.kbd.toggle() calls editor.tf.toggleMark(type).
Selection affinityhard
HTML tagskbd
Render outputkbd
Kit componentKbdLeaf
Registry toolbarMoreToolbarButton toggles KEYS.kbd.

API Reference

APIPackageUse
BaseKbdPlugin@platejs/basic-nodesHeadless keyboard-input plugin.
KbdPlugin@platejs/basic-nodes/reactReact keyboard-input plugin.
tf.kbd.toggle()@platejs/basic-nodesToggles the keyboard-input mark at the selection.
KbdLeafRegistry UIStyled client keyboard-input leaf.
KbdLeafStaticRegistry UIStyled static keyboard-input leaf.