# react-example-text-align

> Text alignment options (left, center, right, justify).

A shadcn/ui block from the [@prosekit](https://prosekit.dev) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/prosekit/react-example-text-align).

- Page, with a live preview: https://shadcn.exchange/r/prosekit/react-example-text-align
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/prosekit/react-example-text-align-light-e38f5c8277fd52fd.png
- Registry item: https://prosekit.dev/r/react-example-text-align.json
- Listed: 2026-10-13, source last changed 2026-10-13

## Install

```bash
npx shadcn@latest add https://prosekit.dev/r/react-example-text-align.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `prosekit`
- Registry items: `https://unpkg.com/prosekit-registry/dist/r/react-sample-sample-doc-text-align.json`, `https://unpkg.com/prosekit-registry/dist/r/react-ui-button.json`

## Files

### `registry/src/react/examples/text-align/editor.tsx`

```tsx
'use client'

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

import { createEditor, type NodeJSON } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { sampleContent } from '../../sample/sample-doc-text-align.ts'

import { defineExtension } from './extension.ts'
import Toolbar from './toolbar.tsx'

interface EditorProps {
  initialContent?: NodeJSON
}

export default function Editor(props: EditorProps) {
  const defaultContent = props.initialContent ?? sampleContent
  const editor = useMemo(() => {
    const extension = defineExtension()
    return createEditor({ extension, defaultContent })
  }, [defaultContent])

  return (
    <ProseKit editor={editor}>
      <div className="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-[canvas] text-black dark:text-white">
        <Toolbar />
        <div className="relative w-full flex-1 box-border overflow-y-auto">
          <div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
        </div>
      </div>
    </ProseKit>
  )
}
```

### `registry/src/react/examples/text-align/extension.ts`

```ts
import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { defineTextAlign } from 'prosekit/extensions/text-align'

export function defineExtension() {
  return union(
    defineBasicExtension(),
    defineTextAlign({ types: ['paragraph', 'heading'] }),
  )
}

export type EditorExtension = ReturnType<typeof defineExtension>
```

### `registry/src/react/examples/text-align/index.ts`

```ts
export { default as ExampleEditor } from './editor.tsx'
```

### `registry/src/react/examples/text-align/toolbar.tsx`

```tsx
'use client'

import type { Editor } from 'prosekit/core'
import { useEditorDerivedValue } from 'prosekit/react'

import { Button } from '../../ui/button/index.ts'

import type { EditorExtension } from './extension.ts'

function isTextAlignActive(editor: Editor<EditorExtension>, value: string) {
  return Object.values(editor.nodes).some((node) => {
    // @ts-expect-error textAlign may not be available on every node
    return node.isActive({ textAlign: value })
  })
}

function getToolbarItems(editor: Editor<EditorExtension>) {
  return {
    left: {
      isActive: isTextAlignActive(editor, 'left'),
      canExec: editor.commands.setTextAlign.canExec('left'),
      command: () => editor.commands.setTextAlign('left'),
    },
    center: {
      isActive: isTextAlignActive(editor, 'center'),
      canExec: editor.commands.setTextAlign.canExec('center'),
      command: () => editor.commands.setTextAlign('center'),
    },
    right: {
      isActive: isTextAlignActive(editor, 'right'),
      canExec: editor.commands.setTextAlign.canExec('right'),
      command: () => editor.commands.setTextAlign('right'),
    },
    justify: {
      isActive: isTextAlignActive(editor, 'justify'),
      canExec: editor.commands.setTextAlign.canExec('justify'),
      command: () => editor.commands.setTextAlign('justify'),
    },
  }
}

export default function Toolbar() {
  const items = useEditorDerivedValue(getToolbarItems)

  return (
    <div className="z-2 box-border border-gray-200 dark:border-gray-800 border-solid border-l-0 border-r-0 border-t-0 border-b flex flex-wrap gap-1 p-2 items-center">
      <Button
        pressed={items.left.isActive}
        disabled={!items.left.canExec}
        onClick={items.left.command}
      >
        Left
      </Button>

      <Button
        pressed={items.center.isActive}
        disabled={!items.center.canExec}
        onClick={items.center.command}
      >
        Center
      </Button>

      <Button
        pressed={items.right.isActive}
        disabled={!items.right.canExec}
        onClick={items.right.command}
      >
        Right
      </Button>

      <Button
        pressed={items.justify.isActive}
        disabled={!items.justify.canExec}
        onClick={items.justify.command}
      >
        Justify
      </Button>
    </div>
  )
}
```
