Back to Plate

Highlight

docs/(plugins)/(marks)/highlight.mdx

1.0.02.4 KB
Original Source
<ComponentPreview name="basic-marks-demo" /> <PackageInfo>

Features

  • Highlight important text with background colors
  • Keyboard shortcut support for quick formatting
  • Renders as <mark> HTML element by default
</PackageInfo>

Kit Usage

<Steps>

Installation

The fastest way to add the highlight plugin is with the BasicMarksKit, which includes pre-configured HighlightPlugin along with other basic marks and their Plate UI components.

<ComponentSource name="basic-marks-kit" />

Add Kit

Add the kit to your plugins:

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

const editor = createPlateEditor({
  plugins: [
    // ...otherPlugins,
    ...BasicMarksKit,
  ],
});
</Steps>

Manual Usage

<Steps>

Installation

bash
npm install @platejs/basic-nodes

Add Plugin

Include HighlightPlugin in your Plate plugins array when creating the editor.

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

const editor = createPlateEditor({
  plugins: [
    // ...otherPlugins,
    HighlightPlugin,
  ],
});

Configure Plugin

You can configure the HighlightPlugin with a custom component and keyboard shortcuts.

tsx
import { HighlightPlugin } from '@platejs/basic-nodes/react';
import { createPlateEditor } from 'platejs/react';
import { HighlightLeaf } from '@/components/ui/highlight-node';

const editor = createPlateEditor({
  plugins: [
    // ...otherPlugins,
    HighlightPlugin.configure({
      node: { component: HighlightLeaf },
      shortcuts: { toggle: { keys: 'mod+shift+h' } },
    }),
  ],
});
  • node.component: Assigns HighlightLeaf to render highlighted text elements.
  • shortcuts.toggle: Defines a keyboard shortcut to toggle highlight formatting.

Add Toolbar Button

You can add MarkToolbarButton to your Toolbar to toggle highlight formatting.

</Steps>

Plugins

HighlightPlugin

Plugin for highlight text formatting. Renders as <mark> HTML element by default.

Transforms

tf.highlight.toggle

Toggles the highlight formatting for the selected text.