Takumi

Helpers

Look up the helpers that build node trees, convert JSX for a renderer binding, and load fonts, and the package that exports each one.

Takumi's packages export helpers that build node trees and load fonts. Import each one from the package you render with:

PackageHelpers
takumi-jsgoogleFonts, container, text, image, Bitmap
takumi-pdfgoogleFonts
@takumi-rs/core and @takumi-rs/wasmfromJsx, googleFonts, fontFromUrl, extractEmojis, container, text, image, Bitmap
Build a node tree
import { ,  } from "takumi-js";

const  = ({
  : { : 32, : "white" },
  : [("Hello Takumi", { : 48 })],
});

render and ImageResponse already convert JSX and HTML and load the images they use, so use these helpers to build node trees or load fonts yourself. @takumi-rs/helpers and the takumi-js/helpers entries still export them, but both are deprecated, and @takumi-rs/helpers becomes internal in v3.

Fonts

HelperUse
googleFonts(options)Load Google Fonts families in one request. 1
fontFromUrl(url, options?)Turn a font URL into a lazy fonts entry. 2

1 render keeps only the subsets the text uses. See Fonts & text.

2 options takes the fetch limits. Without them, pass the URL string in fonts instead, which takumi-js and takumi-pdf take too.

googleFonts and fontFromUrl share fetch limits and retry behavior.

Building nodes

HelperReturns
container(props)A container node.
text(text, style?) / text(props)A text node.
image(props)An image node.

The props argument holds the node's own fields. Node types lists them. Styles, presets, and stylesheet rules share the Declarations type. Custom properties such as "--accent": "red" accept strings or numbers, including in React's style prop.

Write lengths and colors as CSS strings, such as "50%", "1.5rem", or "rgb(0 0 0 / 0.5)".

Converting JSX for a binding

A Renderer from @takumi-rs/core or @takumi-rs/wasm takes a node tree or an HTML string. Convert JSX with fromJsx(element, options?) from the same package, which returns { node, css }:

import { fromJsx, Renderer } from "@takumi-rs/core";

const { node, css } = await fromJsx(<div tw="flex text-4xl">Hello</div>);
const png = await new Renderer().render(node, { width: 1200, height: 630, css });

fromJsx resolves React hooks and context without react-dom. Components that call useState, useContext, or use() render with server semantics: initial state, no effects. Preact trees work only when their components call no Preact hooks.

Emoji

takumi-js splits emoji out of the text through its emoji option. On a renderer binding, call extractEmojis(node, emojiType) to replace emoji with CDN <img> nodes, then call the renderer's prepare, which fetches them with the other images. See Emoji.

emojiType takes the CDN values of the emoji option listed in Image loading & emoji. It does not take from-font, which fetches nothing.

Last updated on