SolidJS bindings for Tiptap
npm i solid-tiptap @tiptap/core @tiptap/pm
yarn add solid-tiptap @tiptap/core @tiptap/pm
pnpm add solid-tiptap @tiptap/core @tiptap/pm
You also need the Tiptap extensions you plan to use, such as @tiptap/starter-kit.
solid-js2.0 or later.@tiptap/coreand@tiptap/pm3.x.- Node.js 22.12 or later, if you import the package in Node.js.
Solid 1.x users: use the solid-1 branch, or the previous major ([email protected] for Solid 1.x, [email protected] for Solid 1.x with Tiptap 2).
import StarterKit from '@tiptap/starter-kit'; import { createTiptapEditor } from 'solid-tiptap'; function App() { let ref!: HTMLDivElement; const editor = createTiptapEditor(() => ({ element: ref, extensions: [StarterKit], content: `<p>Example Text</p>`, })); return <div id="editor" ref={ref} />; }
createTiptapEditor returns an accessor for the editor.
- The editor is created after the component mounts, once
elementis set. The accessor returnsundefineduntil then. - The options function is tracked. When a signal it reads changes, the old editor is destroyed and a new one is created.
- The editor is destroyed when the owning component unmounts.
Because the options are tracked, read signals that should not recreate the editor with untrack.
import { untrack } from 'solid-js'; const editor = createTiptapEditor(() => ({ element: ref, extensions: [StarterKit], // Changing `initialContent` later does not recreate the editor. content: untrack(initialContent), }));
Use the editor's own commands to update it after it is created.
editor()?.commands.setContent('<p>New content</p>'); editor()?.setEditable(false);
Render the components that need the editor inside <Show>, so they only run once the editor exists.
const editor = createTiptapEditor(() => ({ ... })); <Show when={editor()} keyed> {(instance) => <Toolbar editor={instance} />} </Show>
keyed passes the editor itself to the children. Without it, Solid passes an accessor, so you would write editor={instance()}.
element also accepts undefined and null. The editor waits until the element is set, so a signal ref works without a non-null assertion.
const [element, setElement] = createSignal<HTMLDivElement>(); const editor = createTiptapEditor(() => ({ element: element(), extensions: [StarterKit], })); return <div ref={setElement} />;
Tiptap's BubbleMenu and FloatingMenu extensions take an element option. Pass a mounted element the same way you pass the editor element.
import BubbleMenu from '@tiptap/extension-bubble-menu'; function App() { let container!: HTMLDivElement; let menu!: HTMLDivElement; const editor = createTiptapEditor(() => ({ element: container, extensions: [StarterKit, BubbleMenu.configure({ element: menu })], })); return ( <> <div ref={menu}> <Show when={editor()} keyed> {(instance) => <Toolbar editor={instance} />} </Show> </div> <div ref={container} /> </> ); }
Tiptap does not notify Solid when the editor changes. createEditorTransaction reads a value again after every editor transaction, so you can use it in JSX and effects.
import { createEditorTransaction } from 'solid-tiptap'; const isBold = createEditorTransaction( () => props.editor, (editor) => editor.isActive('bold'), ); createEffect(isBold, (bold) => { if (bold) { // ... } });
Like any Solid 2 signal, the new value is visible after Solid applies queued updates. Call flush() from solid-js if you need it right away, for example in a test.
editor.commands.setContent('<p>Bye</p>'); flush(); html(); // '<p>Bye</p>'
The read function receives whatever the first accessor returns. If that can be undefined, handle it in the read function.
const length = createEditorTransaction( editor, (instance) => instance?.state.doc.textContent.length ?? 0, );
Each helper wraps createEditorTransaction for one part of the Editor API. Each takes an editor accessor, which may return undefined. Each returns an accessor that returns undefined while there is no editor.
| Helper | Reads | Returns |
|---|---|---|
useEditorHTML |
editor.getHTML() |
string |
useEditorJSON |
editor.getJSON() |
JSONContent |
useEditorIsActive |
editor.isActive(...) |
boolean |
useEditorIsEmpty |
editor.isEmpty |
boolean |
useEditorIsEditable |
editor.isEditable |
boolean |
useEditorIsFocused |
editor.isFocused |
boolean |
const html = useEditorHTML(editor); createEffect(html, (value) => { saveHTML(value); });
const json = useEditorJSON(editor); createEffect(json, (data) => { if (data) { uploadJSON(data); } });
useEditorIsActive takes a name accessor with optional attributes, a name accessor alone, or attributes alone.
// A node or mark by name. const isBold = useEditorIsActive(editor, () => 'bold'); // A node or mark by name and attributes. const isHeading = useEditorIsActive(editor, () => 'heading', { level: 1 }); // Any node or mark with these attributes. const isCentered = useEditorIsActive(editor, { textAlign: 'center' });
Creates a Tiptap editor. Also exported as createEditor, useEditor and useTiptapEditor.
optionsis a function that returns Tiptap's editor options. No editor is created whileelementisundefinedornull.- Returns
() => Editor | undefined.
editoris a function that returns anEditororundefined.readis called with the result ofeditor(). Its result is returned by the accessor.- Returns an accessor that runs
readagain after every editor transaction.
UseEditorOptionsis the options type forcreateTiptapEditor. ItselementisHTMLElement | null | undefined.BaseEditorOptionsis Tiptap'sEditorOptionswithoutelement, with every field optional.
MIT © lxsmnsyc
