Split Text
A split text animation for React and Tailwind CSS: letters or words stagger in with a rise, drop, blur or fade when the text scrolls into view. The finished text is server-rendered for SEO, lines only break between words, and screen readers get the whole string.
- Letters or words
- Stagger on view
- Server-rendered text
- No re-renders
- prefers-reduced-motion
Midnight, rendered.
import { SplitText } from "@/components/dusk/text/split-text";
export default function SplitTextDemo() {
return (
<SplitText as="h2" className="text-center text-5xl font-bold tracking-tight sm:text-6xl">
Midnight, rendered.
</SplitText>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/split-text.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/text/split-text.tsx.split-text.tsx "use client"; import * as React from "react"; import { stagger, useAnimate, useInView, useReducedMotion, type DOMKeyframesDefinition } from "motion/react"; type SplitTextTag = "span" | "p" | "div" | "strong" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; export interface SplitTextProps extends Omit<React.HTMLAttributes<HTMLElement>, "children"> { /** The text to animate. Plain text only. */ children: string; /** Element to render. Use a heading level for titles. */ as?: SplitTextTag; /** Animate each letter or each word. */ by?: "letters" | "words"; /** How each piece enters. */ effect?: "rise" | "blur" | "fade" | "drop"; /** Seconds between pieces. Defaults to 0.03 for letters and 0.07 for words. */ stagger?: number; /** Seconds before the first piece starts. */ delay?: number; /** Seconds each piece takes. */ duration?: number; /** Play only the first time it scrolls into view. When `false` it replays on every entry. */ once?: boolean; /** How much of the element must be visible to start (0–1). */ amount?: number; } const HIDDEN: Record<NonNullable<SplitTextProps["effect"]>, DOMKeyframesDefinition> = { rise: { opacity: 0, transform: "translateY(0.35em)" }, drop: { opacity: 0, transform: "translateY(-0.4em) rotate(-3deg)" }, blur: { opacity: 0, filter: "blur(6px)", transform: "scale(1.05)" }, fade: { opacity: 0 }, }; const SHOWN: Record<NonNullable<SplitTextProps["effect"]>, DOMKeyframesDefinition> = { rise: { opacity: 1, transform: "translateY(0em)" }, drop: { opacity: 1, transform: "translateY(0em) rotate(0deg)" }, blur: { opacity: 1, filter: "blur(0px)", transform: "scale(1)" }, fade: { opacity: 1 }, }; const PIECE = "[data-split-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); } /** * Letters or words stagger in when the text scrolls into view. * * The server renders the finished text, so no-JS visitors and crawlers see it * immediately; the pieces are hidden just before the first client paint and * then animated with Motion (no React state per piece). Words stay real text; * in letter mode screen readers get the whole string from a visually hidden * copy, because letter-by-letter markup can be read one character at a time. * Reduced motion skips the animation entirely. */ export function SplitText({ children, as = "span", by = "letters", effect = "rise", stagger: gap, delay = 0, duration = 0.6, once = true, amount = 0.4, className, ...props }: SplitTextProps) { 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 = gap ?? (by === "letters" ? 0.03 : 0.07); // Hide the pieces before the first client paint (the server copy stays fully visible). React.useLayoutEffect(() => { if (reduceMotion || played.current) return; animate(PIECE, HIDDEN[effect], { duration: 0 }); }, [animate, effect, reduceMotion]); React.useEffect(() => { if (reduceMotion) { animate(PIECE, SHOWN[effect], { duration: 0 }); return; } if (!inView) { if (!once && played.current) animate(PIECE, HIDDEN[effect], { duration: 0 }); return; } played.current = true; const controls = animate(PIECE, SHOWN[effect], { duration, delay: stagger(step, { startDelay: delay }), ease: [0.22, 1, 0.36, 1], }); return () => controls.stop(); }, [animate, inView, once, reduceMotion, effect, duration, step, delay]); const words = children.split(/(\s+)/).filter(Boolean); // Words are real text, read normally. Letter-by-letter markup is read one character // at a time by some screen readers, so letters get a visually hidden copy instead. if (by === "words") { return ( <Comp ref={scope} data-slot="split-text" className={className} {...props}> {words.map((word, i) => /^\s+$/.test(word) ? ( word ) : ( <span key={i} data-split-piece className="inline-block whitespace-pre"> {word} </span> ) )} </Comp> ); } return ( <Comp ref={scope} data-slot="split-text" className={className} {...props}> <span className="sr-only">{children}</span> <span aria-hidden> {words.map((word, i) => /^\s+$/.test(word) ? ( word ) : ( // Keep each word's letters together so lines only break between words. <span key={i} className="inline-block whitespace-pre"> {graphemes(word).map((char, j) => ( <span key={j} data-split-piece className="inline-block"> {char} </span> ))} </span> ) )} </span> </Comp> ); }
Usage
import { SplitText } from "@/components/dusk/text/split-text"
export function Headline() {
return (
<SplitText as="h1" effect="rise" className="text-6xl font-bold">
Hello, night shift.
</SplitText>
)
}Examples
Effects
effect picks how each piece enters: rise, drop, blur or fade. Tune the rhythm with stagger, duration and delay.
Rise up
Drop in
Out of focus
Quiet fade
import { SplitText } from "@/components/dusk/text/split-text";
export default function SplitTextEffectsDemo() {
return (
<div className="grid w-full max-w-xl gap-6 text-3xl font-semibold tracking-tight sm:grid-cols-2">
<SplitText as="p" effect="rise">
Rise up
</SplitText>
<SplitText as="p" effect="drop">
Drop in
</SplitText>
<SplitText as="p" effect="blur" stagger={0.05}>
Out of focus
</SplitText>
<SplitText as="p" effect="fade" stagger={0.06} className="text-white/70">
Quiet fade
</SplitText>
</div>
);
}Words, replayed
by="words" animates whole words, which suits paragraphs. With once={false} it replays every time it scrolls back into view.
Words arrive one after another every time this paragraph scrolls back into view, and line breaks only ever happen between them.
import { SplitText } from "@/components/dusk/text/split-text";
export default function SplitTextWordsDemo() {
return (
<SplitText
as="p"
by="words"
effect="blur"
once={false}
duration={0.8}
className="max-w-lg text-center text-xl leading-relaxed text-white/75"
>
Words arrive one after another every time this paragraph scrolls back into view, and line breaks only ever
happen between them.
</SplitText>
);
}API Reference
Also accepts any HTML attribute for the rendered element.
| Prop | Type | Default | Description |
|---|---|---|---|
| children | string | — | The text to animate. Plain text only. |
| as | "span" | "p" | "div" | "strong" | "h1" … "h6" | "span" | Element to render. Use a heading level for titles. |
| by | "letters" | "words" | "letters" | Animate each letter or each word. |
| effect | "rise" | "drop" | "blur" | "fade" | "rise" | How each piece enters. |
| stagger | number | 0.03 (letters) / 0.07 (words) | Seconds between pieces. |
| delay | number | 0 | Seconds before the first piece. |
| duration | number | 0.6 | Seconds each piece takes. |
| once | boolean | true | Play only the first time. When false it replays on every entry. |
| amount | number | 0.4 | How much of the element must be visible to start (0–1). |
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 mode the words are the real text. In letter 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 ever break between words. Letters are split by grapheme, so emoji and accents stay intact.
- Pieces animate with Motion directly on the DOM, with no React state per letter.
- With prefers-reduced-motion the text is shown immediately.