Text Scramble
Changed characters cycle through random glyphs before they settle.
Preview
"use client";
import { useState } from "react";
import { TextScramble } from "@/components/motion/text-scramble";
const PHRASES = [
"Inspecting the repository",
"Running the checks",
"Preparing the update",
];
export function TextScramblePreview() {
const [index, setIndex] = useState(0);
return (
<div className="flex w-full flex-col items-center gap-8 text-center">
<TextScramble
text={PHRASES[index]}
className="font-mono text-xl font-medium text-foreground"
/>
<button
type="button"
onClick={() => setIndex((current) => (current + 1) % PHRASES.length)}
className="inline-flex h-9 items-center rounded-full border border-border bg-card px-4 text-xs font-medium text-foreground press hover:border-(--color-border-strong)"
>
Next phrase
</button>
</div>
);
}
"use client";
// beui.dev/components/motion/text-animation/text-scramble
import { useReducedMotion } from "motion/react";
import {
useEffect,
useRef,
useState,
type CSSProperties,
} from "react";
import { cn } from "@/lib/utils";
const DEFAULT_GLYPHS = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789#%&@$?/";
export interface TextScrambleProps {
/** Final text revealed by the scramble animation. */
text: string;
/** Maximum animation duration in milliseconds. */
duration?: number;
/** Characters sampled while unresolved positions are scrambling. */
glyphs?: string;
/** Classes merged onto the root element. */
className?: string;
/** Inline styles for the root element. */
style?: CSSProperties;
}
/** Character scramble that resolves to `text` and respects reduced motion. */
export function TextScramble({
text,
duration,
glyphs = DEFAULT_GLYPHS,
className,
style,
}: TextScrambleProps) {
const reduce = useReducedMotion() ?? false;
const [display, setDisplay] = useState(text);
const mounted = useRef(false);
useEffect(() => {
if (!mounted.current) {
mounted.current = true;
setDisplay(text);
return;
}
if (reduce || !glyphs) {
setDisplay(text);
return;
}
const characters = text.split("");
const startedAt = performance.now();
const animationDuration = duration
?? Math.min(760, Math.max(420, characters.length * 32));
let frame = 0;
let lastUpdate = 0;
const animate = (now: number) => {
if (now - lastUpdate >= 40) {
lastUpdate = now;
const progress = Math.min((now - startedAt) / animationDuration, 1);
const settled = Math.floor(progress * characters.length);
setDisplay(characters.map((character, index) => {
if (index < settled || character === " ") return character;
return glyphs[Math.floor(Math.random() * glyphs.length)];
}).join(""));
}
if (now - startedAt < animationDuration) {
frame = requestAnimationFrame(animate);
} else {
setDisplay(text);
}
};
frame = requestAnimationFrame(animate);
return () => cancelAnimationFrame(frame);
}, [duration, glyphs, reduce, text]);
return (
<span className={cn("inline-block whitespace-pre", className)} style={style}>
<span className="sr-only">{text}</span>
<span aria-hidden="true">{reduce ? text : display}</span>
</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 file
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/text-scramble
import { useReducedMotion } from "motion/react";
import {
useEffect,
useRef,
useState,
type CSSProperties,
} from "react";
import { cn } from "@/lib/utils";
const DEFAULT_GLYPHS = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789#%&@$?/";
export interface TextScrambleProps {
/** Final text revealed by the scramble animation. */
text: string;
/** Maximum animation duration in milliseconds. */
duration?: number;
/** Characters sampled while unresolved positions are scrambling. */
glyphs?: string;
/** Classes merged onto the root element. */
className?: string;
/** Inline styles for the root element. */
style?: CSSProperties;
}
/** Character scramble that resolves to `text` and respects reduced motion. */
export function TextScramble({
text,
duration,
glyphs = DEFAULT_GLYPHS,
className,
style,
}: TextScrambleProps) {
const reduce = useReducedMotion() ?? false;
const [display, setDisplay] = useState(text);
const mounted = useRef(false);
useEffect(() => {
if (!mounted.current) {
mounted.current = true;
setDisplay(text);
return;
}
if (reduce || !glyphs) {
setDisplay(text);
return;
}
const characters = text.split("");
const startedAt = performance.now();
const animationDuration = duration
?? Math.min(760, Math.max(420, characters.length * 32));
let frame = 0;
let lastUpdate = 0;
const animate = (now: number) => {
if (now - lastUpdate >= 40) {
lastUpdate = now;
const progress = Math.min((now - startedAt) / animationDuration, 1);
const settled = Math.floor(progress * characters.length);
setDisplay(characters.map((character, index) => {
if (index < settled || character === " ") return character;
return glyphs[Math.floor(Math.random() * glyphs.length)];
}).join(""));
}
if (now - startedAt < animationDuration) {
frame = requestAnimationFrame(animate);
} else {
setDisplay(text);
}
};
frame = requestAnimationFrame(animate);
return () => cancelAnimationFrame(frame);
}, [duration, glyphs, reduce, text]);
return (
<span className={cn("inline-block whitespace-pre", className)} style={style}>
<span className="sr-only">{text}</span>
<span aria-hidden="true">{reduce ? text : display}</span>
</span>
);
}
textstringFinal text revealed by the scramble animation.
—duration?numberMaximum animation duration in milliseconds.
—glyphs?stringCharacters sampled while unresolved positions are scrambling.
ABCDEFGHJKLMNPQRSTUVWXYZ0123456789#%&@$?/className?stringClasses merged onto the root element.
—style?CSSPropertiesInline styles for the root element.
—Dia Text Animation
A fixed sentence whose last word cycles in behind a colorful sweep.
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