Installation
Usage
"use client"
import { useState } from "react"
import { RichTextEditor, type JSONContent, type RichTextThread } from "@/components/rich-text-editor"
const me = { id: "u1", name: "Elena Novak" }
export function NotesPage() {
const [doc, setDoc] = useState<JSONContent | string>("<h2>Launch notes</h2><p>Type / for blocks, or select text to comment.</p>")
const [threads, setThreads] = useState<RichTextThread[]>([])
return (
<RichTextEditor
className="h-[40rem]"
defaultTitle="Launch notes"
value={doc}
onChange={(json, output) => {
setDoc(json)
// Store the JSON to edit later; output.html() and output.markdown() are there for publishing.
void fetch("/api/notes/launch", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ json, markdown: output.markdown() }) })
}}
user={me}
mentions={[me, { id: "u2", name: "Mateo Rossi" }]}
threads={threads}
onThreadsChange={(next) => setThreads(next)}
onAI={async function* (request, { signal }) {
// Your route calls your model (Claude, OpenAI, anything) and streams text back.
const response = await fetch("/api/ai", { method: "POST", body: JSON.stringify(request), signal })
const reader = response.body!.pipeThrough(new TextDecoderStream()).getReader()
for (;;) {
const { done, value } = await reader.read()
if (done) return
yield value
}
}}
onImageUpload={async (file, { signal }) => {
const body = new FormData()
body.append("file", file)
const response = await fetch("/api/upload", { method: "POST", body, signal })
const { url } = (await response.json()) as { url: string }
return url
}}
/>
)
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value / defaultValue | JSONContent | string | "" | The document, as Tiptap JSON or an HTML string. Pass back the JSON onChange gives you. |
| onChange | (json, { html, markdown, text }) => void | — | Every edit: the document as JSON, with functions that turn it into HTML, Markdown or plain text when you need them. |
| title / defaultTitle / onTitleChange | string | — | A title field above the document. Leave both out for no title. |
| placeholder | string | "Write, or press “/” for commands…" | Shown in an empty line. |
| mentions | RichTextPerson[] | [] | People offered after "@": { id, name, color?, avatar? }. |
| user | RichTextPerson | — | Who is writing. Turns on commenting; without it threads are shown read-only. |
| threads / defaultThreads / onThreadsChange | RichTextThread[] | [] | Comment threads: { id, quote, resolved?, comments }. Every add, reply, resolve and delete arrives in onThreadsChange with what changed. The highlighted text lives in the document, so save both. |
| onAI | (request, { signal }) => Promise<string> | AsyncIterable<string> | — | Turns on Ask AI. The request has the action (improve, shorter, translate, continue, summarise, ask…), the selection and the whole document as Markdown. Return Markdown, all at once or streamed. |
| onImageUpload | (file, { signal, onProgress }) => Promise<string> | — | Uploads pasted, dropped and chosen images and resolves to their URL. Without it, images are added by address. |
| editable | boolean | true | false shows the document read-only. |
| statusBar / status | boolean / ReactNode | true / — | The word count, reading time and comments switch along the bottom, and your own status beside them (such as "Saved"). |
| comments | boolean | true | Commenting and the comments panel. false hides both, even with a user. |
| defaultCommentsOpen | boolean | true | Whether the comments panel starts open. |
| label | string | "Document" | Accessible name of the document. |
Accessibility
- The document is a multi-line text box with its own name; every block, mark and table works from the keyboard, and Markdown shortcuts work as you type.
- The "/" and "@" lists follow the combobox pattern: focus stays in the text, the arrow keys move, Enter or Tab chooses and Escape closes, and the highlighted option is announced.
- Alt F10 moves focus into the selection toolbar, where the arrow keys move between buttons and Escape returns to the text. Shortcuts: Cmd or Ctrl B, I, U, K for a link, J for Ask AI and Alt M to comment.
- Dragging is never required: Cmd or Ctrl Shift Up and Down move a block, and the block menu (Enter on the handle) has Move up, Move down, Duplicate, Delete and Turn into.
- Comments are a labelled region of articles; replies send on Enter, deleting a thread asks first, and AI answers and upload progress are announced politely.
- Under prefers-reduced-motion nothing fades or pulses.
Changelog
- v1.0.0Latest
First release.