blockcn

Basic editor

Render the editor in Next.js or Vite and learn how its pieces fit together.

The editor is split into two parts:

  • useEditor creates the Tiptap instance: extensions, content and whether it's editable.
  • <Editor> draws it, using compound components you can rearrange.

Keeping them apart lets you change the extensions or the starting document without rewriting the UI.

createEditorExtensions() is the document schema without React. The React slash menu and the code, callout, and toggle views come from createBlockcnEditor. A page that cannot use React calls mountBlockcnEditor(element) instead: a DOM toolbar (bold, italic, heading, list, quote) and a DOM slash menu. See installation.

The component

src/components/block-editor.tsx
"use client";

import { Editor } from "@/components/editor/Editor";
import { useEditor } from "@/components/editor/useEditor";
import { createBlockcnEditor } from "@/components/editor/preset";

const { extensions } = createBlockcnEditor();

export function BlockEditor() {
  const editor = useEditor({ extensions });

  return (
    <Editor editor={editor}>
      <Editor.Frame>
        <Editor.Content />
        <Editor.Chrome />
      </Editor.Frame>
      <Editor.Outline />
    </Editor>
  );
}

Keep extensions stable

useEditor creates the editor once. Declare the extensions array outside the component, or pass it in deps when it should be rebuilt. In development, changing the array without deps logs a warning and leaves the first editor in place.

This uses only the core schema: paragraphs, headings (H1–H4), bulleted and numbered lists, to-dos, quotes, callouts, toggles, syntax-highlighted code blocks and a divider. Text supports bold, italic, underline (Mod-U), strikethrough, subscript, superscript, inline code, links, color, highlight and alignment. Mod-K opens the link popover and Mod-Shift-K removes the link. Mod-F finds text. Mod-Alt-F or Mod-H opens replace. Match case, whole word, and regular expressions are in the panel. Enter and Shift+Enter move through matches, and Escape closes the panel and puts the caret back. Replace all is one undo step. A read-only editor can find but not replace. core.findReplace: false leaves the feature out. The panel is inside Editor.Frame, so it stays available while Editor.Chrome is hidden. <Editor.Stats /> shows the word count, the character count, and a reading time (200 words a minute, rounded up). A selection adds the selected word count. core.documentStats: { limit: { characters: 1000 } } or { words: 500 } refuses typing and paste that would go past the limit, and the footer shows how many are left. core.documentStats: false leaves the count out. Han, Hiragana, Katakana, and Hangul characters each count as one word. An image, a mention, and other atoms add a block and not their internal text. Mod-Shift-ArrowUp and Mod-Shift-ArrowDown move the current block, and the block menu has Move up and Move down (they also move a multi-block selection, and a block inside a toggle or column stays in that container). Mod-D duplicates it, and Mod-/ opens the block menu. The side menu + button inserts a paragraph and opens the slash menu there.

At the start of a block, # through #### (or up to the configured heading levels), - or * , 1. , > , [] or [x] , ---, and three backticks plus a language convert as you type. One undo puts those characters back. Turning a block off with core removes its rule. Callouts and toggles are inserted from the slash menu; they have no typing shortcut. With the math add-on, $$ starts a math block and $E=mc^2$ starts inline math.

Pass core to leave a block out or change it. false removes it; an object configures it. Heading levels default to 1–4 and can be 1–6. createBlockcnEditor({ core }) takes the same option.

const extensions = createEditorExtensions({
  core: {
    heading: { levels: [1, 2, 3] },
    callout: false,
  },
});

A document that still contains a removed block loads as an unsupported block. getJSON() writes the original block back, so turning the block on again restores it. Slash items, Turn into, the side menu, toolbar buttons, Markdown paste, and HTML export for that block are left out too.

starterKit is deep-merged over the StarterKit defaults, so you can change link behavior, the drop cursor, or turn a mark off. Where core and starterKit set the same key, core wins. document and codeBlock stay off, because the editor supplies those nodes. history: false still removes undo.

const extensions = createEditorExtensions({
  starterKit: {
    link: { openOnClick: true },
    strike: false,
  },
});

Anatomy

PartWhat it does
EditorProvides the editor through context. Renders nothing until the editor is ready.
Editor.FrameThe positioned wrapper. Leaves room on the left for the drag handle.
Editor.ContentThe editable document.
Editor.ChromeFloating UI. With no children it renders Editor.SideMenu and Editor.FormattingToolbar. Hidden when the editor is read-only.
Editor.OutlineA heading list for quick navigation. Leave it out if you don't want it.
<ExportMenu />A file menu for import (with a report) and HTML, Markdown, Word, and PDF export, from the export-menu install (see Blocks and export).

If you pass children to Editor.Chrome, you choose exactly what renders. Include Editor.SideMenu and Editor.FormattingToolbar yourself:

<Editor.Chrome>
  <Editor.SideMenu />
  <Editor.FormattingToolbar />
  {/* add-on toolbars go here */}
</Editor.Chrome>

See every prop in the API reference, including how to add a toolbar button.

Render it

Next.js

The editor and ProseMirror need a browser. Load the component with server rendering turned off:

src/app/page.tsx
"use client";

import dynamic from "next/dynamic";

const BlockEditor = dynamic(
  () => import("@/components/block-editor").then((mod) => mod.BlockEditor),
  { ssr: false },
);

export default function Page() {
  return (
    <main className="mx-auto max-w-3xl px-4 py-10">
      <BlockEditor />
    </main>
  );
}

next/dynamic with ssr: false only works in a client component, which is why the page has "use client". To keep the page a server component, move the dynamic() call into a small client wrapper and import that.

The editable editor needs a browser. A saved document can be rendered on the server with renderDocument(json), which returns an HTML string and does not use the DOM. Pass { addons } so that render uses each add-on's serializers map.

Read-only mode

Pass editable: false. Editor.Chrome hides itself, so readers see the content and the outline without editing controls.

const editor = useEditor({ extensions, editable: false });

You can switch modes on the fly. useEditor calls editor.setEditable() whenever editable changes.

Next: save and load content.

On this page