React

The @tlob/babloom-react adapter exposes a useEditor hook and re-renders your toolbar on every state change.

Live demo

This editor on the page is powered by the real core running in React.

Usage

tsx
import { StarterKit, PlaceholderExtension } from "@tlob/babloom-core";
import { useEditor } from "@tlob/babloom-react";
import "@tlob/babloom-core/styles.css";

export function Editor() {
  const { editor, ref } = useEditor({
    content: "<p>Hello BaBloom</p>",
    extensions: [StarterKit(), PlaceholderExtension({ text: "Start writing…" })],
    onUpdate: ({ editor }) => console.log(editor.getHTML()),
  });

  return (
    <div>
      <button
        onMouseDown={(e) => { e.preventDefault(); editor?.toggleBold(); }}
        data-active={editor?.isActive("bold")}
      >
        Bold
      </button>
      <div ref={ref} />
    </div>
  );
}

Notes

Use onMouseDown with preventDefault on toolbar buttons so the selection inside the editor is preserved. The hook destroys the editor instance automatically on unmount.