# Text alignment {#text-align}

Source: [Align text in a React editor · Yurich](https://leeyujoo.com/yurich/features/text-align/)

Package: [`yurich`](https://www.npmjs.com/package/yurich)

Package version: `0.1.2`

Align paragraphs and list items.

## Try it {#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.

## Add it to your editor {#setup}

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

```tsx
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 {#behavior}

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](https://leeyujoo.com/yurich/features/)
