Documentation

Text alignment

Align paragraphs and list items.

Try it

Put the caret in “The toolbar has the floor.” and click Center. The paragraph has style="text-align: center" in HTML and attrs.textAlign is "center" in JSON.

Live example
Example result

Add it to your editor

Enable TextAlign to change alignment on selected blocks. This example wires up left, center, right, and justified alignment with command-specific pressed states.

import {
  EditorContent, EditorProvider, TextAlign, useEditor, useIsActive,
  type EditorInstance,
} from 'yurich';

const choices = [
  ['alignLeft', "Left"],
  ['alignCenter', "Center"],
  ['alignRight', "Right"],
  ['alignJustify', "Justify"],
] as const;

function AlignButton({ editor, command, label }: {
  editor: EditorInstance;
  command: (typeof choices)[number][0];
  label: string;
}) {
  const active = useIsActive(editor, command);

  return (
    <button
      type="button"
      aria-pressed={active}
      onMouseDown={event => event.preventDefault()}
      onClick={() => editor.execute(command)}
    >
      {label}
    </button>
  );
}

export function AlignmentEditor() {
  const editor = useEditor({
    extensions: [TextAlign],
    initialValue: "<p>The toolbar has the floor.</p>",
  });

  return (
    <EditorProvider editor={editor}>
      <div role="group" aria-label="Text alignment">
        {choices.map(([command, label]) => (
          <AlignButton key={command} editor={editor} command={command} label={label} />
        ))}
      </div>
      <EditorContent ariaLabel="Editor" />
    </EditorProvider>
  );
}

Behavior to account for

If the command covers several blocks, each selected block is aligned. Enable TextAlign in every editor that will load or preserve the saved alignment.

Features