Text Animate
A text animation component for React and Tailwind CSS with six presets (fade in, blur in, slide up, slide down, scale up and blur in up) that animates by character, word or line, on mount or when scrolled into view. The finished text is server-rendered for SEO, line mode follows how the text really wraps, and the heading level is yours to choose.
- 6 presets
- Character, word or line
- On mount or in view
- Server-rendered text
- prefers-reduced-motion
After hours, on purpose.
import { TextAnimate } from "@/components/dusk/text-animate/text-animate";
export default function TextAnimateDemo() {
return (
<TextAnimate
as="h2"
by="character"
animation="blurInUp"
className="text-center text-5xl font-bold tracking-tight sm:text-6xl"
>
After hours, on purpose.
</TextAnimate>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/text-animate.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/text-animate/text-animate.tsx.text-animate.tsx "use client"; import * as React from "react"; import { useAnimate, useInView, useReducedMotion, type AnimationPlaybackControls, type DOMKeyframesDefinition, } from "motion/react"; import { cn } from "@/lib/utils"; type TextAnimateTag = "span" | "p" | "div" | "strong" | "em" | "blockquote" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; export type TextAnimatePreset = "fadeIn" | "blurIn" | "slideUp" | "slideDown" | "scaleUp" | "blurInUp"; export interface TextAnimateProps extends Omit<React.HTMLAttributes<HTMLElement>, "children"> { /** The text to animate. Plain text; `\n` starts a new line. */ children: string; /** Element to render. Use a heading level for titles. */ as?: TextAnimateTag; /** How each piece enters. */ animation?: TextAnimatePreset; /** Split into characters, words, or the lines the text actually wraps onto. */ by?: "character" | "word" | "line"; /** Start when the component mounts, or when it scrolls into view. */ trigger?: "mount" | "inView"; /** Seconds before the first piece starts. */ delay?: number; /** Seconds each piece takes. Defaults to 0.45 / 0.6 / 0.75 for character / word / line. */ duration?: number; /** Seconds between pieces. Defaults to 0.025 / 0.06 / 0.14 for character / word / line. */ stagger?: number; /** In `"inView"` mode, play only the first time. When `false` it replays on every entry. */ once?: boolean; /** How much of the element must be visible to start (0–1). */ amount?: number; /** Classes for every animated piece, e.g. a color. */ segmentClassName?: string; } const HIDDEN: Record<TextAnimatePreset, DOMKeyframesDefinition> = { fadeIn: { opacity: 0 }, blurIn: { opacity: 0, filter: "blur(6px)" }, slideUp: { opacity: 0, transform: "translateY(0.35em)" }, slideDown: { opacity: 0, transform: "translateY(-0.35em)" }, scaleUp: { opacity: 0, transform: "scale(0.9)" }, blurInUp: { opacity: 0, filter: "blur(6px)", transform: "translateY(0.3em)" }, }; const SHOWN: Record<TextAnimatePreset, DOMKeyframesDefinition> = { fadeIn: { opacity: 1 }, blurIn: { opacity: 1, filter: "blur(0px)" }, slideUp: { opacity: 1, transform: "translateY(0em)" }, slideDown: { opacity: 1, transform: "translateY(0em)" }, scaleUp: { opacity: 1, transform: "scale(1)" }, blurInUp: { opacity: 1, filter: "blur(0px)", transform: "translateY(0em)" }, }; const DEFAULTS = { character: { stagger: 0.025, duration: 0.45 }, word: { stagger: 0.06, duration: 0.6 }, line: { stagger: 0.14, duration: 0.75 }, } as const; const PIECE = "[data-text-piece]"; /** User-perceived characters, so emoji and accented letters aren't split apart. */ function graphemes(text: string): string[] { if (typeof Intl !== "undefined" && "Segmenter" in Intl) { return Array.from(new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(text), (s) => s.segment); } return Array.from(text); } /** Groups pieces by the visual line they sit on (offsetTop ignores the entrance transforms). */ function groupByLine(pieces: HTMLElement[]): HTMLElement[][] { const lines: HTMLElement[][] = []; let lastTop = -Infinity; for (const piece of pieces) { const top = piece.offsetTop; if (top > lastTop + 2 || !lines.length) { lines.push([piece]); lastTop = top; } else { lines[lines.length - 1].push(piece); } } return lines; } /** * One text animation component with presets: fade, blur, slide up or down, * scale up and blur-in-up, by character, word or visual line, on mount or * when scrolled into view. * * The server renders the finished text so crawlers and no-JS visitors see it; * pieces are hidden just before the first client paint and animated with * Motion directly on the DOM. Line mode measures where the text really wraps, * so it follows every breakpoint. Reduced motion shows the text at once. */ export function TextAnimate({ children, as = "p", animation = "fadeIn", by = "word", trigger = "inView", delay = 0, duration, stagger, once = true, amount = 0.3, segmentClassName, className, ...props }: TextAnimateProps) { const Comp = as as React.ElementType; const [scope, animate] = useAnimate<HTMLElement>(); const inView = useInView(scope, { once, amount }); const reduceMotion = useReducedMotion(); const played = React.useRef(false); const step = stagger ?? DEFAULTS[by].stagger; const length = duration ?? DEFAULTS[by].duration; const active = trigger === "mount" || inView; // Hide the pieces before the first client paint (the server copy stays fully visible). React.useLayoutEffect(() => { if (reduceMotion || played.current) return; animate(PIECE, HIDDEN[animation], { duration: 0 }); }, [animate, animation, reduceMotion, children, by]); React.useEffect(() => { if (reduceMotion) { animate(PIECE, SHOWN[animation], { duration: 0 }); return; } if (!active) { if (!once && played.current) animate(PIECE, HIDDEN[animation], { duration: 0 }); return; } const root = scope.current; if (!root) return; played.current = true; const pieces = Array.from(root.querySelectorAll<HTMLElement>(PIECE)); const groups = by === "line" ? groupByLine(pieces) : pieces.map((piece) => [piece]); const controls: AnimationPlaybackControls[] = groups.map((group, i) => animate(group, SHOWN[animation], { duration: length, delay: delay + i * step, ease: [0.22, 1, 0.36, 1], }) ); return () => controls.forEach((c) => c.stop()); }, [animate, scope, active, once, reduceMotion, animation, by, length, step, delay, children]); const lines = children.split("\n"); const piece = cn("inline-block whitespace-pre", segmentClassName); const renderWords = (line: string, lineIndex: number) => line .split(/(\s+)/) .filter(Boolean) .map((word, i) => { if (/^\s+$/.test(word)) return word; const key = `${lineIndex}-${i}`; if (by !== "character") { return ( <span key={key} data-text-piece className={piece}> {word} </span> ); } // Letters of a word stay together so lines only break between words. return ( <span key={key} className="inline-block whitespace-pre"> {graphemes(word).map((char, j) => ( <span key={j} data-text-piece className={piece}> {char} </span> ))} </span> ); }); const content = lines.map((line, i) => ( <React.Fragment key={i}> {i > 0 && <br />} {renderWords(line, i)} </React.Fragment> )); // Words are real text, read normally. Letter-by-letter markup can be read one character // at a time by some screen readers, so character mode gets a visually hidden copy instead. return ( <Comp ref={scope} data-slot="text-animate" className={className} {...props}> {by === "character" ? ( <> <span className="sr-only">{children}</span> <span aria-hidden>{content}</span> </> ) : ( content )} </Comp> ); }
Usage
import { TextAnimate } from "@/components/dusk/text-animate/text-animate"
export function SectionTitle() {
return (
<TextAnimate as="h2" animation="blurInUp" by="character" className="text-5xl font-bold">
Built for the late shift.
</TextAnimate>
)
}Examples
Presets
animation picks the entrance: fadeIn, blurIn, slideUp, slideDown, scaleUp or blurInUp. Here they run with trigger="mount"; remount to replay.
fadeIn
Quietly brilliant
blurIn
Quietly brilliant
slideUp
Quietly brilliant
slideDown
Quietly brilliant
scaleUp
Quietly brilliant
blurInUp
Quietly brilliant
"use client";
import * as React from "react";
import { RotateCcw } from "lucide-react";
import { TextAnimate, type TextAnimatePreset } from "@/components/dusk/text-animate/text-animate";
const presets: TextAnimatePreset[] = ["fadeIn", "blurIn", "slideUp", "slideDown", "scaleUp", "blurInUp"];
export default function TextAnimatePresets() {
const [run, setRun] = React.useState(0);
return (
<div className="flex w-full max-w-xl flex-col items-center gap-6">
<div key={run} className="grid w-full gap-x-8 gap-y-5 sm:grid-cols-2">
{presets.map((preset, i) => (
<div key={preset}>
<p className="mb-1 font-mono text-xs text-white/55">{preset}</p>
<TextAnimate
animation={preset}
by={i % 2 ? "character" : "word"}
trigger="mount"
className="text-2xl font-semibold tracking-tight"
>
Quietly brilliant
</TextAnimate>
</div>
))}
</div>
<button
type="button"
onClick={() => setRun((n) => n + 1)}
className="inline-flex items-center gap-2 rounded-lg border border-white/10 px-3 py-1.5 text-sm text-white/70 transition-colors hover:text-white focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-none"
>
<RotateCcw className="size-3.5" aria-hidden />
Replay
</button>
</div>
);
}By line
by="line" measures where the text wraps when the animation starts and moves each visual line together. A \n in the string forces a break. With once={false} it replays on every entry.
Line mode measures where this paragraph actually wraps, so each visual line rises together, at every screen width, without you adding a single line break.
import { TextAnimate } from "@/components/dusk/text-animate/text-animate";
export default function TextAnimateLines() {
return (
<TextAnimate
by="line"
animation="slideUp"
once={false}
className="max-w-md text-center text-xl leading-relaxed text-white/75"
>
Line mode measures where this paragraph actually wraps, so each visual line rises together, at every screen
width, without you adding a single line break.
</TextAnimate>
);
}Sequenced heading
Chain several blocks with delay for an eyebrow, title and subtitle. as keeps the real heading structure.
Changelog · v2.0
Faster builds.
Fewer surprises.
A new compiler, smarter caching and a calmer dev server.
import { TextAnimate } from "@/components/dusk/text-animate/text-animate";
export default function TextAnimateHeading() {
return (
<div className="max-w-lg space-y-3 text-center">
<TextAnimate as="p" animation="fadeIn" by="word" className="text-sm font-medium text-white/60">
Changelog · v2.0
</TextAnimate>
<TextAnimate as="h3" animation="slideUp" by="word" delay={0.2} className="text-4xl font-semibold tracking-tight">
{"Faster builds.\nFewer surprises."}
</TextAnimate>
<TextAnimate as="p" animation="blurIn" by="word" delay={0.6} stagger={0.03} className="text-white/60">
A new compiler, smarter caching and a calmer dev server.
</TextAnimate>
</div>
);
}API Reference
Also accepts any HTML attribute for the rendered element.
| Prop | Type | Default | Description |
|---|---|---|---|
| children | string | — | The text to animate. Plain text; \n starts a new line. |
| as | "p" | "span" | "div" | "strong" | "em" | "blockquote" | "h1" … "h6" | "p" | Element to render. Use a heading level for titles. |
| animation | "fadeIn" | "blurIn" | "slideUp" | "slideDown" | "scaleUp" | "blurInUp" | "fadeIn" | How each piece enters. |
| by | "character" | "word" | "line" | "word" | Split into characters, words, or the visual lines the text wraps onto. |
| trigger | "mount" | "inView" | "inView" | Start on mount or when the text scrolls into view. |
| delay | number | 0 | Seconds before the first piece. |
| duration | number | 0.45 / 0.6 / 0.75 | Seconds each piece takes (character / word / line). |
| stagger | number | 0.025 / 0.06 / 0.14 | Seconds between pieces (character / word / line). |
| once | boolean | true | In "inView" mode, play only the first time. When false it replays on every entry. |
| amount | number | 0.3 | How much of the element must be visible to start (0–1). |
| segmentClassName | string | — | Classes for every animated piece. |
Accessibility
- The server renders the finished text, so crawlers and no-JS visitors always see it; pieces are hidden just before the first client paint.
- In word and line mode the words are the real text. In character mode a visually hidden copy carries the string and the letters are aria-hidden, because some screen readers read split letters one at a time.
- Letters are grouped per word, so lines only break between words, and split by grapheme so emoji and accents stay intact.
- Pieces animate with Motion directly on the DOM, with no React state per piece.
- With prefers-reduced-motion the text is shown immediately.