Resizable Text
Words in a sentence you resize like a text layer in a design tool.
Customize
<ResizableText children={children} />Changes show live and stay in the address, so a link keeps them.
Code
"use client";
import { useLayoutEffect, useRef, useState, type CSSProperties, type PointerEvent } from "react";
// Words shown as a selected text layer in a design tool. Pull a corner and the text grows, push it back and it shrinks.
// Hover it for a small toolbar above it that switches the font: the box glides to the new width while the old letters
// fade out and the new ones fade in. A size readout sits under the box, like in Figma. The letters can arrive in
// colour first and settle into ink.
//
// While a corner is held, the size is written straight onto the element once per frame (no re-render, no easing), so
// the corner stays under the pointer. The words sit in a sentence, so they always grow to the right, and they stop
// growing at the end of their line: they are never pushed down onto the next line, by a drag or by a wider font.
export type ResizableTextFont = {
/** A short id, e.g. "serif". */
id: string;
/** Its name for screen readers, e.g. "Serif". */
label: string;
/** The CSS font-family. */
family: string;
/** "italic" for an italic face. */
style?: "normal" | "italic";
/** Letter spacing, e.g. "-0.01em". */
letterSpacing?: string;
};
export type ResizableTextProps = {
/** The words in the layer. */
children: string;
/** The starting size in px. A drag never makes it smaller than this. */
size?: number;
/** The largest size a drag can reach, in px. It also stops where the words would wrap. */
max?: number;
/** The fonts the toolbar switches between. The first one is the starting font. */
fonts?: ResizableTextFont[];
/** The colour of the frame, the corner handles and the size readout. */
color?: string;
/** The letters arrive as soft colour first, then settle into ink. */
reveal?: boolean;
/** Extra classes on the layer. */
className?: string;
};
export const resizableTextFonts: ResizableTextFont[] = [
{ id: "serif", label: "Serif", family: 'ui-serif, Georgia, "Times New Roman", serif', style: "italic", letterSpacing: "0" },
{ id: "sans", label: "Sans", family: 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif', letterSpacing: "-0.01em" },
{ id: "mono", label: "Mono", family: "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace", letterSpacing: "-0.03em" },
];
const MORPH = 450; // ms for the box to glide to a new font's width
const PAD = 8; // the layer's side padding (4 px each side) does not grow with the text
const corners = [
{ id: "tl", x: -1, y: -1, cursor: "nwse-resize" },
{ id: "tr", x: 1, y: -1, cursor: "nesw-resize" },
{ id: "bl", x: -1, y: 1, cursor: "nesw-resize" },
{ id: "br", x: 1, y: 1, cursor: "nwse-resize" },
] as const;
const faceOf = (font: ResizableTextFont): CSSProperties => ({ fontFamily: font.family, fontStyle: font.style ?? "normal", letterSpacing: font.letterSpacing ?? "normal" });
export function ResizableText({ children, size: start = 17, max = 72, fonts = resizableTextFonts, color = "#3b82f6", reveal = true, className = "" }: ResizableTextProps) {
const list = fonts.length ? fonts : resizableTextFonts;
const [size, setSize] = useState(start);
const [dragging, setDragging] = useState(false);
const [base, setBase] = useState({ width: 0, height: 0 });
const [fontId, setFontId] = useState(list[0]!.id);
const [swaps, setSwaps] = useState(0);
const [ghost, setGhost] = useState<{ font: ResizableTextFont; id: number } | null>(null);
const box = useRef<HTMLSpanElement>(null);
const readout = useRef<HTMLSpanElement>(null);
const drag = useRef({ pointer: -1, size: start, from: start, sx: 1, sy: 1, w: 0, h: 0, startX: 0, startY: 0, x: 0, y: 0, max, frame: 0 });
const before = useRef<DOMRect | null>(null);
const settle = useRef<(() => void) | null>(null);
const font = list.find((option) => option.id === fontId) ?? list[0]!;
// Let go of a font glide early, so a resize never fights a fixed width
const release = () => settle.current?.();
const describe = (value: number, measured = base) =>
measured.width > 0 && value > start + 0.05 ? `${Math.round((measured.width * value) / start)} × ${Math.round((measured.height * value) / start)}` : "Hug × Hug";
// The size and the readout belong to the element itself, so nothing else ever writes over a drag in progress
useLayoutEffect(() => {
if (box.current && drag.current.pointer === -1) box.current.style.fontSize = `${size}px`;
}, [size]);
useLayoutEffect(() => {
if (readout.current && drag.current.pointer === -1) readout.current.textContent = describe(drag.current.size);
});
// One frame's worth of pointer movement: the drag along the box's diagonal decides how much wider it gets
const apply = () => {
const state = drag.current;
state.frame = 0;
const node = box.current;
if (!node || state.pointer === -1) return;
const dx = (state.x - state.startX) * state.sx;
const dy = (state.y - state.startY) * state.sy;
const grow = ((dx * state.w + dy * state.h) * state.w) / (state.w * state.w + state.h * state.h);
const next = Math.min(state.max, Math.max(start, (state.from * (state.w + grow - PAD)) / (state.w - PAD)));
if (Math.abs(next - state.size) < 0.01) return;
state.size = next;
node.style.fontSize = `${next}px`;
if (readout.current) readout.current.textContent = describe(next, { width: (state.w * start) / state.from, height: (state.h * start) / state.from });
};
const grab = (event: PointerEvent<HTMLElement>, dirX: number, dirY: number) => {
release();
const node = box.current;
const line = node?.parentElement;
if (!node || !line) return;
const state = drag.current;
cancelAnimationFrame(state.frame); // a drag whose release was missed never blocks the next one
event.preventDefault();
try {
event.currentTarget.setPointerCapture(event.pointerId);
} catch {
return; // not a live pointer, so there is nothing to follow
}
const rect = node.getBoundingClientRect();
const now = state.size;
const lineEnd = line.getBoundingClientRect().right;
Object.assign(state, {
pointer: event.pointerId,
from: now,
sx: dirX,
sy: dirY,
w: rect.width,
h: rect.height,
startX: event.clientX,
startY: event.clientY,
x: event.clientX,
y: event.clientY,
frame: 0,
// as wide as the rest of its line allows (3 px spare, so text rounding never tips it onto the next line)
max: Math.max(now, Math.min(max, (now * (lineEnd - rect.left - 3 - PAD)) / (rect.width - PAD))),
});
setBase({ width: (rect.width * start) / now, height: (rect.height * start) / now });
setDragging(true);
};
const pull = (event: PointerEvent<HTMLElement>) => {
const state = drag.current;
if (state.pointer !== event.pointerId) return;
state.x = event.clientX;
state.y = event.clientY;
if (!state.frame) state.frame = requestAnimationFrame(apply);
};
const letGo = (event: PointerEvent<HTMLElement>) => {
const state = drag.current;
if (state.pointer !== event.pointerId) return;
if (state.frame) {
cancelAnimationFrame(state.frame);
apply(); // the last movement still counts
}
state.pointer = -1;
setSize(state.size);
setDragging(false);
};
// The readout shows the layer's real size, measured when the pointer comes over it and again after a font switch
const measure = () => {
const rect = box.current?.getBoundingClientRect();
if (rect && drag.current.pointer === -1) setBase({ width: (rect.width * start) / drag.current.size, height: (rect.height * start) / drag.current.size });
};
// Remember the box as it is now, and the layout effect below glides it to the new font's size
const pick = (next: ResizableTextFont) => {
if (next.id === font.id || !box.current) return;
release();
before.current = box.current.getBoundingClientRect();
setGhost({ font, id: swaps + 1 });
setFontId(next.id);
setSwaps((count) => count + 1);
};
useLayoutEffect(() => {
const node = box.current;
const from = before.current;
if (!node || !from) return;
before.current = null;
node.style.transition = "none";
node.style.width = "";
node.style.height = "";
// Fonts are not the same width: at the same size a wider one may no longer fit on the line the words sit on. Then
// it comes down to the largest size that still fits there, so a font switch never drops the words onto the next
// line (or lifts them back up), whatever size they were dragged to.
const line = node.parentElement;
if (line) {
const room = line.getBoundingClientRect().right - from.left - 3;
const natural = node.getBoundingClientRect().width;
const now = drag.current.size;
const fit = Math.max(start, (now * (room - PAD)) / (natural - PAD));
if (natural > room + 0.5 && fit < now - 0.01) {
drag.current.size = fit;
node.style.fontSize = `${fit}px`;
}
}
const to = node.getBoundingClientRect();
const moves = Math.abs(to.width - from.width) > 0.5 ? "width" : Math.abs(to.height - from.height) > 0.5 ? "height" : null;
let fallback = 0;
// When the glide is over, the box goes back to sizing itself and the readout is measured again
const done = () => {
settle.current = null;
window.clearTimeout(fallback);
node.removeEventListener("transitionend", ended);
node.classList.remove("is-morphing");
node.style.transition = "";
node.style.width = "";
node.style.height = "";
const rect = node.getBoundingClientRect();
if (drag.current.pointer === -1) setBase({ width: (rect.width * start) / drag.current.size, height: (rect.height * start) / drag.current.size });
};
const ended = (event: TransitionEvent) => {
if (event.target === node && event.propertyName === moves) done();
};
if (!moves) {
done();
return;
}
node.classList.add("is-morphing");
node.style.width = `${from.width}px`;
node.style.height = `${from.height}px`;
void node.offsetWidth; // start from the old size
node.style.transition = `width ${MORPH}ms var(--rt-glide), height ${MORPH}ms var(--rt-glide)`;
node.style.width = `${to.width}px`;
node.style.height = `${to.height}px`;
node.addEventListener("transitionend", ended);
fallback = window.setTimeout(done, MORPH * 4); // in case the end event never comes
settle.current = done;
return () => {
window.clearTimeout(fallback);
node.removeEventListener("transitionend", ended);
};
}, [fontId, start]);
return (
<span
ref={box}
className={`rt-layer ${dragging ? "is-dragging" : ""} ${className}`}
style={{ ...faceOf(font), "--rt-color": color } as CSSProperties}
onPointerEnter={measure}
>
<style href="dimaac-resizable-text" precedence="default">
{css}
</style>
<span className="rt-sr">{children}</span>
<span className="rt-text">
<span aria-hidden="true" className={swaps ? (swaps % 2 ? "rt-swap-a" : "rt-swap-b") : undefined}>
{[...children].map((letter, index) => (
<span key={index} className={reveal ? "rt-letter" : undefined} style={{ "--i": index } as CSSProperties}>
{letter}
</span>
))}
</span>
{ghost ? (
<span key={ghost.id} aria-hidden="true" className="rt-ghost" style={faceOf(ghost.font)} onAnimationEnd={() => setGhost(null)}>
{children}
</span>
) : null}
</span>
{corners.map((corner) => (
<i
key={corner.id}
aria-hidden="true"
className={`rt-handle rt-handle-${corner.id}`}
style={{ cursor: corner.cursor }}
onPointerDown={(event) => grab(event, corner.x, corner.y)}
onPointerMove={pull}
onPointerUp={letGo}
onPointerCancel={letGo}
onLostPointerCapture={letGo}
/>
))}
{list.length > 1 ? (
<span className="rt-bar">
<span className="rt-fonts" role="group" aria-label="Font">
{list.map((option) => (
<button key={option.id} type="button" aria-pressed={font.id === option.id} aria-label={option.label} style={faceOf(option)} onClick={() => pick(option)}>
Aa
</button>
))}
</span>
</span>
) : null}
<span ref={readout} className="rt-readout" aria-hidden="true" />
</span>
);
}
const DARK = `:where(html[data-theme="dark"],html.dark)`;
const css = `
.rt-layer {
--rt-glide: cubic-bezier(0.2, 0.8, 0.2, 1);
position: relative;
display: inline-block;
padding: 0 4px;
line-height: 1.05;
white-space: nowrap;
box-shadow: 0 0 0 1px var(--rt-color);
transition: opacity 0.3s;
}
.rt-layer:hover,
.rt-layer.is-dragging {
z-index: 6;
}
.rt-sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
.rt-text {
position: relative;
display: block;
}
.rt-layer.is-morphing .rt-text {
overflow: clip;
overflow-clip-margin: 3px;
}
.rt-letter {
animation:
rt-tint-fade 0.3s ease-out backwards,
rt-tint-in 0.7s cubic-bezier(0.55, 0.085, 0.68, 0.53) backwards;
animation-delay: calc(0.4s + var(--i) * 0.03s);
}
@keyframes rt-tint-fade {
from { opacity: 0; }
}
@keyframes rt-tint-in {
from {
color: transparent;
text-shadow: 0 0 8px hsl(calc(180 + var(--i) * 15) 100% 60%);
}
}
.rt-swap-a { animation: rt-swap-a 0.4s var(--rt-glide); }
.rt-swap-b { animation: rt-swap-b 0.4s var(--rt-glide); }
@keyframes rt-swap-a {
from { opacity: 0.3; filter: blur(5px); }
}
@keyframes rt-swap-b {
from { opacity: 0.3; filter: blur(5px); }
}
.rt-ghost {
position: absolute;
top: 0;
left: 0;
white-space: nowrap;
pointer-events: none;
animation: rt-ghost-out 0.32s ease-out forwards;
}
@keyframes rt-ghost-out {
to { opacity: 0; filter: blur(4px); }
}
.rt-bar {
position: absolute;
bottom: 100%;
left: 50%;
z-index: 7;
display: flex;
padding-bottom: 12px;
font-size: 11px;
line-height: 1;
letter-spacing: 0;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 3px);
transition: opacity 0.2s, transform 0.3s var(--rt-glide);
}
.rt-layer:hover .rt-bar,
.rt-layer.is-dragging .rt-bar,
.rt-layer:has(:focus-visible) .rt-bar {
opacity: 1;
pointer-events: auto;
transform: translate(-50%, 0);
}
.rt-fonts {
display: flex;
gap: 2px;
padding: 3px;
border-radius: 9px;
background: #fff;
box-shadow: 0 0 0 1px rgb(0 0 0 / 0.08), 0 8px 18px -10px rgb(0 0 0 / 0.3);
}
.rt-fonts button {
display: grid;
place-items: center;
width: 28px;
height: 22px;
padding: 0;
border: 0;
border-radius: 6px;
background: none;
color: #a1a19d;
font-size: 12px;
line-height: 1;
cursor: pointer;
transition: background-color 0.2s, color 0.2s;
}
.rt-fonts button:hover,
.rt-fonts button[aria-pressed="true"] {
color: #161615;
}
.rt-fonts button[aria-pressed="true"] {
background: #f2f2ef;
}
.rt-handle {
position: absolute;
z-index: 8;
width: 7px;
height: 7px;
border: 1px solid var(--rt-color);
background: #fff;
touch-action: none;
}
.rt-handle::before {
content: "";
position: absolute;
inset: -9px;
}
.rt-handle-tl { top: -4px; left: -4px; }
.rt-handle-tr { top: -4px; right: -4px; }
.rt-handle-bl { bottom: -4px; left: -4px; }
.rt-handle-br { right: -4px; bottom: -4px; }
.rt-readout {
position: absolute;
top: calc(100% + 7px);
left: 50%;
z-index: 7;
padding: 3px 5px;
border-radius: 3px;
background: var(--rt-color);
color: #fff;
font: normal 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0;
white-space: nowrap;
pointer-events: none;
opacity: 0;
transform: translate(-50%, -2px);
transition: opacity 0.2s, transform 0.3s var(--rt-glide);
}
.rt-layer:hover .rt-readout,
.rt-layer.is-dragging .rt-readout,
.rt-layer:has(:focus-visible) .rt-readout {
opacity: 1;
transform: translate(-50%, 0);
}
${DARK} .rt-fonts {
background: #1f1f1d;
box-shadow: 0 0 0 1px rgb(255 255 255 / 0.1), 0 8px 18px -10px rgb(0 0 0 / 0.6);
}
${DARK} .rt-fonts button {
color: #7d7d78;
}
${DARK} .rt-fonts button:hover,
${DARK} .rt-fonts button[aria-pressed="true"] {
color: #ededea;
}
${DARK} .rt-fonts button[aria-pressed="true"] {
background: #2c2c2a;
}
${DARK} .rt-handle {
background: #111110;
}
@media (prefers-reduced-motion: reduce) {
.rt-letter, .rt-swap-a, .rt-swap-b { animation: none; }
.rt-ghost { display: none; }
.rt-layer, .rt-bar, .rt-readout, .rt-fonts button { transition-duration: 0.01ms; }
}
`;
The component. Installs to components/dimaac/resizable-text.tsx.
Install
With the shadcn CLI
npx shadcn@latest add https://ui.dimaac.com/r/resizable-text.jsonIt adds components/dimaac/resizable-text.tsx and installs what it imports. With the @dimaac namespace in components.json (Docs) it's just npx shadcn@latest add @dimaac/resizable-text.
By hand
# Nothing to install: it only needs React- It needs nothing but React 19.
- Copy the component file below into components/dimaac/resizable-text.tsx.
- It's styled with Tailwind v4 classes. If that folder sits outside the places Tailwind scans, add it with @source in your CSS.
- Import it with import { ResizableText } from "@/components/dimaac/resizable-text" and give its parent a size: it fills it.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
children (required) | string | - | The words in the layer. |
size | number | - | The starting size in px. A drag never makes it smaller than this. |
max | number | 72 | The largest size a drag can reach, in px. It also stops where the words would wrap. |
fonts | ResizableTextFont[] | resizableTextFonts | The fonts the toolbar switches between. The first one is the starting font. |
color | string | "#3b82f6" | The colour of the frame, the corner handles and the size readout. |
reveal | boolean | true | The letters arrive as soft colour first, then settle into ink. |
className | string | "" | Extra classes on the layer. |
Use with AI
Agent prompt
Paste it into Claude Code, Cursor or any coding agent, or open it straight in a chat. It carries your Customize settings.
Add the Resizable Text component (<ResizableText />) from DiMaac UI to this project.
Docs: https://ui.dimaac.com/components/resizable-text
Source and dependencies (shadcn registry JSON): https://ui.dimaac.com/r/resizable-text.json
Install it with:
npx shadcn@latest add https://ui.dimaac.com/r/resizable-text.json
Then use it like this:
import { ResizableText } from "@/components/dimaac/resizable-text";
<ResizableText children={children} />
If the shadcn CLI isn't set up here, fetch the registry JSON above, write files[0].content to components/dimaac/resizable-text.tsx.
It uses Tailwind v4 classes, so make sure Tailwind scans that folder. Give its parent a size, keep light and dark mode working and keep its prefers-reduced-motion fallback.
Full index for agents: https://ui.dimaac.com/llms.txtFiles agents read
- llms.txt
- Every component and how to install it
- resizable-text.md
- This page as Markdown: install, props and the full source
- resizable-text.json
- The shadcn registry item
About
A few words in a sentence drawn as a selected text layer, with a blue frame and four corner handles. Pull a corner and the words grow or shrink with the pointer, like scaling text in Figma; they stop at the end of their line, so the sentence never jumps. Point at them for a small toolbar that switches the font while the box glides to its new width, and a size readout under the box. Plain React, sized once per frame while you drag. With reduced motion the colour reveal and the glide are off.
- Installs to
components/dimaac/resizable-text.tsx- Uses
- React only
- Tags
- text, resize, typography, design-tool, interactive