GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - lxsmnsyc/solid-tiptap: SolidJS bindings for tiptap

BranchesTags
Open more actions menu

Latest commit

 

History

70 Commits
70 Commits

Folders and files

NameName
Last commit message
Last commit date
.changeset
.changeset
 
 
.github/workflows
.github/workflows
 
 
examples/simple-example
examples/simple-example
 
 
images
images
 
 
packages/solid-tiptap
packages/solid-tiptap
 
 
scripts
scripts
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
oxfmt.config.ts
oxfmt.config.ts
 
 
oxlint.config.ts
oxlint.config.ts
 
 
package.json
package.json
 
 
pnpm-lock.yaml
pnpm-lock.yaml
 
 
pnpm-workspace.yaml
pnpm-workspace.yaml
 
 
tsconfig.json
tsconfig.json
 
 

Repository files navigation

solid-tiptap

SolidJS bindings for Tiptap

NPM Open in StackBlitz

solid-tiptap example

Install

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.

Requirements

  • solid-js 2.0 or later.
  • @tiptap/core and @tiptap/pm 3.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).

Usage

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 element is set. The accessor returns undefined until 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);

Passing the editor to other components

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()}.

Signal refs

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} />;

Bubble and floating menus

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} />
    </>
  );
}

Reading editor state

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,
);

Helpers

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' });

API

createTiptapEditor(options)

Creates a Tiptap editor. Also exported as createEditor, useEditor and useTiptapEditor.

  • options is a function that returns Tiptap's editor options. No editor is created while element is undefined or null.
  • Returns () => Editor | undefined.

createEditorTransaction(editor, read)

  • editor is a function that returns an Editor or undefined.
  • read is called with the result of editor(). Its result is returned by the accessor.
  • Returns an accessor that runs read again after every editor transaction.

Types

  • UseEditorOptions is the options type for createTiptapEditor. Its element is HTMLElement | null | undefined.
  • BaseEditorOptions is Tiptap's EditorOptions without element, with every field optional.

Sponsors

Sponsors

License

MIT © lxsmnsyc

About

SolidJS bindings for tiptap

Resources

Readme
MIT license
Activity

Stars

160 stars

Watchers

1 watching

Forks

9 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.