Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/rotating-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 { AnimatePresence, motion, useInView, type HTMLMotionProps, type Transition } from "motion/react";
import { cn } from "@/lib/utils";
import {
srOnlyStyle,
usePageVisible,
useReduceMotion,
useTempoTransition,
} from "./motion";
export type RotatingTextProps = Omit<HTMLMotionProps<"span">, "children"> & {
text?: string | string[];
texts?: string | string[];
duration?: number;
y?: number;
delay?: number;
align?: "start" | "center" | "end";
loop?: boolean;
transition?: Transition;
};
export function RotatingText({
text,
texts: textsProp,
duration = 2000,
y = -50,
delay = 0,
align = "start",
loop = true,
transition,
className,
style,
...props
}: RotatingTextProps) {
const reduce = useReduceMotion();
const tween = useTempoTransition(
transition ?? { duration: 0.3, ease: [0.23, 1, 0.32, 1] },
);
const list = React.useMemo(() => {
const value = textsProp ?? text ?? "";
const next = Array.isArray(value) ? value : [value];
return next.length ? next : [""];
}, [text, textsProp]);
const unique = React.useMemo(() => [...new Set(list)], [list]);
const [index, setIndex] = React.useState(0);
const [rotated, setRotated] = React.useState(false);
const ref = React.useRef<HTMLSpanElement>(null);
const onScreen = useInView(ref);
const pageVisible = usePageVisible();
const playing = onScreen && pageVisible && (loop || index < list.length - 1);
React.useEffect(() => {
if (!playing) return;
const timer = setTimeout(
() => {
setRotated(true);
setIndex((current) =>
loop ? (current + 1) % list.length : Math.min(current + 1, list.length - 1),
);
},
rotated ? duration : delay,
);
return () => clearTimeout(timer);
}, [delay, duration, index, list.length, loop, playing, rotated]);
const current = list[index % list.length] ?? "";
const travel = reduce ? 0 : y;
return (
<motion.span
ref={ref}
data-slot="rotating-text"
className={cn("relative inline-block overflow-hidden py-1 align-baseline", className)}
style={style}
{...props}
>
<span className="sr-only" style={srOnlyStyle()}>
{list.join(", ")}
</span>
<span
aria-hidden="true"
className="inline-grid"
style={{ justifyItems: align, alignItems: "baseline" }}
>
{unique.map((item) => (
<span
key={item}
className="invisible whitespace-nowrap"
style={{ gridArea: "1 / 1" }}
>
{item}
</span>
))}
<span style={{ gridArea: "1 / 1", justifySelf: align }}>
<AnimatePresence mode="wait" initial={false}>
<motion.span
key={`${current}-${index}`}
aria-hidden="true"
initial={{ opacity: 0, y: -travel }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: travel }}
transition={tween}
className="inline-block whitespace-nowrap"
>
{current}
</motion.span>
</AnimatePresence>
</span>
</span>
</motion.span>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/rotating-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 { RotatingText } from "@/components/prequelui/rotating-text";
export default function Demo() {
return (
<div className="flex items-baseline gap-2 text-4xl font-medium">
<span>Make</span>
<RotatingText
texts={["design", "product", "motion"]}
duration={2000}
delay={1400}
align="start"
/>
<span>alive.</span>
</div>
);
}import { RotatingText } from "@/components/prequelui/rotating-text";
export function Variant() {
return (
<RotatingText tempo={1.2}>
…
</RotatingText>
);
}Animation
One word leaves upward while the next rises from below. AnimatePresence waits for the exit to finish before mounting the replacement, so the two words are never visible at the same time.
- Travel: 50 px in both directions, 300 ms ease-out.
- Default cycle: 2 s; the first rotation waits for `delay`.
- The box reserves the width of the longest word to avoid layout jumps.
- Reduced motion keeps only the crossfade.
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.
RotatingText
| Prop | Type | Default |
|---|---|---|
| texts | string[] | — |
| duration | number | 2000 |
| delay | number | 0 |
| y | number | -50 |
| loop | boolean | true |
| align | "start" | "center" | "end" | "start" |