Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/splitting-text.jsonFirst component? Set up your project first
Frameworks
The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.
"use client";
import * as React from "react";
import {
motion,
type HTMLMotionProps,
type TargetAndTransition,
type Transition,
type Variants,
} from "motion/react";
import { cn } from "@/lib/utils";
import {
splitGraphemes,
srOnlyStyle,
useReduceMotion,
useSeen,
useTempo,
useTempoTransition,
} from "./motion";
type SplittingBaseProps = Omit<
HTMLMotionProps<"span">,
"children" | "initial" | "animate" | "transition"
> & {
initial?: TargetAndTransition;
animate?: TargetAndTransition;
transition?: Transition;
stagger?: number;
delay?: number;
disableAnimation?: boolean;
inView?: boolean;
inViewOnce?: boolean;
};
export type SplittingTextProps = SplittingBaseProps &
(
| { type?: "chars" | "words"; text: string }
| { type?: "lines"; text: string[] }
);
const HIDDEN: TargetAndTransition = { x: 150, opacity: 0 };
const SHOWN: TargetAndTransition = { x: 0, opacity: 1 };
const STEP = { chars: 0.05, words: 0.2, lines: 0.3 } as const;
export function SplittingText({
text,
type = "chars",
initial = HIDDEN,
animate = SHOWN,
transition,
stagger,
delay = 0,
disableAnimation = false,
inView = false,
inViewOnce = true,
className,
...props
}: SplittingTextProps) {
const reduce = useReduceMotion();
const tempo = useTempo();
const tween = useTempoTransition(transition ?? { duration: 0.7, ease: [0.23, 1, 0.32, 1] });
const ref = React.useRef<HTMLSpanElement>(null);
const seen = useSeen(ref, inView, inViewOnce);
const still = disableAnimation || reduce;
const kind = Array.isArray(text) ? "lines" : type;
const gap = (stagger ?? STEP[kind] * tempo) as number;
const base = delay / 1000;
const variants: Variants = {
hidden: still ? animate : initial,
visible: (index: number) => ({
...animate,
transition: still ? { duration: 0 } : { ...tween, delay: base + index * gap },
}),
};
const piece = (content: React.ReactNode, index: number) => (
<motion.span
key={index}
custom={index}
variants={variants}
className="inline-block will-change-transform"
style={{ whiteSpace: kind === "chars" ? "pre" : undefined }}
>
{content}
</motion.span>
);
let visual: React.ReactNode;
if (Array.isArray(text)) {
visual = text.map((line, index) => (
<React.Fragment key={`line-${index}`}>
{piece(line, index)}
{index < text.length - 1 ? <br /> : null}
</React.Fragment>
));
} else if (kind === "words") {
const words = text.match(/\S+\s*/g) ?? [];
visual = words.map((word, index) => (
<React.Fragment key={`word-${index}`}>
{piece(word.trim(), index)}
{/\s$/.test(word) ? " " : null}
</React.Fragment>
));
} else {
let charIndex = 0;
visual = text.split(/(\s+)/).map((token, wordIndex) => {
if (!token) return null;
if (/^\s+$/.test(token)) return <span key={`space-${wordIndex}`}>{token}</span>;
return (
<span
key={`word-${wordIndex}`}
className="inline-block whitespace-nowrap"
>
{splitGraphemes(token).map((char) => piece(char, charIndex++))}
</span>
);
});
}
const label = Array.isArray(text) ? text.join(" ") : text;
return (
<motion.span
ref={ref}
data-slot="splitting-text"
initial="hidden"
animate={seen || still ? "visible" : "hidden"}
className={cn("inline", className)}
{...props}
>
<span aria-hidden="true">{visual}</span>
<span className="sr-only" style={srOnlyStyle()}>
{label}
</span>
</motion.span>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/splitting-text.jsonDependencies
motion lucide-reactThe registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.
Usage
Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.
import { SplittingText } from "@/components/prequelui/splitting-text";
export default function Demo() {
return (
<SplittingText
text="Every word arrives on time."
type="words"
stagger={0.2}
inView
className="text-3xl font-medium"
/>
);
}import { SplittingText } from "@/components/prequelui/splitting-text";
export function Variant() {
return (
<SplittingText tempo={1.2}>
…
</SplittingText>
);
}Animation
The text is cut into characters, words or lines and every piece enters on its own delay. The default entrance is a 150 px slide from the right with a fade, but initial and animate can be replaced entirely.
- Piece tween: 700 ms ease-out.
- Default stagger: 50 ms per character, 200 ms per word, 300 ms per line.
- The visual copy is hidden from assistive technology; a single label is announced.
- Reduced motion mounts the final state without travel.
API Reference
Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.
SplittingText
| Prop | Type | Default |
|---|---|---|
| text | string | string[] | — |
| type | "chars" | "words" | "lines" | "chars" |
| stagger | number | 0.05 / 0.2 / 0.3 |
| delay | number | 0 |
| inView | boolean | false |
| initial | TargetAndTransition | { x: 150, opacity: 0 } |