Back to Plate

Text Align

content/docs/(plugins)/(styles)/text-align.mdx

53.0.84.9 KB
Original Source

TextAlignPlugin stores block alignment in an align property and renders it as CSS text-align. Setting the default start value removes the property from matching blocks.

<ComponentPreview name="align-demo" /> <PackageInfo>

Features

  • Block-level text alignment.
  • start, left, center, right, end, and justify values.
  • HTML text-align style deserialization.
  • Default-value cleanup.
  • Configurable target block types.
  • Registry dropdown toolbar for common alignments.
</PackageInfo>

Kit Usage

<Steps>

Add the Kit

Use AlignKit for the Plate UI setup. It targets headings, paragraphs, images, and media embeds.

<ComponentSource name="align-kit" />
tsx
import { createPlateEditor } from 'platejs/react';

import { AlignKit } from '@/components/editor/plugins/align-kit';

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

Add the Toolbar Control

AlignToolbarButton shows left, center, right, and justify controls.

tsx
import { AlignToolbarButton } from '@/components/ui/align-toolbar-button';

export function AlignControls() {
  return <AlignToolbarButton />;
}
</Steps>

Manual Usage

<Steps>

Install Package

bash
npm install @platejs/basic-styles

Add the Plugin

Configure the blocks that can store alignment.

tsx
import { TextAlignPlugin } from '@platejs/basic-styles/react';
import { KEYS } from 'platejs';
import { createPlateEditor } from 'platejs/react';

export const editor = createPlateEditor({
  plugins: [
    TextAlignPlugin.configure({
      inject: {
        nodeProps: {
          defaultNodeValue: 'start',
          nodeKey: 'align',
          styleKey: 'textAlign',
          validNodeValues: [
            'start',
            'left',
            'center',
            'right',
            'end',
            'justify',
          ],
        },
        targetPlugins: [...KEYS.heading, KEYS.p],
      },
    }),
  ],
});

Set Alignment

Use the bound transform or the headless utility.

tsx
import { setAlign } from '@platejs/basic-styles';

editor.tf.textAlign.setNodes('center');

setAlign(editor, 'start', { at: [] });
</Steps>

Ownership

SurfaceOwnerWhat It Does
BaseTextAlignPlugin@platejs/basic-stylesHeadless plugin that stores alignment, injects block props, and parses HTML text-align styles.
TextAlignPlugin@platejs/basic-styles/reactReact wrapper around BaseTextAlignPlugin.
setAlign@platejs/basic-stylesSets or clears alignment on matching blocks.
tf.textAlign.setNodes@platejs/basic-stylesBound transform exposed by the plugin.
BaseAlignKitRegistry kitStatic/headless setup for headings, paragraphs, images, and media embeds.
AlignKitRegistry kitReact setup plus toolbar dependency.
AlignToolbarButtonRegistry UIDropdown that writes alignment through textAlign.setNodes.

The package owns alignment storage and parsing. The registry owns the dropdown UI.

Behavior

BehaviorSource
Plugin keyKEYS.textAlign
Stored propertyalign
Rendered CSS styletextAlign
Default target plugins[KEYS.p]
Registry target plugins[...KEYS.heading, KEYS.p, KEYS.img, KEYS.mediaEmbed]
Default valuestart
Valid valuesstart, left, center, right, end, justify
HTML parserReads element.style.textAlign.
Setting a custom valueSets { align: value } on matching blocks.
Setting startUnsets the stored alignment property.
Non-target blocksAre ignored by setAlign.

HTML

The plugin injects an HTML deserializer into each target plugin. Pasted HTML with a text-align style becomes an align prop on matching blocks.

html
<p style="text-align: center">Centered text</p>

API Reference

APIPackageUse
TextAlignPlugin@platejs/basic-styles/reactReact alignment plugin.
BaseTextAlignPlugin@platejs/basic-stylesHeadless alignment plugin.
editor.tf.textAlign.setNodes(value, options?)@platejs/basic-stylesSets or clears alignment on matching blocks.
setAlign(editor, value, options?)@platejs/basic-stylesHeadless transform behind the bound API.

Options

OptionSurfaceUse
inject.targetPluginsTextAlignPluginBlock types that can keep align.
inject.nodeProps.nodeKeyTextAlignPluginStored block property; registry kits use align.
inject.nodeProps.defaultNodeValueTextAlignPluginValue that clears the stored property when selected.
inject.nodeProps.styleKeyTextAlignPluginCSS property used for rendering.
inject.nodeProps.validNodeValuesRegistry toolbarValues exposed to alignment UI.
SetNodesOptionssetAlignSlate node update options passed to setNodes or unsetNodes.