# react-example-font-family

> Change the font family of text.

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

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

## Install

```bash
npx shadcn@latest add https://prosekit.dev/r/react-example-font-family.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-font-family.json`

## Files

### `registry/src/react/examples/font-family/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-font-family.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/font-family/extension.ts`

```ts
import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { defineFontFamily } from 'prosekit/extensions/font-family'

export function defineExtension() {
  return union(defineBasicExtension(), defineFontFamily())
}

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

### `registry/src/react/examples/font-family/index.ts`

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

### `registry/src/react/examples/font-family/toolbar.tsx`

```tsx
'use client'

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

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

const fonts = [
  { label: 'Arial', family: 'Arial, sans-serif' },
  { label: 'Times New Roman', family: 'Times New Roman, serif' },
  { label: 'Courier New', family: 'Courier New, monospace' },
  { label: 'Georgia', family: 'Georgia, serif' },
  { label: 'Verdana', family: 'Verdana, sans-serif' },
  { label: 'Comic Sans MS', family: 'Comic Sans MS, cursive' },
  { label: 'Impact', family: 'Impact, sans-serif' },
  { label: 'Inter', family: 'Inter, sans-serif', google: true },
  { label: 'Playfair Display', family: 'Playfair Display, serif', google: true },
  { label: 'Merriweather', family: 'Merriweather, serif', google: true },
]

function loadGoogleFonts() {
  const linkId = 'prosekit-google-fonts'
  if (document.getElementById(linkId)) return
  const link = document.createElement('link')
  link.id = linkId
  link.rel = 'stylesheet'
  link.href =
    'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&family=Merriweather:wght@400;700&family=Playfair+Display:wght@400;700&display=swap'
  document.head.appendChild(link)
}

function getToolbarState(editor: Editor<EditorExtension>) {
  let activeFont = ''
  for (const font of fonts) {
    if (editor.marks.fontFamily.isActive({ family: font.family })) {
      activeFont = font.family
      break
    }
  }

  const handleChange = (value: string) => {
    if (!value) {
      editor.commands.removeFontFamily()
      return
    }
    const font = fonts.find((f) => f.family === value)
    if (font?.google) {
      loadGoogleFonts()
    }
    editor.commands.addFontFamily({ family: value })
  }

  return { activeFont, handleChange }
}

export default function Toolbar() {
  const state = useEditorDerivedValue(getToolbarState)

  useEffect(() => {
    loadGoogleFonts()
  }, [])

  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">
      <select
        value={state.activeFont}
        onChange={(e) => state.handleChange(e.target.value)}
        className="px-2 py-1 rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 text-sm"
      >
        <option value="">Default Font</option>
        {fonts.map((font) => (
          <option key={font.family} value={font.family} style={{ fontFamily: font.family }}>
            {font.label}
          </option>
        ))}
      </select>
    </div>
  )
}
```
