Back to Plate

Superscript

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

53.0.83.5 KB
Original Source

Superscript applies the superscript leaf mark to selected text. Its toggle transform removes subscript, so the same text cannot keep both vertical-position marks through the plugin API.

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

Features

  • KEYS.sup leaf mark.
  • Directional selection affinity.
  • HTML deserialization from sup and vertical-align: super.
  • <sup> rendering by default.
  • Optional Markdown-style input rule through SuperscriptRules.
  • Mutual exclusion with KEYS.sub.
</PackageInfo>

Kit Usage

<Steps>

Add Basic Marks

BasicMarksKit includes SuperscriptPlugin, the ^ input rule, and a mod+period shortcut.

<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 Control

The registry MoreToolbarButton toggles superscript from the More menu and removes subscript.

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 SuperscriptPlugin directly when you want the default <sup> render.

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

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

Configure the input rule and shortcut when you want the same behavior as the kit.

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

export const superscriptPlugin = SuperscriptPlugin.configure({
  inputRules: [SuperscriptRules.markdown()],
  shortcuts: { toggle: { keys: 'mod+period' } },
});

Ownership

SurfaceOwnerWhat It Does
BaseSuperscriptPlugin@platejs/basic-nodesHeadless superscript mark, HTML parser, render tag, selection rule, and toggle transform.
SuperscriptPlugin@platejs/basic-nodes/reactReact wrapper for the headless superscript mark.
SuperscriptRules.markdown@platejs/basic-nodesOptional ^ mark input rule factory.
BasicMarksKitRegistryAdds SuperscriptPlugin, the input rule, and mod+period.
MoreToolbarButtonRegistry UICalls editor.tf.toggleMark(KEYS.sup, { remove: KEYS.sub }).

The package owns superscript semantics. The registry owns shortcut configuration and toolbar placement.

Behavior

BehaviorSource
Mark keyKEYS.sup (superscript)
Leaf behaviornode.isLeaf: true
Toggle transformeditor.tf.superscript.toggle() calls editor.tf.toggleMark(type, { remove: editor.getType(KEYS.sub) }).
Selection affinitydirectional
HTML tagssup
HTML stylesvertical-align: super
Render outputsup
Kit input ruleSuperscriptRules.markdown()
Kit shortcutmod+period

API Reference

APIPackageUse
BaseSuperscriptPlugin@platejs/basic-nodesHeadless superscript plugin.
SuperscriptPlugin@platejs/basic-nodes/reactReact superscript plugin.
SuperscriptRules.markdown()@platejs/basic-nodesCreates the ^ mark input rule.
tf.superscript.toggle()@platejs/basic-nodesToggles superscript and removes subscript.