PandaText / Docs / tldraw

PandaText for tldraw

The @pandatext/tldraw package adds a panda-text shape to a tldraw 5 canvas. PandaText measures, draws and edits its text instead of the browser, so a note wraps on the same word in Chrome, Safari and Firefox, and in your exports.

Try it

A tldraw canvas with the PandaText shape. Double-click a note to edit it, or pick the panda in the toolbar and click the canvas to add one.

Loading the canvas…

Install

Requires: tldraw 5, React 18 or 19, and the web requirements of @pandatext/core.

Install the shape with the web packages and tldraw:

npm install @pandatext/tldraw @pandatext/textview @pandatext/core tldraw

With Vite, keep the PandaText packages out of dependency pre-bundling, so the engine finds its .wasm file:

// vite.config.ts
export default defineConfig({
  optimizeDeps: { exclude: ["@pandatext/core", "@pandatext/textview", "@pandatext/tldraw"] },
});

A Content Security Policy needs 'wasm-unsafe-eval' in script-src. tldraw needs its own license key in production; see tldraw.dev.

Add the shape

Shapes measure their text as soon as tldraw asks, so load the engine and fonts with loadPandaText before the canvas mounts. Then pass the shape util, the tool, the UI overrides and a toolbar that shows the tool.

import { useEffect, useState } from "react";
import { DefaultToolbar, DefaultToolbarContent, Tldraw, type TLComponents } from "tldraw";
import "tldraw/tldraw.css";
import {
  PandaTextShapeTool,
  PandaTextShapeUtil,
  PandaTextToolbarItem,
  loadPandaText,
  pandaTextUiOverrides,
} from "@pandatext/tldraw";
import "@pandatext/tldraw/PandaTextShape.css";

const shapeUtils = [PandaTextShapeUtil];
const tools = [PandaTextShapeTool];
const components: TLComponents = {
  Toolbar: () => (
    <DefaultToolbar>
      <PandaTextToolbarItem />
      <DefaultToolbarContent />
    </DefaultToolbar>
  ),
};

export default function App() {
  const [ready, setReady] = useState(false);

  useEffect(() => {
    loadPandaText().then(() => setReady(true));
  }, []);

  if (!ready) return null;

  return (
    <div style={{ position: "fixed", inset: 0 }}>
      <Tldraw
        shapeUtils={shapeUtils}
        tools={tools}
        overrides={pandaTextUiOverrides}
        components={components}
        onMount={(editor) => {
          editor.createShape({
            type: "panda-text",
            x: 100,
            y: 100,
            props: { text: "Double-click to edit this text with PandaText." },
          });
        }}
      />
    </div>
  );
}

Toolbar tool

Each tldraw prop above does one job: tools registers PandaTextShapeTool, overrides gives it a label and the panda icon, and components.Toolbar shows the button. With your own toolbar, render <PandaTextToolbarItem /> there. With your own UI, switch to the tool directly:

import { PANDA_TEXT_TYPE } from "@pandatext/tldraw";

editor.setCurrentTool(PANDA_TEXT_TYPE);
  • Click the canvas to place a shape and start typing. A shape left empty is deleted when editing ends.
  • Dragging places the shape where the drag began. The tool doesn’t set a fixed width.
  • The tool has no keyboard shortcut. To add one, set kbd on tools["panda-text"] in a tools override that runs after pandaTextUiOverrides.
  • Double-clicking the empty canvas and pasting text still create tldraw’s own text shape.

Styles and wrapping

  • The shape has tldraw’s color, size, font (draw, sans, serif, mono) and text-align styles, so the style panel sets them.
  • The text color follows the theme and color mode.
  • The end alignment aligns right.
  • Drag a side handle to set a fixed width; the corners scale the shape.
  • SVG and image export draw the text with PandaText too, so an export matches the canvas.

Options

loadPandaText(options?) loads the engine and one font collection per font style, once. Later calls return the same promise.

fonts
Font URLs or bytes for each tldraw font style: draw, sans, serif, mono. A style you leave out uses tldraw’s own faces (regular, bold, italic, bold italic). An empty list skips the style, and its shapes use draw, which can’t be empty.
wasmUrl
Where to load pandatext_core.wasm from. The break dictionaries stay next to the package.
locateFile
A function that returns the URL of pandatext_core.wasm and pandatext-break-dictionaries.dat.
breakDictionaries
Passed to createPandaText: where the Chinese, Japanese and Thai word dictionaries come from.
licenseKey
Your PandaText license key, from early access.

For example, your own sans faces and a wasm file from your asset folder:

await loadPandaText({
  fonts: {
    sans: ["/fonts/Inter-Regular.woff2", "/fonts/Inter-Bold.woff2", "/fonts/Inter-Italic.woff2", "/fonts/Inter-BoldItalic.woff2"],
  },
  wasmUrl: "/assets/pandatext_core.wasm",
});