← All writing
Engineering Apr 1, 2026 · 8 min read

What should designers know about Pretext?

Pretext measures every line of text instantly, without asking the browser. No more guessing container heights, broken masonry, or bloated chat bubbles. Here's what it means for designers.

EngineeringTypographyPerformanceAI Interfaces

Every time a browser displays text, it has to figure out how tall each block will be. Where do lines break? How tall is each line? How much space does the whole paragraph take? This calculation (called a reflow) has always required asking the browser itself, which is slow and can freeze the page.

Think of it like this: Imagine you’re laying out a magazine, but every time you want to know how tall an article is, you have to print it, measure it with a ruler, then re-layout the page. That’s essentially what browsers have been doing, thousands of times per second on complex pages.

Pretext solves this by computing text layout in pure JavaScript, no printing required. It can measure 100,000 text blocks in under 50ms, enabling layouts that were previously impossible or impractical.

Demo 1: Shrinkwrapped chat bubbles

This is the clearest win for product designers. CSS has no way to make a chat bubble hug its content precisely — a multiline message gets a bubble as wide as its max-width, leaving the shorter lines floating in empty space. Drag the slider to see the difference.

CSS fit-content vs Pretext shrinkwrap
400px
CSS fit-content
Sizes bubble to the longest wrapped line. Shorter lines leave empty space behind.
Wasted pixels: —
yo did you see the new Pretext library?
yeah! measures text without touching the DOM. pure JS arithmetic
wild. so chat bubbles can finally hug their content?
zero wasted pixels. no more empty bubble space.
Pretext shrinkwrap
Binary-searches the minimum width for the same line count. Zero wasted pixels.
Wasted pixels: 0
yo did you see the new Pretext library?
yeah! measures text without touching the DOM. pure JS arithmetic
wild. so chat bubbles can finally hug their content?
zero wasted pixels. no more empty bubble space.

Both sides wrap to the exact same number of lines. The only difference is width. Pretext binary-searches for the minimum container width that produces the same line count — no DOM reads, no reflow, just arithmetic.

Demo 2: Masonry grids that actually work

Masonry layouts (Pinterest-style) have been a CSS holy grail for years. The problem: to position a card without a gap, you must know the height of every card above it. Without text measurement, developers guess, and visible gaps appear when guesses are wrong.

Pretext-powered masonry, no DOM reads
6
AI-generated copy shouldn't require fixed-height containers. Now it doesn't.
Masonry at 120fps.
Shrinkwrapped chat bubbles, the way they were always supposed to look.
Layout without reflow. Sounds impossible. Isn't.
Designers can now spec things engineers can actually build fast.
No more 'we can't because of reflow'.
120fps, no DOM reads on resize

Pretext measures all items in one pass, then positions them perfectly at 120fps, even as you resize the window. No layout thrashing. No red bars in Chrome DevTools.

Demo 3: Dynamic truncation with precision

Truncating text with CSS ellipsis is blunt. You set a fixed height and cut. With Pretext, you can truncate based on actual line count at any container width, and add a “read more” exactly where the real break is.

Line-accurate truncation
400px
2

Pretext is a pure TypeScript library that computes text layout without touching the DOM. It tells you exactly how many lines a block of text occupies at a given container width, and it does this at a speed that makes real-time, responsive layout computation practical for the first time. No reflow. No synchronized layout reads. Just math, running entirely in userland, at the speed of a JavaScript function call.

Read more

Truncation recomputes instantly. No reflow triggered.

Already shipping in Cursor

This isn’t theoretical. Cursor, the AI code editor, already uses Pretext to truncate file paths in the middle of their diff panel. The filename is always readable, no matter how narrow the panel gets.

GitDesktopTerminalFiles
portal-website/apps/web/src/comp…code-changes-panel.test.tsx
+128-3
portal-website/apps/web/src/comp…code-changes-panel.tsx
+61-14
portal-website/apps/web/src/comp…review-diff-card.test.tsx
+77
1import { fireEvent, render, screen, waitFor } from "@testing-library/react";
2import { useState } from "react";
3import { beforeEach, describe, expect, it, vi } from "vitest";
4import { ReviewDiffCard } from "@components/background-shared/review-diff-card";

Cursor truncates file paths in the middle using Pretext, keeping the filename always visible.

Before Pretext, the only options were: truncate from the end (losing the filename) or accept overflow. Now there’s a third option: measure precisely, truncate smartly.

What this unlocks for designers

For years, the split between “beautiful but slow” and “fast but boxy” has been a real tradeoff. Editorial layouts with flowing text, magazine-style columns, perfectly-hugged labels, all of these required expensive DOM measurements that made them impractical at scale.

Before Pretext

Text height = guess or reflow. Dense UIs required fixed heights. Dynamic content caused jank. AI chat interfaces with variable-length outputs were especially painful.

After Pretext

Text height = computed in userland. Virtualize 100k cards. Shrinkwrap any element. Lay out entire pages without touching the DOM. AI UIs can resize at 120fps.

The AI connection

This matters especially now. AI-generated content is inherently variable; answers can be two lines or twenty. Chat interfaces, AI writing tools, and generative dashboards all suffer when containers can’t predict content height. Pretext makes variable-length AI outputs as easy to lay out as static text.

Designer takeaway: You can now spec layouts that hug their content, scroll virtual lists of thousands of AI responses, and build magazine-quality compositions, without handing engineers an unsolvable performance problem. The constraint that made you choose safe layouts is gone.

Based on Cheng Lou’s Pretext library, a fast, accurate, comprehensive text measurement algorithm in pure TypeScript. chenglou.me/pretext →

React
Share
More writing
You Learned the AI Tools. You're Still Getting Replaced. Here's Why.
Senior designers, freelancers, fresh graduates — the anxiety is universal. But a specific group is n...
Apr 3, 2026