āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š shadcn/directory/udecode/plate/(guides)/plugin ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
Plate plugins are highly configurable, allowing you to customize their behavior to suit your needs. This guide will walk you through the most common configuration options and how to use them.
The most basic plugin configuration requires only a key:
const MyPlugin = createPlatePlugin({
key: 'minimal',
});
While this plugin doesn't do anything yet, it's a starting point for more complex configurations.
The .configure method allows you to configure an existing plugin:
const ConfiguredPlugin = MyPlugin.configure({
options: {
myOption: 'new value',
},
});
Node plugins are used to define new types of nodes in your editor using the node property. These can be elements (either block or inline) or leaf nodes (for text-level formatting).
To create a new type of element, use the node.isElement option:
const ParagraphPlugin = createPlatePlugin({
key: 'p',
node: {
isElement: true,
type: 'p',
},
});
You can associate a component with your element. See Plugin Components for more information.
const ParagraphPlugin = createPlatePlugin({
key: 'p',
node: {
isElement: true,
type: 'p',
component: ParagraphElement,
},
});
For inline elements, void elements, or leaf nodes, use the appropriate node options:
const LinkPlugin = createPlatePlugin({
key: 'link',
node: {
isElement: true,
isInline: true,
type: 'a',
},
});
const ImagePlugin = createPlatePlugin({
key: 'image',
node: {
isElement: true,
isVoid: true,
type: 'img',
},
});
const BoldPlugin = createPlatePlugin({
key: 'bold',
node: {
isLeaf: true,
},
});
Rather than render an element or a mark, you may want to customize the behavior of your editor. Various plugin options are available to modify the behavior of Plate.
The rules property allows you to configure common editing behaviors like breaking, deleting, and merging nodes without overriding editor methods. This is a powerful way to define intuitive interactions for your custom elements.
For example, you can define what happens when a user presses Enter in an empty heading, or Backspace at the start of a blockquote.
import { H1Plugin } from '@platejs/heading/react';
H1Plugin.configure({
rules: {
break: { empty: 'reset' },
},
});
See the Plugin Rules guide for a complete list of available rules and actions.
The recommended way to respond to user-generated events from inside a plugin is with the handlers plugin option. A handler should be a function that takes a PlatePluginContext & { event } object.
The onChange handler, which is called when the editor value changes, is an exception to this rule; the context object includes the changed value instead of event.
const ExamplePlugin = createPlatePlugin({
key: 'example',
handlers: {
onChange: ({ editor, value }) => {
console.info(editor, value);
},
onKeyDown: ({ editor, event }) => {
console.info(`You pressed ${event.key}`);
},
},
});
You may want to inject a class name or CSS property into any node having a certain property. For example, the following plugin sets the textAlign CSS property on paragraphs with an align property.
import { KEYS } from 'platejs';
const TextAlignPlugin = createPlatePlugin({
key: 'align',
inject: {
nodeProps: {
defaultNodeValue: 'start',
nodeKey: 'align',
styleKey: 'textAlign',
validNodeValues: ['start', 'left', 'center', 'right', 'end', 'justify'],
},
targetPlugins: [KEYS.p],
// This is injected into all `targetPlugins`. In this example, ParagraphPlugin will be able to deserialize `textAlign` style.
targetPluginToInject: ({ editor, plugin }) => ({
parsers: {
html: {
deserializer: {
parse: ({ element, node }) => {
if (element.style.textAlign) {
node[editor.getType('align')] = element.style.textAlign;
}
},
},
},
},
}),
},
});
A paragraph node affected by the above plugin would look like this:
{
type: 'p',
align: 'right',
children: [{ text: 'This paragraph is aligned to the right!' }],
}
The overrideEditor method provides a way to override existing editor methods while maintaining access to the original implementations. This is particularly useful when you want to modify the behavior of core editor functionality.
const CustomPlugin = createPlatePlugin({
key: 'custom',
}).overrideEditor(({ editor, tf: { deleteForward }, api: { isInline } }) => ({
// Override transforms
transforms: {
deleteForward(options) {
// Custom logic before deletion
console.info('Deleting forward...');
// Call original transform
deleteForward(options);
// Custom logic after deletion
console.info('Deleted forward');
},
},
// Override API methods
api: {
isInline(element) {
// Custom inline element check
if (element.type === 'custom-inline') {
return true;
}
// Fall back to original behavior
return isInline(element);
},
},
}));
tf (transforms) and api parametersExample with typed options:
type CustomConfig = PluginConfig<
'custom',
{ allowDelete: boolean }
>;
const CustomPlugin = createTPlatePlugin<CustomConfig>({
key: 'custom',
options: { allowDelete: true },
}).overrideEditor(({ editor, tf: { deleteForward }, getOptions }) => ({
transforms: {
deleteForward(options) {
// Use typed options to control behavior
if (!getOptions().allowDelete) {
return;
}
deleteForward(options);
},
},
}));
You can extend the editor for complex functionality. To do this, you can use the extendEditor plugin option to directly mutate properties of the editor object after its creation.
const CustomNormalizerPlugin = createPlatePlugin({
key: 'customNormalizer',
extendEditor: ({ editor }) => {
editor.customState = true;
return editor;
},
});
<Callout type="info" title="Difference between extendEditor and overrideEditor">
- Use `extendEditor` when integrating legacy Slate plugins like `withYjs` that need direct editor mutation. There is only one `extendEditor` per plugin.
- Prefer using `overrideEditor` for modifying editor behavior as it has single purpose responsibility and better type safety. It can be called multiple times to layer different overrides.
</Callout>
Each plugin has its own store, which can be used to manage plugin-specific state.
const MyPlugin = createPlatePlugin({
key: 'myPlugin',
options: {
count: 0,
},
}).extend(({ editor, plugin, setOption }) => ({
handlers: {
onClick: () => {
setOption('count', 1);
},
},
}));
You can access and update the store using the following methods:
// Get the current value
const count = editor.getOption(MyPlugin, 'count');
// Set a new value
editor.setOption(MyPlugin, 'count', 5);
// Update the value based on the previous state
editor.setOption(MyPlugin, 'count', (prev) => prev + 1);
In React components, you can use the usePluginOption or usePluginOptions hook to subscribe to store changes:
const MyComponent = () => {
const count = usePluginOption(MyPlugin, 'count');
return <div>Count: {count}</div>;
};
See more in Plugin Context and Editor Methods guides.
You can specify plugin dependencies using the dependencies property. This ensures that the required plugins are loaded before the current plugin.
const MyPlugin = createPlatePlugin({
key: 'myPlugin',
dependencies: ['paragraphPlugin', 'listPlugin'],
});
The enabled property allows you to conditionally enable or disable a plugin:
const MyPlugin = createPlatePlugin({
key: 'myPlugin',
enabled: true, // or false to disable
});
Plate supports nested plugins, allowing you to create plugin hierarchies. Use the plugins property to define child plugins:
const ParentPlugin = createPlatePlugin({
key: 'parent',
plugins: [
createPlatePlugin({ key: 'child1' }),
createPlatePlugin({ key: 'child2' }),
],
});
The priority property determines the order in which plugins are registered and executed. Plugins with higher priority values are processed first:
const HighPriorityPlugin = createPlatePlugin({
key: 'highPriority',
priority: 100,
});
const LowPriorityPlugin = createPlatePlugin({
key: 'lowPriority',
priority: 50,
});
This is particularly useful when you need to ensure certain plugins are initialized or run before others.
The parsers property accepts string keys to build your own parsers:
const MyPlugin = createPlatePlugin({
key: 'myPlugin',
parsers: {
myCustomParser: {
deserializer: {
parse: // ...
},
serializer: {
parse: // ...
}
},
},
});
Core plugins includes html and htmlReact parsers.
Using above methods, plugin types are automatically inferred from the given configuration.
If you need to pass an explicit type as generic, you can use createTPlatePlugin.
The createTPlatePlugin function allows you to create a typed plugin:
type CodeBlockConfig = PluginConfig<
// key
'code_block',
// options
{ syntax: boolean; syntaxPopularFirst: boolean },
// api
{
plugin: {
getSyntaxState: () => boolean;
};
toggleSyntax: () => void;
},
// transforms
{
insert: {
codeBlock: (options: { language: string }) => void;
}
}
>;
const CodeBlockPlugin = createTPlatePlugin<CodeBlockConfig>({
key: 'code_block',
options: { syntax: true, syntaxPopularFirst: false },
}).extendEditorApi<CodeBlockConfig['api']>(() => ({
plugin: {
getSyntaxState: () => true,
},
toggleSyntax: () => {},
})).extendEditorTransforms<CodeBlockConfig['transforms']>(() => ({
insert: {
codeBlock: ({ editor, getOptions }) => {
editor.tf.insertBlock({ type: 'code_block', language: getOptions().language });
},
},
}));
When using typed plugins, you get full type checking and autocompletion āØ
const editor = createPlateEditor({
plugins: [ExtendedCodeBlockPlugin],
});
// Type-safe access to options
const options = editor.getOptions(ExtendedCodeBlockPlugin);
options.syntax;
options.syntaxPopularFirst;
options.hotkey;
// Type-safe API
editor.api.toggleSyntax();
editor.api.plugin.getSyntaxState();
editor.api.plugin2.setLanguage('python');
editor.api.plugin.getLanguage();
// Type-safe Transforms
editor.tf.insert.codeBlock({ language: 'typescript' });
See the PlatePlugin API for more plugin options.
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā