typstream

typstream

The convenience package for React apps, with the Typstream component from @typstream/react already set up with html-compat, the math font and the vendored Typst packages.

typstream is the package most React apps install. Its <Typstream> is @typstream/react's component with the defaults an app wants already applied: layout that survives the HTML target, a math font for MathML, and a set of Typst packages documents may import. It also re-exports the rest of @typstream/react, the engine client and the healer, so one import covers a typical app.

When to use it

Use typstream in any React app, including Next.js. Use @typstream/react instead when you want to choose every plugin yourself, @typstream/dom when you don't use React, and @typstream/node to validate or render documents on a server.

Install

npm install typstream

React 19 is a peer dependency. The engine runs in a Web Worker that loads Typst as WebAssembly. Vite and Next.js (Turbopack or webpack) bundle the worker, the wasm and the fonts as assets with no configuration. See Installation for the details.

Example

Pass the model's text so far as source, and say whether more is coming with isStreaming:

Answer.tsx
"use client";
import { Typstream } from "typstream";

export function Answer({ text, streaming }: { text: string; streaming: boolean }) {
  return <Typstream source={text} isStreaming={streaming} animated caret />;
}

While isStreaming is true, every prefix is healed so it compiles, and the last good render stays on screen while a prefix doesn't. When it turns false, the document is compiled strictly (final mode) and the caret goes away. The React guide and the Next.js + AI SDK guide build a full chat around it.

What it adds to @typstream/react

DefaultWhat it doesTurn it off
html-compatAdds the htmlCompat plugin: its Typst preamble, the sanitizer allowances and its stylesheet, so grids, alignment, boxes and shapes survive the HTML target.htmlCompat={false}
The math fontRegisters the engine's copy of New Computer Modern Math (from @typstream/engine-wasm/fonts) as a web font, so MathML delimiters stretch.Pass your own mathFont URL to use another copy.
Typst packagesAdds the universe plugin: documents may import CeTZ and fletcher (diagrams), cetz-plot, physica and unify (math), chemformula and alchemist (chemistry), at the vendored versions. The tarballs are served from your app's origin (your bundler emits them as assets) and fetched only when a document imports one.packages={false}

The two plugins go before any plugins you pass, so your own plugins apply after them.

Documents are untrusted code written by a model. The security guide covers what they can't do and the Content-Security-Policy to serve typstream under (no 'unsafe-inline' or 'unsafe-eval', only 'wasm-unsafe-eval').

API

Typstream

const Typstream: (props: TypstreamProps) => JSX.Element;

Renders a streaming Typst document. It takes every prop of @typstream/react's <Typstream>, plus htmlCompat and packages. Settings it doesn't receive come from the nearest TypstreamProvider, then the defaults.

<Typstream source={text} isStreaming={status === "streaming"} target="paged" layout="fit-width" />

TypstreamProps

Prop

Type

preloadTypstream and usePreloadTypstream

const preloadTypstream: (options?: PreloadTypstreamOptions) => TypstreamPreload;
const usePreloadTypstream: (when?: boolean, options?: PreloadTypstreamOptions) => void;

@typstream/react's, warming the plugins this package's <Typstream> adds: html-compat and universe, unless htmlCompat or packages is false, before the plugins given. Universe has no examples, so preloading downloads no Typst package; a package downloads when a document first imports it.

import { preloadTypstream } from "typstream";

<button onClick={() => { preloadTypstream(); generate(); }}>Generate</button>

PreloadTypstreamOptions

Prop

Type

Re-exports

Everything else in typstream comes from another package and is documented there:

ExportFromReference
TypstreamProvider, useTypstream@typstream/reactReact
Types: TypstreamProviderProps, TypstreamDefaults, TypstreamHandle, TypstreamInfo, TelemetryHooks@typstream/reactReact
PRELOAD_HOLD_MS, type TypstreamPreload@typstream/reactReact
Types: Engine, DocumentState, DocumentStatus, PreloadOptions@typstream/engine (through @typstream/react)Engine
createEngine, engineWorker, types EngineOptions, ImageResolver@typstream/engineEngine
fetchImages, type FetchImagesOptions@typstream/engine/imagesEngine, Images
createHealer, heal, types Healer, HealOptions, HealResult@typstream/healHeal
htmlCompat, htmlCompatCss@typstream/html-compathtml-compat
universe, UNIVERSE_PACKAGES, type UniversePackage@typstream/universeUniverse
Types: AppliedFix, CompiledEvent, CompileReport, DocumentEvent, FallbackEvent, FallbackLevel, RenderedEvent, WatchdogEvent@typstream/core (through @typstream/react)Core

createEngine and engineWorker are there for apps that share one engine across several roots or tune it (pass the result as the engine prop). heal and createHealer are there for apps that heal text themselves, for example to show the healed source next to the preview.

See also

On this page