Plate
PlateEditorsCreateTemplates
GitHub16kGitHub
DiscordDiscord
    • Stream
    • Copilot
    • Prompt Editor
  • Comments
  • Discussion
  • Suggestions
    • Basic Blocks
      • Blockquote
      • Heading
      • Horizontal Rule
    • Board
    • Callout
    • Code Block
    • Column
    • Date
    • Equation
    • FieldExperimental
    • Link
    • Media
    • MentionElement
    • Table
    • Table of Contents
    • Footnote
    • Details
  • Marks
    • Bold
    • Italic
    • Underline
    • Code
    • Highlight
    • Keyboard Input
    • Strikethrough
    • Subscript
    • Superscript
      • Font
      • Line Height
      • Text Align
      • Clear Formatting
      • Format Painter
    • Indent
    • List
      • Exit Break
      • Single Block
      • Trailing Block
    • Autoformat
    • Block Menu
    • Block Placeholder
    • Combobox
      • Emoji
      • MentionElement
      • Slash Command
    • Drag & Drop
    • Navigation Feedback
    • Linting
    • Tabbable
    • Toolbar
    • Yjs
    • Multi SelectEditor
    • DOCX
    • HTML
    • Markdown

Clear Formatting

PreviousNext

Remove text and block styles from a selection.

Clear Formatting DemoClear Formatting Toolbar Button

FormattingPlugin removes text styles inside the selection and block styles from the blocks the selection fully covers. Headings, lists, links and quotes keep their shape.

Loading…
Text AlignFormat Painter

On This Page

FeaturesKit usageAdd the kitAdd the toolbar buttonManual usageInstall packageAdd the pluginClear formattingBehaviorBlock styles from other pluginsOwnershipAPI ReferenceOptions
Build your editor
Production-ready AI template and reusable components.
Get all-access

Features

  • Removes every text style inside the selection, such as bold, color and highlight, in one undo step.
  • Resets alignment, line height, text indent and indent on blocks the selection fully covers.
  • Keeps list depth, heading levels, links, comments and suggestions.
  • At a caret, clears the styles the next typed text would get.
  • Mod+\ shortcut and a bound editor.update.formatting.clear method.
  • formatting plugin point for block styles that other plugins add.
Report an issue

Kit usage

Add the kit

FormattingKit adds FormattingPlugin with its Mod+\ shortcut. It clears what the other style kits in the editor add, so place it next to them.

'use client';
 
import { FormattingPlugin } from 'platejs/react';
 
export const FormattingKit = [FormattingPlugin] as const;
'use client';
 
import { FormattingPlugin } from 'platejs/react';
 
export const FormattingKit = [FormattingPlugin] as const;
import { createEditor } from "platejs/react";
 
import { FormattingKit } from "@/components/editor/formatting";
 
export const editor = createEditor({
  plugins: [...FormattingKit],
});
import { createEditor } from "platejs/react";
 
import { FormattingKit } from "@/components/editor/formatting";
 
export const editor = createEditor({

Add the toolbar button

Install ClearFormattingToolbarButton when the editor needs a toolbar control.

pnpm dlx shadcn@latest add @plate/clear-formatting-toolbar-button
pnpm dlx shadcn@latest add @plate/clear-formatting-toolbar-button
import { ClearFormattingToolbarButton } from "@/components/editor/clear-formatting-toolbar-button";
 
export function FormattingControls() {
  return <ClearFormattingToolbarButton />;
}
import { ClearFormattingToolbarButton } from "@/components/editor/clear-formatting-toolbar-button";
 
export function FormattingControls() {
  return <ClearFormattingToolbarButton />;
}

Manual usage

Install package

pnpm add platejs
pnpm add platejs

Add the plugin

import { createEditor, FormattingPlugin } from "platejs/react";
 
export const editor = createEditor({
  plugins: [FormattingPlugin],
});
import { createEditor, FormattingPlugin } from "platejs/react";
 
export const editor = createEditor({
  plugins: [FormattingPlugin],
});

BaseFormattingPlugin from platejs is the same plugin without the shortcut, for headless editors.

Clear formatting

clear works on the selection, or on the range you pass as at.

editor.update.formatting.clear();
editor.update.formatting.clear({
  at: {
    anchor: { offset: 0, path: [0, 0] },
    focus: { offset: 5, path: [0, 0] },
  },
});
editor.update.formatting.clear();
editor.update.formatting.clear({
  at: {
    anchor: { offset: 0, path: [0, 0] },
    focus: { offset: 

Behavior

SelectionResult
Part of a blockRemoves text styles from the selected text. Block styles stay.
Whole blocksAlso removes textAlign, lineHeight, textIndent and indent from each fully covered block. A block the selection covers only in part keeps its block styles.
A list itemKeeps indent, because it is the list depth. A paragraph that continues a list item keeps it too.
Headings, quotes, code blocksKeep their type. A heading stays a heading at its level.
A linkKeeps the link and its url. Text styles inside it go.
An inline voidA mention loses its text styles. An inline void that takes no text styles, such as a date, keeps what it stores.
A block selectionClears every selected block as if its whole text were selected.
A caretRemoves the pending styles, so the next typed text is plain. The document does not change and no undo step is added.
Suggesting modeRecords the removed styles as suggestions. A code block refuses changes while suggesting, so a clear whose selection reaches one is refused and changes nothing.

One clear is one undo step, whatever it removes.

The format painter uses the same rules to copy a look from one piece of text onto another.

A text style is any text property whose schema role is content. Comments, suggestions and other metadata properties are not styles, so they stay.

Block styles from other plugins

A plugin adds its own block styles through the formatting plugin point. block lists the element properties clear formatting removes from a fully covered block. keeps lets a block keep a property when it means structure there.

import { definePlugin, formatting } from "platejs";
 
export const ShadePlugin = definePlugin("shade", {
  contributions: [
    formatting.of({
      block: ["shade"],
      keeps: {
        shade: ({ node }) => node.type === "callout",
      },
    }),
  ],
});
import { definePlugin, formatting } from "platejs";
 
export const ShadePlugin = definePlugin("shade", {
  contributions: [
    formatting.of({
      block: ["shade"],
      keeps: {
        shade: ({ node }) => node.type === "callout",
      },
    }),
  ],
});

The line height, text align, text indent and indent plugins contribute their keys this way. The list plugin keeps indent on list items.

Ownership

SurfaceOwnerWhat It Does
BaseFormattingPluginplatejsHeadless plugin with the clear update method.
FormattingPluginplatejs/reactReact plugin with the Mod+\ shortcut.
formattingplatejsPlugin point for block styles and the blocks that keep them.
FormattingKitRegistry kitAdds FormattingPlugin.
ClearFormattingToolbarButtonRegistry UIToolbar button that calls formatting.clear and shows the shortcut.

API Reference

APIPackageUse
FormattingPluginplatejs/reactReact clear formatting plugin.
BaseFormattingPluginplatejsHeadless clear formatting plugin.
editor.update.formatting.clear(options?)platejsClears formatting in the selection or options.at.
tx.formatting.clear(options?)platejsTransaction form for a larger atomic update.
formatting.of(contribution)platejsContributes block keys and keeps predicates from a plugin.

Options

OptionSurfaceUse
atformatting.clearRange to clear. Defaults to the selection.
blockformatting.ofElement properties to remove from fully covered blocks.
keepsformatting.ofPer property, a predicate given { index, node, path, siblings } that keeps the property on that block.
ShortcutAction
Mod+\Clear formatting.
plugins: [...FormattingKit],
});
5
, path: [
0
,
0
] },
},
});