Keyboard input plugin for Plate rich-text editor framework
npx @tessl/cli install tessl/npm-udecode--plate-kbd@49.0.0The @udecode/plate-kbd package provides keyboard input formatting support for the Plate rich-text editor framework. It enables rendering keyboard shortcuts and key combinations as styled inline elements within text content, making it ideal for technical documentation, tutorials, and help content that needs to clearly indicate keyboard shortcuts.
npm install @udecode/plate-kbdimport { BaseKbdPlugin } from "@udecode/plate-kbd";
// For React-specific usage
import { KbdPlugin } from "@udecode/plate-kbd/react";CommonJS:
const { BaseKbdPlugin } = require("@udecode/plate-kbd");
// For React-specific usage
const { KbdPlugin } = require("@udecode/plate-kbd/react");The package provides dual export structure:
@udecode/plate-kbd): Core plugin without React dependencies@udecode/plate-kbd/react): React-enhanced plugin versionBoth CommonJS (require) and ES modules (import) are supported.
// Import dependencies
import { createPlateEditor } from "@udecode/plate/react";
import { Plate } from "@udecode/plate/react";
import { KbdPlugin } from "@udecode/plate-kbd/react";
// Register the plugin with Plate
const editor = createPlateEditor({
plugins: [
// ... other plugins
KbdPlugin,
],
});
// Use in JSX
function MyEditor() {
return (
<Plate editor={editor}>
<div>
Press <kbd>Ctrl+C</kbd> to copy or <kbd>Cmd+V</kbd> to paste.
</div>
</Plate>
);
}Note: The createPlateEditor and Plate components are imported from @udecode/plate/react, not from this package.
The @udecode/plate-kbd package follows Plate's standard plugin architecture with two main components:
<kbd> elements through serialization/deserializationCore keyboard input formatting functionality for Plate editors. Provides leaf node support for inline keyboard shortcut rendering.
/**
* Base plugin for keyboard input formatting support.
* Enables inline keyboard shortcut rendering as leaf nodes.
*/
const BaseKbdPlugin: PlatePlugin;The BaseKbdPlugin is configured with:
'kbd' (from KEYS.kbd constant)isLeaf: true)<KBD> element deserialization<kbd> elementsReact-enhanced version of the base plugin with additional React-specific features and optimizations.
/**
* React-enhanced keyboard input plugin.
* Extends BaseKbdPlugin with React-specific features.
*/
const KbdPlugin: PlatePlugin;This plugin is created by wrapping BaseKbdPlugin with toPlatePlugin(), providing:
import { createPlateEditor } from "@udecode/plate/react";
import { KbdPlugin } from "@udecode/plate-kbd/react";
// Create editor with kbd support
const editor = createPlateEditor({
plugins: [KbdPlugin],
});
// The plugin will automatically render keyboard shortcuts
// when they are marked with the kbd formatting// The plugin automatically handles HTML kbd elements
const htmlContent = `
<p>Use <kbd>Ctrl+S</kbd> to save your document.</p>
`;
// This HTML will be properly deserialized with kbd formatting preservedimport { KEYS } from "@udecode/plate";
// The kbd plugin provides the KEYS.kbd constant for identification
const kbdKey = KEYS.kbd; // 'kbd'
// Mark manipulation requires the core Plate editor transforms
// These are not provided by this package but by @udecode/plate
const editor = useEditorRef();
editor.tf.toggleMark(KEYS.kbd);
// Check if current selection has kbd formatting
const isKbdActive = editor.tf.isMark(KEYS.kbd);Note: Transform methods (editor.tf.toggleMark, editor.tf.isMark) are provided by the core Plate framework, not by this package.
/**
* Keyboard input text type.
* Text node with kbd formatting mark.
*/
type TKbdElement = TText & { kbd: true };
/**
* Plugin configuration type.
* Standard Plate plugin interface.
*/
interface PlatePlugin {
key: string;
node?: {
isLeaf?: boolean;
};
parsers?: {
html?: {
deserializer?: {
rules?: Array<{ validNodeName: string[] }>;
};
};
};
render?: {
as?: string;
};
}
/**
* Base text node type (from @udecode/slate).
*/
interface TText {
text: string;
[key: string]: unknown;
}@udecode/plate (peer dependency >=49.0.0)react (>=18.0.0), react-dom (>=18.0.0)The kbd plugin works seamlessly with other Plate features:
<kbd> elementsCommon use cases for the kbd plugin:
The plugin follows Plate's standard error handling patterns. No specific exceptions are thrown by the kbd plugin itself, but standard Plate editor errors may occur during:
For troubleshooting, ensure:
<kbd> elements when deserializing