notion-editor
v0.2.0
Published
A Notion-style block editor for React, built on ProseMirror
Maintainers
Readme
notion-editor
A Notion-style block editor for React, built on ProseMirror.
npm install notion-editorimport { NotionEditor } from "notion-editor";
export default function Page() {
return <NotionEditor autoFocus onChange={(blocks) => console.log(blocks)} />;
}That's it. You get the slash menu, the floating format toolbar, drag handles, markdown shortcuts, nested lists, tables, columns and light/dark theming.
Theming
The editor does not own your theme — it detects yours and mirrors it. All of these work with no configuration:
| Your setup | What it sets | Detected |
|---|---|---|
| Tailwind darkMode: 'class' | <html class="dark"> | ✅ |
| next-themes attribute="class" | <html class="dark\|light"> | ✅ |
| next-themes attribute="data-theme" | <html data-theme="dark"> | ✅ |
| Chakra / GitHub Primer | data-color-mode="dark" | ✅ |
| Bootstrap 5.3 | data-bs-theme="dark" | ✅ |
| Nothing | prefers-color-scheme | ✅ |
An explicit host setting always beats the OS preference, so a user who has chosen light mode in your app stays in light mode on a dark-mode machine.
To override the editor specifically:
<NotionEditor theme="dark" /> {/* force */}
<NotionEditor theme="system" /> {/* follow the OS, ignore the host */}
<NotionEditor theme="auto" /> {/* default: mirror the host */}Every colour is a CSS variable on .ne-root, so you can restyle without
forking:
.ne-root {
--ne-accent: #7c3aed;
--ne-text: #1a1a1a;
--ne-surface: #ffffff;
}Floating menus render in a portal on document.body and carry the resolved
theme as data-ne-theme, so they match even though they sit outside your
provider's DOM subtree.
Storage
Two formats, both first-class:
const editor = useNotionEditor();
editor.getJSON(); // ProseMirror document JSON — lossless. Recommended.
editor.getBlocks(); // Flat Block[] — convenient, but a lossy projection.
editor.getText(); // Plain text, one block per line.Block[] is the format the pre-1.0 versions of this package used. It is kept
so existing documents load unchanged, but it cannot express everything the
schema can (nested column contents collapse, unknown types degrade). Use
getJSON() for anything you intend to keep.
Controlling the editor
import { NotionEditor, useNotionEditor } from "notion-editor";
function MyEditor() {
const editor = useNotionEditor();
return (
<>
<button
onClick={() => editor.toggleMark("bold")}
aria-pressed={editor.state.bold}
>
Bold
</button>
<button onClick={() => editor.setBlockType("heading", { level: 1 })}>
H1
</button>
<span>{editor.state.wordCount} words</span>
<NotionEditor ref={editor.ref} onStateChange={editor.onStateChange} />
</>
);
}editor.state is a reactive snapshot: active marks, current block type, word
and character counts, and whether the document is empty.
Collaborative editing
Real-time editing is opt-in, from a separate entry point, so applications that don't need it never bundle Yjs.
npm install yjs y-prosemirror y-websocketimport { NotionEditor } from "notion-editor";
import { collabPlugins } from "notion-editor/collab";
import * as Y from "yjs";
import { WebsocketProvider } from "y-websocket";
const ydoc = new Y.Doc();
const provider = new WebsocketProvider("wss://your.server", "room-1", ydoc);
<NotionEditor
disableHistory // Yjs brings its own per-user undo
extraPlugins={collabPlugins({
doc: ydoc,
awareness: provider.awareness,
user: { name: "Ada", color: "#2383e2" },
})}
/>;disableHistory matters: in a shared document Ctrl+Z must undo your last
edit, not a collaborator's, and Yjs's undo manager is the thing that knows the
difference.
Selection
Notion has two selection modes, and so does this editor. They are separate modes, not one mode drawn two ways — that distinction matters, because inferring "block selection" from whether a text range happens to cover a whole block produces a hybrid Notion never shows (interior blocks tinted, first and last merely text-highlighted).
| | Text mode | Block mode |
|---|---|---|
| Enter it by | Dragging through prose | Dragging in the margin (any direction), Escape, the drag handle, or Shift+click |
| Looks like | Highlight hugs the glyphs | Each block a rounded tile, with gaps between them (list items too) |
| Caret | Visible | Hidden |
| Arrows | Move by character | Move by block |
| Shift+arrows | Extend by character | Extend by block |
| Backspace | Deletes the range | Deletes the blocks outright |
| Formatting | Applies to the exact characters, across blocks | Applies to whole blocks |
Escape walks outward: text selection → block selection → caret. Ctrl/⌘ A
selects the block's text, and again selects every block. Shift+click (on a
block or its handle) extends a block selection to that block.
The lasso works from any side of the text — left, right, above or below — and scrolls the page (or the editor's scrolling container) when you drag near its top or bottom edge, so a selection can run past what is on screen. A plain click in the blank space below the content puts the caret on a new line at the end, as in Notion.
The tints are --ne-selection (text) and --ne-block-selection (blocks and
the lasso) on .ne-root.
Deleting in block mode removes the blocks rather than joining them, which is what Notion does — deleting the underlying text range would merge the first and last block into one.
The gutter needs horizontal room. .ne-root reserves --ne-gutter (54px by
default, 0 under 640px) for it and for the +/drag handles; override that
variable if you lay them out yourself.
Blocks
Text · Heading 1–3 · Bulleted list · Numbered list · To-do · Toggle · Quote · Callout · Code · Divider · Image · Table · 2 and 3 column layouts
Lists nest for real (Tab / Shift+Tab), and so do to-dos and toggles: Tab
tucks a to-do, toggle, paragraph or heading under a to-do or toggle just above
it, and Shift+Tab moves it back out, taking the blocks below it along. Any
block converts to any other — including a list item straight to a heading.
Tables get a + bar below (add row) and to the right (add column) on hover,
and a ⋯ button on the current cell for inserting or deleting rows and
columns. Tab in the last cell adds a row.
Block menu
Click a block's ⋮⋮ handle (or press Ctrl/⌘ /) for Notion's block menu: a
"Search actions…" box, the block's type, Turn into ›, Color › (text or
background for the whole block), Duplicate, Move up/down and Delete with their
shortcuts, and the word and character count. With several blocks selected,
every action applies to all of them. Block colours are stored as a
blockColor attribute ("red", "red_background", …) and survive
getJSON(), getBlocks() and copy/paste.
Slash menu
Type / for the block picker: "Type to search" right in the line, outline
icons, the markdown shortcut for each block on the right, a Recent group,
and multi-word search (/to do, /2 col).
Markdown shortcuts
| Type | Get |
|---|---|
| # ## ### | Headings |
| - * + | Bulleted list |
| 1. | Numbered list |
| [] [x] | To-do |
| > | Toggle (as in Notion) |
| " | Quote |
| ``` | Code block |
| --- | Divider |
| [label](url) | Link |
| **bold** *italic* ~~strike~~ `code` | Inline marks |
Keyboard
| Shortcut | Action |
|---|---|
| Ctrl/⌘ B I U E | Bold, italic, underline, code |
| Ctrl/⌘ Shift S | Strikethrough |
| Ctrl/⌘ K | Link (example.com becomes https://example.com; javascript: and data: links are refused, including in pasted HTML) |
| Ctrl/⌘ Alt 0–3 | Text, H1, H2, H3 |
| Ctrl/⌘ Shift 8 / 9 | Bulleted / numbered list |
| Tab / Shift+Tab | Nest / un-nest, or next / previous table cell (a new row from the last) |
| Ctrl/⌘ Z / Shift Z | Undo / redo |
| Shift+Enter | Line break inside a block |
| / | Insert a block (slash menu) |
| Ctrl/⌘ / | Block menu for the current block |
| Ctrl/⌘ D | Duplicate the block(s) |
| Ctrl/⌘ Shift ↑ / ↓ | Move the block(s) up / down |
Keys are bound to the editor's own DOM node, never to window, so Tab and
Ctrl+A behave normally everywhere else on your page.
API
<NotionEditor>
| Prop | Type | Default | |
|---|---|---|---|
| initialBlocks | Block[] | — | Starting document |
| onChange | (blocks, doc) => void | — | After every document change |
| onStateChange | (state) => void | — | After every transaction |
| theme | "auto" \| "light" \| "dark" \| "system" | "auto" | |
| editable | boolean | true | |
| autoFocus | boolean | false | |
| ariaLabel | string | "Rich text editor" | |
| showSlashMenu | boolean | true | |
| showFormatToolbar | boolean | true | |
| showBlockHandles | boolean | true | |
| showTableControls | boolean | true | Add-row/column bars and the cell menu |
| blockDefinitions | BlockDefinition[] | built-ins | Customise the slash menu |
| disableInputRules | boolean | false | Turn off markdown shortcuts |
| disableHistory | boolean | false | Required with Yjs |
| extraPlugins | Plugin[] | — | Any ProseMirror plugins |
Ref handle
getBlocks() / setBlocks(blocks)
getJSON() / setJSON(json)
getText()
focus() / blur()
isEmpty()
view // the raw EditorView, for anything not covered aboveBuilding your own chrome
ProseMirrorEditor is the same engine with no UI attached. SlashMenuPM,
FormatToolbar and BlockHandles are exported individually, as are the
schema, plugins and commands:
import {
ProseMirrorEditor,
schema, buildPlugins, blocksToDoc, docToBlocks,
convertBlock, setColor, setLink, moveBlock,
} from "notion-editor";Accessibility
The editing surface is role="textbox" with aria-multiline. Checkboxes are
real role="checkbox" buttons with aria-checked; toggles expose
aria-expanded. Menus are role="listbox" / role="menu" with
aria-selected and aria-pressed on toolbar buttons. Focus rings use
:focus-visible, and every animation is disabled under
prefers-reduced-motion.
SSR
The package is client-only ("use client"). Theme resolution defers to an
effect so server and client markup agree, and no data-ne-theme is emitted in
auto mode — the CSS cascade handles it, which means no flash and no
hydration warning.
Tests
npm test293 unit tests cover the block/JSON round trip, every command, the slash
trigger's edge cases, host theme detection, and the Notion-parity fixes in
src/pm/parity.test.ts. A few node-view tests run under jsdom.
