Dia Text Animation
A fixed sentence whose last word cycles in behind a colorful sweep.
Preview
import { ChromaticTextReveal } from "@/components/motion/chromatic-text-reveal";
export function ChromaticTextRevealPreview() {
return (
// The sentence never wraps, so it has to be sized against the space it
// actually gets — the surrounding column, not the viewport.
<div className="@container flex w-full justify-center">
<ChromaticTextReveal
prefix="Motion that feels"
words={["natural.", "intentional.", "alive."]}
startOnView={false}
className="shrink-0 font-medium tracking-[-0.04em] text-foreground [font-size:clamp(1.25rem,7.8cqw,3rem)]"
/>
</div>
);
}
"use client";
// beui.dev/components/motion/text-animation/chromatic-reveal
import {
type MotionStyle,
motion,
type UseInViewOptions,
useInView,
useReducedMotion,
} from "motion/react";
import { useCallback, useEffect, useRef, useState } from "react";
import { EASE_IN_OUT, EASE_OUT } from "@/lib/ease";
import { cn } from "@/lib/utils";
const CHROMATIC_PALETTE = [
"#60a5fa",
"#22d3ee",
"#f472b6",
"#fb7185",
"#fbbf24",
];
const TRAIL_HALF_WIDTH = 14;
const REVEAL_START = `-${TRAIL_HALF_WIDTH}%`;
const REVEAL_FINISH = `${100 + TRAIL_HALF_WIDTH}%`;
export type ChromaticTextRevealProps = {
/** Sentence fragment that remains fixed while the final word changes. */
prefix: string;
/** Words revealed one after another after the fixed prefix. */
words: string[];
/** Colors used along the moving chromatic edge. */
colors?: string[];
/** Final text color after the sweep passes. */
foregroundColor?: string;
/** Sweep duration in seconds. */
duration?: number;
/** Delay before the first sweep, in seconds. */
delay?: number;
/** Rest after a word finishes revealing, in seconds. */
pauseDuration?: number;
/** Returns to the first word after the final word. */
loop?: boolean;
/** Starts when the text enters the viewport. */
startOnView?: boolean;
/** Only starts on the first viewport entry. */
once?: boolean;
/** IntersectionObserver root margin used by the viewport trigger. */
inViewMargin?: UseInViewOptions["margin"];
className?: string;
};
function composeChromaticGradient(colors: string[], foregroundColor: string) {
const palette = colors.length > 0 ? colors : CHROMATIC_PALETTE;
const colorStops = palette.map((color, index) => {
const offset =
palette.length === 1
? 0
: -TRAIL_HALF_WIDTH +
(index / (palette.length - 1)) * TRAIL_HALF_WIDTH * 2;
const operator = offset < 0 ? "-" : "+";
const distance = Number(Math.abs(offset).toFixed(2));
return `${color} calc(var(--chromatic-sweep) ${operator} ${distance}%)`;
});
return `linear-gradient(90deg, ${foregroundColor} 0%, ${foregroundColor} calc(var(--chromatic-sweep) - ${TRAIL_HALF_WIDTH}%), ${colorStops.join(", ")}, transparent calc(var(--chromatic-sweep) + ${TRAIL_HALF_WIDTH}%), transparent 100%)`;
}
export function ChromaticTextReveal({
prefix,
words,
colors = CHROMATIC_PALETTE,
foregroundColor = "var(--foreground)",
duration = 1.2,
delay = 0,
pauseDuration = 0.8,
loop = true,
startOnView = true,
once = true,
inViewMargin,
className,
}: ChromaticTextRevealProps) {
const ref = useRef<HTMLSpanElement>(null);
const timerRef = useRef<number | null>(null);
const [wordIndex, setWordIndex] = useState(0);
const reduceMotion = useReducedMotion();
const isInView = useInView(ref, {
once,
margin: inViewMargin,
amount: 0.4,
});
const shouldReveal = !startOnView || isInView || reduceMotion;
const backgroundImage = composeChromaticGradient(colors, foregroundColor);
const hasWords = words.length > 0;
const activeIndex = hasWords ? wordIndex % words.length : 0;
const activeWord = words[activeIndex] ?? "";
const sizingWords = Array.from(new Set(words));
const clearPendingWord = useCallback(() => {
if (timerRef.current !== null) {
window.clearTimeout(timerRef.current);
timerRef.current = null;
}
}, []);
const scheduleNextWord = useCallback(() => {
clearPendingWord();
const isLastWord = activeIndex === words.length - 1;
if (
reduceMotion ||
!shouldReveal ||
words.length < 2 ||
(isLastWord && !loop)
) {
return;
}
timerRef.current = window.setTimeout(() => {
setWordIndex((index) => (index + 1) % words.length);
}, pauseDuration * 1000);
}, [
activeIndex,
clearPendingWord,
loop,
pauseDuration,
reduceMotion,
shouldReveal,
words.length,
]);
useEffect(() => clearPendingWord, [clearPendingWord]);
return (
<span ref={ref} className={cn("inline-flex items-baseline", className)}>
<span className="whitespace-nowrap">
{prefix}
{hasWords ? "\u00A0" : null}
</span>
{hasWords ? (
<span className="relative inline-grid">
{sizingWords.map((word) => (
<span
key={word}
aria-hidden
className="invisible col-start-1 row-start-1 whitespace-nowrap"
>
{word}
</span>
))}
{/* Moving a clipped text gradient defines this effect. Paint
containment bounds that deliberate repaint to the active word. */}
<motion.span
key={`${activeWord}-${activeIndex}`}
aria-hidden
initial={
reduceMotion
? false
: {
opacity: 0.56,
filter: "blur(6px)",
transform: "translateY(5px)",
}
}
animate={{
"--chromatic-sweep": shouldReveal
? REVEAL_FINISH
: REVEAL_START,
opacity: 1,
filter: "blur(0px)",
transform: "translateY(0px)",
}}
transition={{
"--chromatic-sweep": reduceMotion
? { duration: 0 }
: { duration, delay, ease: EASE_IN_OUT },
opacity: reduceMotion
? { duration: 0 }
: { duration: 0.28, ease: EASE_OUT },
filter: reduceMotion
? { duration: 0 }
: { duration: 0.36, ease: EASE_OUT },
transform: reduceMotion
? { duration: 0 }
: { duration: 0.36, ease: EASE_OUT },
}}
onAnimationComplete={scheduleNextWord}
className="absolute start-0 top-0 whitespace-nowrap bg-clip-text text-transparent [background-image:var(--chromatic-gradient)] [contain:paint]"
style={{
"--chromatic-sweep": reduceMotion
? REVEAL_FINISH
: REVEAL_START,
"--chromatic-gradient": backgroundImage,
backgroundSize: "100% 100%",
backgroundRepeat: "no-repeat",
} as MotionStyle}
>
{activeWord}
</motion.span>
<span className="sr-only">{activeWord}</span>
</span>
) : null}
</span>
);
}
Add it with the shadcn CLI, or copy the source manually.
npx shadcn add https://beui.dev/r/theme.json. Theme setupInstall dependencies
npm i clsx motion tailwind-mergeAdd util files
// Shared motion tokens. Easing curves mirror the CSS custom properties in
// globals.css; springs are the canonical physics used across components.
// Strong custom variants — defaults like `ease-in`/`ease-out` feel weak.
export const EASE_OUT = [0.16, 1, 0.3, 1] as const;
/** Accelerating curve for things that fall (a dropped ball, a toppled block). Not for UI entrances. */
export const EASE_IN = [0.55, 0, 1, 0.45] as const;
export const EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;
export const EASE_DRAWER = [0.32, 0.72, 0, 1] as const;
/** CSS string form of EASE_OUT for inline style transitions. */
export const EASE_OUT_CSS = "cubic-bezier(0.16, 1, 0.3, 1)";
/** Press feedback on buttons and other tappable surfaces. */
export const SPRING_PRESS = {
type: "spring",
stiffness: 500,
damping: 30,
mass: 0.6,
} as const;
/** Content swaps — label/icon slots trading places inside a control. */
export const SPRING_SWAP = {
type: "spring",
stiffness: 460,
damping: 30,
mass: 0.55,
} as const;
/** Overlay panel entrances — modals and sheets summoned by pointer. */
export const SPRING_PANEL = {
type: "spring",
stiffness: 420,
damping: 40,
mass: 0.5,
} as const;
/** Shared-layout glides — pills, indicators and panels morphing between positions. */
export const SPRING_LAYOUT = {
type: "spring",
stiffness: 360,
damping: 32,
mass: 0.6,
} as const;
/** Cursor-follow physics for decorative mouse tracking (magnetic, tilt, dock). */
export const SPRING_MOUSE = {
stiffness: 200,
damping: 15,
mass: 0.3,
} as const;
/** Dragged handles and fills (sliders) — critically damped `useSpring` config,
* so the value follows the pointer butterily and never rebounds off an end. */
export const SPRING_GLIDE = {
stiffness: 700,
damping: 50,
mass: 0.5,
} as const;
/** Shared-surface morphs: a trigger growing into its panel and back on one
* weighted spring, so opening and closing travel the same path. */
export const SPRING_MORPH = {
type: "spring",
duration: 0.5,
bounce: 0.22,
} as const;
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Copy the source code
"use client";
// beui.dev/components/motion/text-animation/chromatic-reveal
import {
type MotionStyle,
motion,
type UseInViewOptions,
useInView,
useReducedMotion,
} from "motion/react";
import { useCallback, useEffect, useRef, useState } from "react";
import { EASE_IN_OUT, EASE_OUT } from "@/lib/ease";
import { cn } from "@/lib/utils";
const CHROMATIC_PALETTE = [
"#60a5fa",
"#22d3ee",
"#f472b6",
"#fb7185",
"#fbbf24",
];
const TRAIL_HALF_WIDTH = 14;
const REVEAL_START = `-${TRAIL_HALF_WIDTH}%`;
const REVEAL_FINISH = `${100 + TRAIL_HALF_WIDTH}%`;
export type ChromaticTextRevealProps = {
/** Sentence fragment that remains fixed while the final word changes. */
prefix: string;
/** Words revealed one after another after the fixed prefix. */
words: string[];
/** Colors used along the moving chromatic edge. */
colors?: string[];
/** Final text color after the sweep passes. */
foregroundColor?: string;
/** Sweep duration in seconds. */
duration?: number;
/** Delay before the first sweep, in seconds. */
delay?: number;
/** Rest after a word finishes revealing, in seconds. */
pauseDuration?: number;
/** Returns to the first word after the final word. */
loop?: boolean;
/** Starts when the text enters the viewport. */
startOnView?: boolean;
/** Only starts on the first viewport entry. */
once?: boolean;
/** IntersectionObserver root margin used by the viewport trigger. */
inViewMargin?: UseInViewOptions["margin"];
className?: string;
};
function composeChromaticGradient(colors: string[], foregroundColor: string) {
const palette = colors.length > 0 ? colors : CHROMATIC_PALETTE;
const colorStops = palette.map((color, index) => {
const offset =
palette.length === 1
? 0
: -TRAIL_HALF_WIDTH +
(index / (palette.length - 1)) * TRAIL_HALF_WIDTH * 2;
const operator = offset < 0 ? "-" : "+";
const distance = Number(Math.abs(offset).toFixed(2));
return `${color} calc(var(--chromatic-sweep) ${operator} ${distance}%)`;
});
return `linear-gradient(90deg, ${foregroundColor} 0%, ${foregroundColor} calc(var(--chromatic-sweep) - ${TRAIL_HALF_WIDTH}%), ${colorStops.join(", ")}, transparent calc(var(--chromatic-sweep) + ${TRAIL_HALF_WIDTH}%), transparent 100%)`;
}
export function ChromaticTextReveal({
prefix,
words,
colors = CHROMATIC_PALETTE,
foregroundColor = "var(--foreground)",
duration = 1.2,
delay = 0,
pauseDuration = 0.8,
loop = true,
startOnView = true,
once = true,
inViewMargin,
className,
}: ChromaticTextRevealProps) {
const ref = useRef<HTMLSpanElement>(null);
const timerRef = useRef<number | null>(null);
const [wordIndex, setWordIndex] = useState(0);
const reduceMotion = useReducedMotion();
const isInView = useInView(ref, {
once,
margin: inViewMargin,
amount: 0.4,
});
const shouldReveal = !startOnView || isInView || reduceMotion;
const backgroundImage = composeChromaticGradient(colors, foregroundColor);
const hasWords = words.length > 0;
const activeIndex = hasWords ? wordIndex % words.length : 0;
const activeWord = words[activeIndex] ?? "";
const sizingWords = Array.from(new Set(words));
const clearPendingWord = useCallback(() => {
if (timerRef.current !== null) {
window.clearTimeout(timerRef.current);
timerRef.current = null;
}
}, []);
const scheduleNextWord = useCallback(() => {
clearPendingWord();
const isLastWord = activeIndex === words.length - 1;
if (
reduceMotion ||
!shouldReveal ||
words.length < 2 ||
(isLastWord && !loop)
) {
return;
}
timerRef.current = window.setTimeout(() => {
setWordIndex((index) => (index + 1) % words.length);
}, pauseDuration * 1000);
}, [
activeIndex,
clearPendingWord,
loop,
pauseDuration,
reduceMotion,
shouldReveal,
words.length,
]);
useEffect(() => clearPendingWord, [clearPendingWord]);
return (
<span ref={ref} className={cn("inline-flex items-baseline", className)}>
<span className="whitespace-nowrap">
{prefix}
{hasWords ? "\u00A0" : null}
</span>
{hasWords ? (
<span className="relative inline-grid">
{sizingWords.map((word) => (
<span
key={word}
aria-hidden
className="invisible col-start-1 row-start-1 whitespace-nowrap"
>
{word}
</span>
))}
{/* Moving a clipped text gradient defines this effect. Paint
containment bounds that deliberate repaint to the active word. */}
<motion.span
key={`${activeWord}-${activeIndex}`}
aria-hidden
initial={
reduceMotion
? false
: {
opacity: 0.56,
filter: "blur(6px)",
transform: "translateY(5px)",
}
}
animate={{
"--chromatic-sweep": shouldReveal
? REVEAL_FINISH
: REVEAL_START,
opacity: 1,
filter: "blur(0px)",
transform: "translateY(0px)",
}}
transition={{
"--chromatic-sweep": reduceMotion
? { duration: 0 }
: { duration, delay, ease: EASE_IN_OUT },
opacity: reduceMotion
? { duration: 0 }
: { duration: 0.28, ease: EASE_OUT },
filter: reduceMotion
? { duration: 0 }
: { duration: 0.36, ease: EASE_OUT },
transform: reduceMotion
? { duration: 0 }
: { duration: 0.36, ease: EASE_OUT },
}}
onAnimationComplete={scheduleNextWord}
className="absolute start-0 top-0 whitespace-nowrap bg-clip-text text-transparent [background-image:var(--chromatic-gradient)] [contain:paint]"
style={{
"--chromatic-sweep": reduceMotion
? REVEAL_FINISH
: REVEAL_START,
"--chromatic-gradient": backgroundImage,
backgroundSize: "100% 100%",
backgroundRepeat: "no-repeat",
} as MotionStyle}
>
{activeWord}
</motion.span>
<span className="sr-only">{activeWord}</span>
</span>
) : null}
</span>
);
}
prefixstringSentence fragment that remains fixed while the final word changes.
—wordsstring[]Words revealed one after another after the fixed prefix.
—colors?string[]Colors used along the moving chromatic edge.
[
"#60a5fa",
"#22d3ee",
"#f472b6",
"#fb7185",
"#fbbf24",
]foregroundColor?stringFinal text color after the sweep passes.
var(--foreground)duration?numberSweep duration in seconds.
1.2delay?numberDelay before the first sweep, in seconds.
0pauseDuration?numberRest after a word finishes revealing, in seconds.
0.8loop?booleanReturns to the first word after the final word.
truestartOnView?booleanStarts when the text enters the viewport.
trueonce?booleanOnly starts on the first viewport entry.
trueinViewMargin?MarginTypeIntersectionObserver root margin used by the viewport trigger.
—className?stringClasses merged onto the root element.
—Text Scramble
Changed characters cycle through random glyphs before they settle.
Text Reveal
Words or letters slide up into place with a spring and soft blur.
Text Shimmer
A gradient that sweeps across text for loading labels or emphasis.
Text Cascade
Old letters drop away as new ones roll in, one at a time from the left.
Number Animation
Animated number primitives for count-up values, rolling tickers, and fixed-slot digit swaps.
Action Swap
CTA button and slot primitives for swapping text and icons with blur motion.
Loader
Loading indicator with seventeen variants: spinner, dots, bars, dot-matrix, dither, morph, comet, scramble, metaballs, newton, helix, percent, and five terminal-style ascii spinners. Scales from one size prop, uses currentColor, and reduced-motion swaps every transform for a calm opacity pulse.
Updated