Back to Plate

Italic

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

53.0.83.5 KB
Original Source

Italic applies the italic leaf mark to selected text. ItalicPlugin owns the mark key, shortcut, HTML parsing, render tag, and toggle transform.

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

Features

  • KEYS.italic leaf mark.
  • Default ⌘I / Ctrl+I shortcut.
  • HTML deserialization from em, i, and italic font style.
  • <em> rendering by default.
  • Optional Markdown-style input rules through ItalicRules.
  • Toolbar support through MarkToolbarButton.
</PackageInfo>

Kit Usage

<Steps>

Add Basic Marks

BasicMarksKit includes ItalicPlugin, * and _ Markdown-style input rules, and the standard toolbar buttons that use KEYS.italic.

<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 A Toolbar Button

Use MarkToolbarButton with KEYS.italic.

tsx
import { ItalicIcon } from 'lucide-react';
import { KEYS } from 'platejs';

import { MarkToolbarButton } from '@/components/ui/mark-toolbar-button';

export function ItalicToolbarButton() {
  return (
    <MarkToolbarButton nodeType={KEYS.italic} tooltip="Italic (⌘+I)">
      <ItalicIcon />
    </MarkToolbarButton>
  );
}
</Steps>

Manual Usage

Install the mark package.

bash
npm install @platejs/basic-nodes

Add ItalicPlugin directly when you do not want the whole kit.

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

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

Register Markdown-style input rules when users should type italic delimiters.

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

export const italicPlugin = ItalicPlugin.configure({
  inputRules: [
    ItalicRules.markdown({ variant: '*' }),
    ItalicRules.markdown({ variant: '_' }),
  ],
});

Ownership

SurfaceOwnerWhat It Does
BaseItalicPlugin@platejs/basic-nodesHeadless italic mark, HTML parser, render tag, and toggle transform.
ItalicPlugin@platejs/basic-nodes/reactReact wrapper with default mod+i shortcut.
ItalicRules.markdown@platejs/basic-nodesOptional mark input rule factory.
BasicMarksKitRegistryAdds ItalicPlugin with * and _ Markdown-style input rules.
MarkToolbarButtonRegistry UIReads active mark state and calls the mark toggle hook.

The package owns the mark. The registry owns toolbar placement and icon choice.

Behavior

BehaviorSource
Mark keyKEYS.italic
Leaf behaviornode.isLeaf: true
Toggle transformeditor.tf.italic.toggle() calls editor.tf.toggleMark(type).
ShortcutItalicPlugin registers mod+i.
HTML tagsem, i
HTML stylesfont-style: italic
HTML guardIgnores descendants where fontStyle is normal.
Render outputem

API Reference

APIPackageUse
BaseItalicPlugin@platejs/basic-nodesHeadless italic plugin.
ItalicPlugin@platejs/basic-nodes/reactReact italic plugin with shortcut defaults.
ItalicRules.markdown(options)@platejs/basic-nodesCreates an italic mark input rule.
tf.italic.toggle()@platejs/basic-nodesToggles the italic mark at the selection.