# テキストの配置 {#text-align}

Source: [React エディターで文字の配置を変える · Yurich](https://leeyujoo.com/yurich/ja/features/text-align/)

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

Package version: `0.1.2`

段落やリスト項目の配置を変えます。

## 試してみる {#try-it}

例の段落内で **中央揃え** を押すと HTML に `style="text-align: center"` が付きます。JSON の `attrs.textAlign` は `"center"` です。

## エディターに追加する {#setup}

`TextAlign` を有効にし、左、中央、右、両端揃えをコマンドに接続します。

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

const choices = [
  ['alignLeft', "左揃え"],
  ['alignCenter', "中央揃え"],
  ['alignRight', "右揃え"],
  ['alignJustify', "両端揃え"],
] 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>ここからはツールバーの出番です。</p>",
  });

  return (
    <EditorProvider editor={editor}>
      <div role="group" aria-label="文字揃え">
        {choices.map(([command, label]) => (
          <AlignButton key={command} editor={editor} command={command} label={label} />
        ))}
      </div>
      <EditorContent ariaLabel="エディター" />
    </EditorProvider>
  );
}
```

## 動作の補足 {#behavior}

複数のブロックに適用されます。保存した配置を保つには読み込み先でも `TextAlign` を有効にします。

[機能](https://leeyujoo.com/yurich/ja/features/)
