The guide · breakdown
The future of website interfaces.
Pretext is a single JavaScript/TypeScript package that lets you measure and lay out text without relying on the browser's layout engine. Every website you've ever used asks the browser to figure out where text goes — a process called "reflow", one of the slowest things a browser does, and the biggest bottleneck in web design for 30 years. Pretext skips it entirely: all measurement in pure TypeScript, no DOM, no CSS layout, no reflow. Just math. The result — hundreds of thousands of text elements rendering at 120fps.
The full breakdown
🔒 Drop your email to reveal the full breakdown
Get the full breakdown, free.
Drop your email and the breakdown unlocks instantly, plus every other guide and resource in the library. One email a week, unsubscribe anytime.
Links
- GitHub repo: github.com/chenglou/pretext
- Live demos: chenglou.me/pretext
- More demos: somnai-dreams.github.io/pretext-demos
What the demos show
| Demo | What it does |
|---|---|
| Masonry | Hundreds of thousands of text boxes, all different heights, scrolling at 120fps with zero DOM measurement. |
| Chat Bubbles | Shrinkwrapped chat bubbles that size themselves perfectly. |
| Magazine Layout | Multi-column responsive magazine layout — dynamic and real-time. |
| ASCII Art | Variable font-width ASCII art, because why not. |
| Accordion | Auto-growing text areas, accordions, multiline centering — things that used to be real CSS challenges, now trivial. |
Why this matters (simple version)
- Every website asks the browser permission to place text. That permission is slow. It causes lag. It's called "reflow".
- Pretext skips that permission entirely. It calculates text layout in pure TypeScript.
- The performance difference is insane. 0.05ms vs 30ms. Zero reflows vs hundreds.
- This unlocks UI that wasn't possible before. Smooth virtualization, masonry layouts, magazine-style text, shrinkwrapped elements — all without CSS hacks.
Quick start
Install:
Basic usage — measure text height without touching the DOM:
How it works
prepare()— one-time work: analyses the text, measures segments using the browser's font engine, returns a cached handle.layout()— the fast part: pure arithmetic over cached widths. Run this on resize, scroll, or any layout change.
Key rule: Don't rerun prepare() for the same text. That defeats the precomputation. Only rerun layout().
Prompts to try with AI
Use these with Claude, ChatGPT or Cursor to start building with Pretext.
Prompt 1 — Basic integration
Prompt 2 — Masonry layout
Prompt 3 — Chat interface
Prompt 4 — Canvas rendering
Prompt 5 — Magazine layout
Full API reference
Use case 1: Measure height only (fastest)
Use case 2: Manual line layout (full control)
Helpers
Things to know
- Currently targets a standard text setup:
white-space: normal,word-break: normal,overflow-wrap: break-word. - Supports
{ whiteSpace: 'pre-wrap' }for textarea-like text with preserved spaces, tabs and line breaks. - Avoid the
system-uifont on macOS — use a named font for accuracy. - Supports all languages including emoji and mixed bidirectional text.
- Not a full font-rendering engine (yet) — it's focused on measurement and layout.
Who built this
Cheng Lou — helped build React at Facebook, created ReasonML and ReScript, built Messenger's frontend, and currently runs Midjourney's entire UI stack. Every role was a fight against the browser's rendering pipeline. This is the result.
Original guide by Artem Novitckii. More from him on Instagram, in the Skool community, and across his resource library.