When to use
- A status that moves on: an order going from Packed to Shipped to Out for delivery.
- A button or link label that answers an action: Copy link to Copied, Save to Saved.
- A price, period or short value that changes with a setting, such as Monthly and Yearly pricing.
- Step labels and counts, where
directionshows whether you went forward or back.
When not to use
- Numbers that change often or by small steps, such as a counter or a balance: use Number Roll, which turns each digit.
- Paragraphs or text that wraps: Text Swap never wraps, and is for one line.
- Swapping a whole region of content: use Tabs or render the new content directly.
Installation
pnpm dlx shadcn add https://eased.dev/r/text-swap.jsonnpx shadcn add https://eased.dev/r/text-swap.jsonyarn dlx shadcn add https://eased.dev/r/text-swap.jsonbunx --bun shadcn add https://eased.dev/r/text-swap.jsonThe command adds Text Swap, everything it builds on and Eased’s tokens to your project.
Add the Eased pieces it builds on
Copy each item’s files the same way, or install them with the CLI:
Copy the source into your project
components/eased/text-swap/text-swap.tsxTSX "use client";/** * Text Swap: change a label, status or short value with y + blur, and spring the width to fit. * The engine lives in `_primitives/text-swap`, shared with Button, Field messages and toasts. */export { TextSwap, type TextSwapProps } from "../_primitives/text-swap/text-swap";Keep the folder layout
The files import each other by relative path (
../lib/motion), so keep each one at the path shown, undercomponents/eased/.
Usage
import { TextSwap } from "@/components/eased/text-swap/text-swap";export function OrderStatus({ status }: { status: string }) { return ( <p aria-live="polite"> Your order is <TextSwap>{status}</TextSwap> </p> );}Examples
Elements, with a swap key
When the children are elements, such as an icon and a label, pass swapKey so it knows when the content is new.
<button onClick={copy}> <TextSwap swapKey={copied ? "copied" : "copy"}> {copied ? <><CheckIcon /> Copied</> : "Copy link"} </TextSwap></button>Direction
Pass direction={-1} for going back, or for a value going down, so the text travels the other way.
<TextSwap direction={plan === "yearly" ? 1 : -1}>{plan === "yearly" ? "$96" : "$10"}</TextSwap>API reference
TextSwap
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | The text, or an icon and text, to show. Strings and numbers key themselves. | |
swapKey | string | number | children | What counts as new content. Pass it when children are elements. |
direction | 1 | -1 | 1 | 1: the new text rises from below and the old leaves upward. -1 reverses it. |
animateWidth | boolean | true | Spring the box's width to fit the new text. |
initial | boolean | false | Animate the first render too. |
Every other <span> prop passes through to the outer span, including className and style.
Accessibility
- The outgoing copy is
aria-hiddenwhile it leaves, so screen readers only ever find the current text. - Text Swap does not announce changes itself. Put
aria-live="polite"on a parent when the change should be read out, such as a status.
Motion
The outgoing text lifts 3px and blurs out in 100ms; the incoming text rises from 4px below on the snappy spring while its opacity and 4px blur clear over 150ms, on their own clock so the blur never overshoots. With direction={-1} both travel the other way. The box's width springs to the new text on the smooth spring, and a swap that interrupts another continues from the current width. Reduced motion is a 100ms cross-fade and the width changes at once.
Notes for AI
Use TextSwap around any short label or value that changes in response to state: button labels, statuses, prices. Strings key themselves; pass swapKey when the children are elements, and direction={-1} for back or down.
Do not use it for text that wraps or for numbers that tick, and do not wrap it in your own AnimatePresence or width transition: it already handles both. Eased's Button already swaps string labels (and element labels given swapKey) with the same engine, so pass the label to Button directly rather than wrapping it in TextSwap.
Related components
- Animated Number Counter: digits that roll to a new value.
- Animated Button: its label swaps with the same motion.
- Shimmer Text Effect