# 텍스트 정렬 {#text-align}

Source: [React 에디터에서 문단 정렬하기 · Yurich](https://leeyujoo.com/yurich/ko/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/ko/features/)
