Morphing Text
A morphing text animation for React and Tailwind CSS: words melt into each other with a gooey SVG threshold filter or a soft blur crossfade. Ideal for hero headlines, it reserves the widest word so nothing shifts, keeps every word as real text for screen readers and SEO, and stays still for reduced-motion users.
- Gooey SVG filter
- Blur crossfade
- No layout shift
- SEO-friendly real text
- prefers-reduced-motion
Design, Prototype, Build, Ship
import { MorphingText } from "@/components/dusk/morphing-text/morphing-text";
export default function MorphingTextDemo() {
return (
<MorphingText
as="p"
words={["Design", "Prototype", "Build", "Ship"]}
className="text-6xl font-semibold tracking-tight text-white sm:text-7xl"
/>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/morphing-text.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/morphing-text/morphing-text.tsx.morphing-text.tsx "use client"; import * as React from "react"; import { animate, useInView, usePageInView, useReducedMotion, type AnimationPlaybackControls, } from "motion/react"; import { cn } from "@/lib/utils"; type MorphingTextTag = "span" | "strong" | "em" | "p" | "div" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; export interface MorphingTextProps extends Omit<React.HTMLAttributes<HTMLElement>, "children"> { /** Words or short phrases to cycle through. The first one is server-rendered. */ words: string[]; /** Milliseconds from one word to the next, morph included (minimum: morph + 600). */ interval?: number; /** Length of the morph in milliseconds. */ morphDuration?: number; /** * - `"gooey"` (default): an SVG threshold filter melts the letters into each other. * - `"blur"`: a soft blur crossfade, better for thin fonts. */ variant?: "gooey" | "blur"; /** Element to render. */ as?: MorphingTextTag; /** Horizontal alignment of shorter words inside the reserved width. */ align?: "start" | "center" | "end"; /** Reserve the widest word's width so nothing around the text moves. */ reserveSpace?: boolean; /** * How assistive tech gets the words: * - `"all"` (default): every word once, as visually hidden text (also best for SEO). * - `"polite"`: the current word is a polite live region, announced on each change. */ announce?: "all" | "polite"; /** Classes for each word, e.g. a color. */ wordClassName?: string; } const alignments = { start: "justify-items-start text-left", center: "justify-items-center text-center", end: "justify-items-end text-right", } as const; /** Blur (px) and opacity for a word that is `f` (0–1) of the way in. */ function frame(f: number, maxBlur: number) { if (f <= 0) return { blur: maxBlur, opacity: 0 }; return { blur: Math.min(8 / f - 8, maxBlur), opacity: f ** 0.4 }; } /** * Cycles through words by melting one into the next: the outgoing word blurs * away while the incoming one sharpens, and an SVG threshold filter turns the * overlapping blur into liquid, gooey letterforms. The filter is only applied * during the morph, so resting text stays crisp. * * Frames are written straight to the DOM (no React render per frame). It pauses * off-screen and in background tabs, and shows only the first word when the * user prefers reduced motion. Every word stays available to screen readers. */ export function MorphingText({ words, ...props }: MorphingTextProps) { // Remount on a new word list so the imperative frames start clean. return <MorphingTextInner key={words.join("\u0000")} words={words} {...props} />; } function MorphingTextInner({ words: wordsProp, interval = 3000, morphDuration = 1000, variant = "gooey", as = "span", align = "center", reserveSpace = true, announce = "all", wordClassName, className, style, ...props }: MorphingTextProps) { const Comp = as as React.ElementType; // The parent keys this component by content, so the first list is the list for its lifetime. const [words] = React.useState(wordsProp); const rootRef = React.useRef<HTMLElement>(null); const outRef = React.useRef<HTMLSpanElement>(null); const inRef = React.useRef<HTMLSpanElement>(null); const indexRef = React.useRef(0); const filterId = `morph-${React.useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; const reduceMotion = useReducedMotion(); const pageVisible = usePageInView(); const inView = useInView(rootRef, { amount: 0.4 }); const [liveIndex, setLiveIndex] = React.useState(0); const count = words.length; const running = count > 1 && !reduceMotion && inView && pageVisible; const gooey = variant === "gooey"; const maxBlur = gooey ? 100 : 16; React.useEffect(() => { if (!running) return; const root = rootRef.current; const outEl = outRef.current; const inEl = inRef.current; if (!root || !outEl || !inEl) return; let controls: AnimationPlaybackControls | undefined; let timer: ReturnType<typeof setTimeout>; let morphing = false; const morphMs = Math.max(200, morphDuration); const holdMs = Math.max(600, interval - morphMs); const paint = (f: number) => { const a = frame(1 - f, maxBlur); const b = frame(f, maxBlur); outEl.style.filter = `blur(${a.blur}px)`; outEl.style.opacity = String(a.opacity); inEl.style.filter = `blur(${b.blur}px)`; inEl.style.opacity = String(b.opacity); }; // Promote the incoming word to the resting slot in one synchronous step: no flash. const settle = () => { if (!morphing) return; morphing = false; const next = (indexRef.current + 1) % count; indexRef.current = next; outEl.textContent = words[next]; outEl.style.filter = ""; outEl.style.opacity = ""; inEl.textContent = words[(next + 1) % count]; inEl.style.filter = ""; inEl.style.opacity = "0"; if (gooey) root.style.removeProperty("filter"); if (announce === "polite") setLiveIndex(next); }; const schedule = () => { timer = setTimeout(() => { morphing = true; if (gooey) root.style.filter = `url(#${filterId}) blur(0.4px)`; controls = animate(0, 1, { duration: morphMs / 1000, ease: "linear", onUpdate: paint, onComplete: () => { settle(); schedule(); }, }); }, holdMs); }; schedule(); return () => { clearTimeout(timer); controls?.stop(); // Paused mid-morph (scrolled away, tab hidden): finish on the incoming word. settle(); }; }, [running, count, words, interval, morphDuration, gooey, maxBlur, filterId, announce]); const stack = "[grid-area:1/1] whitespace-nowrap"; return ( <Comp ref={rootRef} data-slot="morphing-text" className={cn( "relative inline-grid", as === "span" || as === "strong" || as === "em" ? "" : "w-full", alignments[align], className )} style={style} {...props} > {announce === "all" && <span className="sr-only">{words.join(", ")}</span>} {announce === "polite" && ( <span className="sr-only" aria-live="polite"> {words[liveIndex]} </span> )} {reserveSpace && words.map((word, i) => ( <span key={i} aria-hidden className={cn(stack, "invisible select-none", wordClassName)}> {word} </span> ))} <span ref={outRef} aria-hidden className={cn(stack, wordClassName)}> {words[0]} </span> {count > 1 && ( <span ref={inRef} aria-hidden className={cn(stack, "select-none", wordClassName)} style={{ opacity: 0 }}> {words[1]} </span> )} {gooey && ( <svg aria-hidden className="pointer-events-none absolute size-0" focusable="false"> <defs> <filter id={filterId}> {/* Snap alpha to fully on/off so overlapping blur fuses into one shape. */} <feColorMatrix in="SourceGraphic" type="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 255 -140" /> </filter> </defs> </svg> )} </Comp> ); }
Usage
import { MorphingText } from "@/components/dusk/morphing-text/morphing-text"
export function Hero() {
return (
<h1 className="text-5xl font-bold">
Build{" "}
<MorphingText words={["faster", "smarter", "together"]} align="start" className="text-muted-foreground" />
</h1>
)
}Examples
Inside a headline
Rendered as a span it sits inline in a sentence. align="start" keeps short words next to the text before them, and the reserved width stops the line from reflowing.
Interfaces that feel fast, fluid, effortless, yours
import { MorphingText } from "@/components/dusk/morphing-text/morphing-text";
export default function MorphingTextHeadlineDemo() {
return (
<h2 className="max-w-lg text-center text-3xl font-semibold tracking-tight sm:text-4xl">
Interfaces that feel{" "}
<MorphingText
words={["fast", "fluid", "effortless", "yours"]}
align="start"
interval={2600}
className="text-white/60"
/>
</h2>
);
}Gooey vs blur
gooey fuses the letters into liquid shapes and works best with bold, solid-color type. blur is a plain blur crossfade that also suits thin weights.
Design, Build, Ship, Repeat
Design, Build, Ship, Repeat
import { MorphingText } from "@/components/dusk/morphing-text/morphing-text";
const words = ["Design", "Build", "Ship", "Repeat"];
export default function MorphingTextVariantsDemo() {
return (
<div className="grid w-full max-w-xl gap-8 sm:grid-cols-2">
<figure className="flex flex-col items-center gap-3">
<MorphingText as="p" words={words} variant="gooey" className="text-4xl font-bold" />
<figcaption className="font-mono text-xs text-muted-foreground">variant="gooey"</figcaption>
</figure>
<figure className="flex flex-col items-center gap-3">
<MorphingText
as="p"
words={words}
variant="blur"
morphDuration={700}
className="text-4xl font-light"
/>
<figcaption className="font-mono text-xs text-muted-foreground">variant="blur"</figcaption>
</figure>
</div>
);
}API Reference
Also accepts any HTML attribute for the rendered element.
| Prop | Type | Default | Description |
|---|---|---|---|
| words | string[] | — | Words or short phrases to cycle through. The first is server-rendered. |
| interval | number | 3000 | Milliseconds from one word to the next, morph included. |
| morphDuration | number | 1000 | Length of the morph in milliseconds. |
| variant | "gooey" | "blur" | "gooey" | Threshold-filter melt, or a soft blur crossfade. |
| as | "span" | "p" | "div" | "h1" … "h6" | "strong" | "em" | "span" | Element to render. |
| align | "start" | "center" | "end" | "center" | Alignment of shorter words inside the reserved width. |
| reserveSpace | boolean | true | Reserve the widest word's width so surrounding content never moves. |
| announce | "all" | "polite" | "all" | Expose every word once as hidden text, or announce each new word in a polite live region. |
| wordClassName | string | — | Classes for each word, e.g. a color. |
Accessibility
- By default every word is in the page once as visually hidden text and the animated copies are
aria-hidden, so screen readers read a stable sentence and search engines index all words. - With
announce="polite"the current word is announced each time it changes instead. - With prefers-reduced-motion only the first word is shown and nothing animates.
- The cycle pauses when the text is off-screen or the tab is hidden, and the filter is removed between morphs so resting text is crisp.
- Each word is shown for at least 600 ms before the next morph starts.