Reasoning Text
Shimmering reasoning lines that cascade, swap or scramble between steps.
Preview
"use client";
import {
ReasoningText,
type ReasoningTextVariant,
} from "@/components/agents/loading-states/reasoning-text";
const EXAMPLES: {
label: string;
variant: ReasoningTextVariant;
phrases: string[];
}[] = [
{
label: "Cascade",
variant: "cascade",
phrases: [
"Thinking",
"Reading the request",
"Working through the details",
"Preparing the answer",
],
},
{
label: "Swap",
variant: "swap",
phrases: [
"Thinking",
"Reading the request",
"Working through the details",
"Preparing the answer",
],
},
{
label: "Scramble",
variant: "scramble",
phrases: ["Thinking", "Searching", "Reasoning", "Composing"],
},
];
export function ReasoningTextPreview() {
return (
<div className="grid w-full max-w-sm gap-7">
{EXAMPLES.map(({ label, variant, phrases }) => (
<div key={variant} className="grid gap-2">
<span className="text-[11px] font-medium uppercase tracking-wider text-muted-foreground/60">
{label}
</span>
<ReasoningText
variant={variant}
phrases={phrases}
className="text-base"
/>
</div>
))}
</div>
);
}
"use client";
// beui.dev/components/agents/loading-states/reasoning-text
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { type ReactNode, useEffect, useId, useState } from "react";
import { Loader } from "@/components/motion/loader";
import { TextScramble } from "@/components/motion/text-scramble";
import { EASE_OUT, SPRING_SWAP } from "@/lib/ease";
import { useMessages } from "@/lib/i18n";
import {
TEXT_SHIMMER_CLASS_NAME,
TEXT_SHIMMER_HIGHLIGHT_CLASS_NAME,
TEXT_SHIMMER_KEYFRAMES,
textShimmerStyle,
} from "@/lib/text-shimmer";
import { cn } from "@/lib/utils";
import { type LoadingStatesMessages, loadingStatesMessages } from "./messages";
const CASCADE_STAGGER = 0.025;
export type ReasoningTextVariant = "cascade" | "swap" | "scramble";
export interface ReasoningTextProps {
/** Phrases cycled through while the agent works. */
phrases?: string[];
/** Animation used when the active phrase changes. */
variant?: ReasoningTextVariant;
/** Milliseconds each phrase remains visible. */
interval?: number;
/** Seconds taken for one shimmer pass. */
shimmerDuration?: number;
/** Optional leading visual. Defaults to a terminal-style ASCII loader. */
indicator?: ReactNode;
/** Override any built-in string, including the default phrases. */
labels?: Partial<LoadingStatesMessages>;
/** Classes merged onto the root element. */
className?: string;
}
type PhraseProps = {
phrase: string;
reduce: boolean;
shimmerDuration: number;
};
function CascadePhrase({
phrase,
reduce,
shimmerDuration,
}: PhraseProps) {
const text = `${phrase}…`;
if (reduce) {
return (
<span
className={cn(
"col-start-1 row-start-1 inline-block justify-self-start whitespace-pre",
TEXT_SHIMMER_CLASS_NAME,
)}
style={textShimmerStyle(shimmerDuration)}
>
{text}
</span>
);
}
// The letters land plain; one highlight layer over the whole phrase carries
// the shimmer, so it sweeps across instead of every letter pulsing at once.
// It fades in as the last letter settles.
const settle = text.length * CASCADE_STAGGER + 0.2;
return (
<AnimatePresence initial={false}>
<motion.span
key={phrase}
className="relative col-start-1 row-start-1 inline-block justify-self-start whitespace-pre [font-kerning:none] [font-variant-ligatures:none]"
initial="initial"
animate="animate"
exit="exit"
>
{text.split("").map((character, characterIndex) => (
<motion.span
// biome-ignore lint/suspicious/noArrayIndexKey: position is the stable cascade slot identity.
key={characterIndex}
custom={characterIndex * CASCADE_STAGGER}
// Own states: a phrase that cycles back re-enters mid-exit, and
// only owned targets are restored.
initial="initial"
animate="animate"
exit="exit"
variants={{
initial: { opacity: 0, y: "100%" },
animate: (delay: number) => ({
opacity: 1,
y: "0%",
transition: { ...SPRING_SWAP, delay },
}),
exit: (delay: number) => ({
opacity: 0,
y: "-100%",
transition: {
duration: 0.14,
ease: EASE_OUT,
delay: delay * 0.45,
},
}),
}}
className="inline-block whitespace-pre text-muted-foreground will-change-[opacity,transform]"
>
{character}
</motion.span>
))}
<motion.span
aria-hidden="true"
variants={{
initial: { opacity: 0 },
animate: { opacity: 1, transition: { duration: 0.3, ease: EASE_OUT, delay: settle } },
exit: { opacity: 0, transition: { duration: 0.1, ease: EASE_OUT } },
}}
className={cn(
"pointer-events-none absolute inset-0 whitespace-pre",
TEXT_SHIMMER_HIGHLIGHT_CLASS_NAME,
)}
style={textShimmerStyle(shimmerDuration)}
>
{text}
</motion.span>
</motion.span>
</AnimatePresence>
);
}
function SwapPhrase({ phrase, reduce, shimmerDuration }: PhraseProps) {
return (
<AnimatePresence initial={false}>
<motion.span
key={phrase}
className={cn(
"col-start-1 row-start-1 inline-block justify-self-start whitespace-nowrap will-change-[opacity,transform]",
TEXT_SHIMMER_CLASS_NAME,
)}
style={textShimmerStyle(shimmerDuration)}
initial={reduce ? { opacity: 0 } : { opacity: 0, y: 3 }}
animate={reduce ? { opacity: 1 } : { opacity: 1, y: 0 }}
exit={reduce ? { opacity: 0 } : { opacity: 0, y: -3 }}
transition={{
duration: reduce ? 0.12 : 0.2,
ease: EASE_OUT,
}}
>
{phrase}…
</motion.span>
</AnimatePresence>
);
}
function ScramblePhrase({
phrase,
shimmerDuration,
}: PhraseProps) {
const target = `${phrase}…`;
return (
<TextScramble
text={target}
className={cn(
"col-start-1 row-start-1 justify-self-start tabular-nums",
TEXT_SHIMMER_CLASS_NAME,
)}
style={textShimmerStyle(shimmerDuration)}
/>
);
}
export function ReasoningText({
phrases,
variant = "cascade",
interval = 1800,
shimmerDuration = 2.2,
indicator,
labels,
className,
}: ReasoningTextProps) {
const t = useMessages("loadingStates", loadingStatesMessages, labels);
const reduce = useReducedMotion() ?? false;
const [index, setIndex] = useState(0);
const statusId = useId();
const safePhrases =
phrases && phrases.length > 0
? phrases
: [
t("phraseThinking"),
t("phraseReading"),
t("phraseConnecting"),
t("phraseForming"),
];
const phrase = safePhrases[index % safePhrases.length];
const longestPhrase = safePhrases.reduce((longest, current) =>
current.length > longest.length ? current : longest,
);
const phraseProps = { phrase, reduce, shimmerDuration };
useEffect(() => {
if (safePhrases.length < 2) return;
const timer = window.setInterval(() => {
setIndex((current) => (current + 1) % safePhrases.length);
}, Math.max(600, interval));
return () => window.clearInterval(timer);
}, [interval, safePhrases.length]);
return (
<>
<style>{TEXT_SHIMMER_KEYFRAMES}</style>
<span
role="status"
aria-live="polite"
aria-labelledby={statusId}
className={cn(
"inline-flex items-center gap-2 text-sm font-medium text-muted-foreground",
className,
)}
>
<span aria-hidden="true" className="inline-flex size-3 shrink-0 items-center justify-center">
{indicator ?? (
<Loader
variant="ascii-line"
size={14}
speed={0.8}
label={t("reasoning")}
/>
)}
</span>
<span aria-hidden="true" className="grid overflow-hidden text-left">
<span className="invisible col-start-1 row-start-1 whitespace-nowrap">
{longestPhrase}…
</span>
{variant === "cascade" ? (
<CascadePhrase {...phraseProps} />
) : variant === "scramble" ? (
<ScramblePhrase {...phraseProps} />
) : (
<SwapPhrase {...phraseProps} />
)}
</span>
<span id={statusId} className="sr-only">
{phrase}
</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 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;
"use client";
import {
createContext,
type ReactNode,
useCallback,
useContext,
useMemo,
} from "react";
/**
* A component's messages. Plain strings with `{name}` placeholders, so a
* dictionary stays JSON-serializable and can be passed from a server layout.
* There are no plural forms: write messages that read correctly for any count.
*/
export type Messages = Record<string, string>;
/** A language file: component namespace → any subset of that component's messages. */
export type LocaleMessages = Record<string, Partial<Messages>>;
type LocaleContextValue = {
locale?: string;
messages: LocaleMessages;
};
const NO_MESSAGES: LocaleMessages = {};
const LocaleContext = createContext<LocaleContextValue>({ messages: NO_MESSAGES });
/** Without a locale, digits stay plain so server and client render the same text. */
export function formatNumber(value: number, locale?: string) {
return locale ? new Intl.NumberFormat(locale).format(value) : String(value);
}
export interface LocaleProviderProps {
/** BCP 47 tag, e.g. `de`, `pt-BR`. Drives `Intl` number formatting. */
locale: string;
/**
* Component namespace → translated strings. Missing keys fall back to English.
* Pass a stable object (a module constant, imported JSON or a memoized value):
* a new object each render gives every translated component a new `t`.
*/
messages?: LocaleMessages;
children: ReactNode;
}
export function LocaleProvider({ locale, messages = NO_MESSAGES, children }: LocaleProviderProps) {
const value = useMemo(() => ({ locale, messages }), [locale, messages]);
return <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>;
}
export function useLocale(): { locale?: string } {
return { locale: useContext(LocaleContext).locale };
}
/**
* Resolves a component's strings: `labels` prop, then the provider's
* messages for `namespace`, then the component's English `defaults`.
* Numbers passed as values are formatted for the provider's locale.
*/
export function useMessages<T extends Messages>(
namespace: string,
defaults: T,
labels?: Partial<T>,
) {
const { locale, messages } = useContext(LocaleContext);
const provided = messages[namespace];
// Inline `labels={{ … }}` is a new object every render. Keying it by content
// keeps `t` one identity until a label changes, so effects and memos that
// depend on `t` don't re-run per render, and render-time reads never lag.
const labelsKey = labels ? JSON.stringify(labels) : "";
const stableLabels = useMemo(
() => (labelsKey ? (JSON.parse(labelsKey) as Partial<T>) : undefined),
[labelsKey],
);
return useCallback(
(key: keyof T & string, values?: Record<string, string | number>) => {
const template = stableLabels?.[key] ?? provided?.[key] ?? defaults[key];
if (!values) return template;
return template.replace(/\{(\w+)\}/g, (match, name: string) => {
const value = values[name];
if (value === undefined) return match;
return typeof value === "number" ? formatNumber(value, locale) : value;
});
},
[defaults, stableLabels, locale, provided],
);
}
import type { CSSProperties } from "react";
// The reduced-motion rule travels with the component. The site's globals.css calms CSS
// animation globally, but the registry bundles imports only, so an installed
// copy never receives that reset.
//
// `!important` because the sweep is an inline style, which outranks a plain rule
// in a media query. It selects a marker class carried by TEXT_SHIMMER_CLASS_NAME
// so it also reaches consumers that build their own span out of these exports.
export const TEXT_SHIMMER_KEYFRAMES =
"@keyframes beui-text-shimmer{from{background-position:200% 0}to{background-position:-200% 0}}" +
"@media (prefers-reduced-motion: reduce){.beui-text-shimmer{animation:none !important}}";
export const TEXT_SHIMMER_CLASS_NAME =
"beui-text-shimmer bg-[length:200%_100%] bg-clip-text text-transparent bg-[linear-gradient(110deg,var(--muted-foreground)_30%,var(--foreground)_50%,var(--muted-foreground)_70%)]";
/**
* Only the moving highlight band, transparent elsewhere. Laid over text drawn
* in pieces (a letter cascade), so one band sweeps the whole phrase instead
* of every piece carrying, and flashing, its own gradient.
*/
export const TEXT_SHIMMER_HIGHLIGHT_CLASS_NAME =
"beui-text-shimmer bg-[length:200%_100%] bg-clip-text text-transparent bg-[linear-gradient(110deg,transparent_30%,var(--foreground)_50%,transparent_70%)]";
export function textShimmerStyle(duration: number): CSSProperties {
return {
animation: `beui-text-shimmer ${duration}s linear infinite`,
};
}
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/agents/loading-states/reasoning-text
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { type ReactNode, useEffect, useId, useState } from "react";
import { Loader } from "@/components/motion/loader";
import { TextScramble } from "@/components/motion/text-scramble";
import { EASE_OUT, SPRING_SWAP } from "@/lib/ease";
import { useMessages } from "@/lib/i18n";
import {
TEXT_SHIMMER_CLASS_NAME,
TEXT_SHIMMER_HIGHLIGHT_CLASS_NAME,
TEXT_SHIMMER_KEYFRAMES,
textShimmerStyle,
} from "@/lib/text-shimmer";
import { cn } from "@/lib/utils";
import { type LoadingStatesMessages, loadingStatesMessages } from "./messages";
const CASCADE_STAGGER = 0.025;
export type ReasoningTextVariant = "cascade" | "swap" | "scramble";
export interface ReasoningTextProps {
/** Phrases cycled through while the agent works. */
phrases?: string[];
/** Animation used when the active phrase changes. */
variant?: ReasoningTextVariant;
/** Milliseconds each phrase remains visible. */
interval?: number;
/** Seconds taken for one shimmer pass. */
shimmerDuration?: number;
/** Optional leading visual. Defaults to a terminal-style ASCII loader. */
indicator?: ReactNode;
/** Override any built-in string, including the default phrases. */
labels?: Partial<LoadingStatesMessages>;
/** Classes merged onto the root element. */
className?: string;
}
type PhraseProps = {
phrase: string;
reduce: boolean;
shimmerDuration: number;
};
function CascadePhrase({
phrase,
reduce,
shimmerDuration,
}: PhraseProps) {
const text = `${phrase}…`;
if (reduce) {
return (
<span
className={cn(
"col-start-1 row-start-1 inline-block justify-self-start whitespace-pre",
TEXT_SHIMMER_CLASS_NAME,
)}
style={textShimmerStyle(shimmerDuration)}
>
{text}
</span>
);
}
// The letters land plain; one highlight layer over the whole phrase carries
// the shimmer, so it sweeps across instead of every letter pulsing at once.
// It fades in as the last letter settles.
const settle = text.length * CASCADE_STAGGER + 0.2;
return (
<AnimatePresence initial={false}>
<motion.span
key={phrase}
className="relative col-start-1 row-start-1 inline-block justify-self-start whitespace-pre [font-kerning:none] [font-variant-ligatures:none]"
initial="initial"
animate="animate"
exit="exit"
>
{text.split("").map((character, characterIndex) => (
<motion.span
// biome-ignore lint/suspicious/noArrayIndexKey: position is the stable cascade slot identity.
key={characterIndex}
custom={characterIndex * CASCADE_STAGGER}
// Own states: a phrase that cycles back re-enters mid-exit, and
// only owned targets are restored.
initial="initial"
animate="animate"
exit="exit"
variants={{
initial: { opacity: 0, y: "100%" },
animate: (delay: number) => ({
opacity: 1,
y: "0%",
transition: { ...SPRING_SWAP, delay },
}),
exit: (delay: number) => ({
opacity: 0,
y: "-100%",
transition: {
duration: 0.14,
ease: EASE_OUT,
delay: delay * 0.45,
},
}),
}}
className="inline-block whitespace-pre text-muted-foreground will-change-[opacity,transform]"
>
{character}
</motion.span>
))}
<motion.span
aria-hidden="true"
variants={{
initial: { opacity: 0 },
animate: { opacity: 1, transition: { duration: 0.3, ease: EASE_OUT, delay: settle } },
exit: { opacity: 0, transition: { duration: 0.1, ease: EASE_OUT } },
}}
className={cn(
"pointer-events-none absolute inset-0 whitespace-pre",
TEXT_SHIMMER_HIGHLIGHT_CLASS_NAME,
)}
style={textShimmerStyle(shimmerDuration)}
>
{text}
</motion.span>
</motion.span>
</AnimatePresence>
);
}
function SwapPhrase({ phrase, reduce, shimmerDuration }: PhraseProps) {
return (
<AnimatePresence initial={false}>
<motion.span
key={phrase}
className={cn(
"col-start-1 row-start-1 inline-block justify-self-start whitespace-nowrap will-change-[opacity,transform]",
TEXT_SHIMMER_CLASS_NAME,
)}
style={textShimmerStyle(shimmerDuration)}
initial={reduce ? { opacity: 0 } : { opacity: 0, y: 3 }}
animate={reduce ? { opacity: 1 } : { opacity: 1, y: 0 }}
exit={reduce ? { opacity: 0 } : { opacity: 0, y: -3 }}
transition={{
duration: reduce ? 0.12 : 0.2,
ease: EASE_OUT,
}}
>
{phrase}…
</motion.span>
</AnimatePresence>
);
}
function ScramblePhrase({
phrase,
shimmerDuration,
}: PhraseProps) {
const target = `${phrase}…`;
return (
<TextScramble
text={target}
className={cn(
"col-start-1 row-start-1 justify-self-start tabular-nums",
TEXT_SHIMMER_CLASS_NAME,
)}
style={textShimmerStyle(shimmerDuration)}
/>
);
}
export function ReasoningText({
phrases,
variant = "cascade",
interval = 1800,
shimmerDuration = 2.2,
indicator,
labels,
className,
}: ReasoningTextProps) {
const t = useMessages("loadingStates", loadingStatesMessages, labels);
const reduce = useReducedMotion() ?? false;
const [index, setIndex] = useState(0);
const statusId = useId();
const safePhrases =
phrases && phrases.length > 0
? phrases
: [
t("phraseThinking"),
t("phraseReading"),
t("phraseConnecting"),
t("phraseForming"),
];
const phrase = safePhrases[index % safePhrases.length];
const longestPhrase = safePhrases.reduce((longest, current) =>
current.length > longest.length ? current : longest,
);
const phraseProps = { phrase, reduce, shimmerDuration };
useEffect(() => {
if (safePhrases.length < 2) return;
const timer = window.setInterval(() => {
setIndex((current) => (current + 1) % safePhrases.length);
}, Math.max(600, interval));
return () => window.clearInterval(timer);
}, [interval, safePhrases.length]);
return (
<>
<style>{TEXT_SHIMMER_KEYFRAMES}</style>
<span
role="status"
aria-live="polite"
aria-labelledby={statusId}
className={cn(
"inline-flex items-center gap-2 text-sm font-medium text-muted-foreground",
className,
)}
>
<span aria-hidden="true" className="inline-flex size-3 shrink-0 items-center justify-center">
{indicator ?? (
<Loader
variant="ascii-line"
size={14}
speed={0.8}
label={t("reasoning")}
/>
)}
</span>
<span aria-hidden="true" className="grid overflow-hidden text-left">
<span className="invisible col-start-1 row-start-1 whitespace-nowrap">
{longestPhrase}…
</span>
{variant === "cascade" ? (
<CascadePhrase {...phraseProps} />
) : variant === "scramble" ? (
<ScramblePhrase {...phraseProps} />
) : (
<SwapPhrase {...phraseProps} />
)}
</span>
<span id={statusId} className="sr-only">
{phrase}
</span>
</span>
</>
);
}
/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */
export const loadingStatesMessages = {
thinking: "Thinking…",
churning: "Churning",
inProgress: "{label}, in progress",
reasoning: "Reasoning",
phraseThinking: "Thinking",
phraseReading: "Reading the context",
phraseConnecting: "Connecting the details",
phraseForming: "Forming a response",
};
export type LoadingStatesMessages = typeof loadingStatesMessages;
"use client";
import { motion, useReducedMotion } from "motion/react";
import { useEffect, useId, useState } from "react";
import { EASE_IN_OUT } from "@/lib/ease";
import { useMessages } from "@/lib/i18n";
import { cn } from "@/lib/utils";
export type LoaderVariant =
| "spinner"
| "dots"
| "bars"
| "dot-matrix"
| "dither"
| "ascii"
| "ascii-line"
| "ascii-braille"
| "ascii-blocks"
| "ascii-bounce"
| "morph"
| "comet"
| "scramble"
| "metaballs"
| "newton"
| "helix"
| "percent";
// Terminal-style frame sets — the loaders CLI AI agents cycle through.
const ASCII_SETS: Record<string, string[]> = {
ascii: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
"ascii-line": ["|", "/", "-", "\\"],
"ascii-braille": ["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"],
"ascii-blocks": ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", "▇", "▆", "▅", "▄", "▃", "▂"],
"ascii-bounce": ["⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"],
};
/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */
export const loaderMessages = {
label: "Loading",
/** The word the `scramble` variant resolves to. */
scramble: "LOADING",
};
export type LoaderMessages = typeof loaderMessages;
export interface LoaderProps {
/** Which animation to render. */
variant?: LoaderVariant;
/** Base square size in px. Everything scales from this. */
size?: number;
/** Seconds per animation cycle. */
speed?: number;
/** Accessible label announced to screen readers. */
label?: string;
/** Override any built-in string. */
labels?: Partial<LoaderMessages>;
/** Classes merged onto the root element. */
className?: string;
}
// Reduced motion keeps a calm opacity pulse and drops every transform.
const REDUCED = {
animate: { opacity: [1, 0.4, 1] },
transition: { duration: 1.4, ease: EASE_IN_OUT, repeat: Infinity },
};
export function Loader({
variant = "spinner",
size = 32,
speed = 1,
label: labelProp,
labels,
className,
}: LoaderProps) {
const t = useMessages("loader", loaderMessages, labels);
const label = labelProp ?? t("label");
const reduce = useReducedMotion() ?? false;
return (
<span
role="status"
aria-label={label}
className={cn(
"inline-flex items-center justify-center text-foreground",
className,
)}
>
{variant === "spinner" && <Spinner size={size} speed={speed} reduce={reduce} />}
{variant === "dots" && <Dots size={size} speed={speed} reduce={reduce} />}
{variant === "bars" && <Bars size={size} speed={speed} reduce={reduce} />}
{variant === "dot-matrix" && (
<DotMatrix size={size} speed={speed} reduce={reduce} />
)}
{variant === "dither" && <Dither size={size} speed={speed} reduce={reduce} />}
{ASCII_SETS[variant] && (
<Ascii frames={ASCII_SETS[variant]} size={size} speed={speed} reduce={reduce} />
)}
{variant === "morph" && (
<Morph size={size} speed={speed} reduce={reduce} label={label} />
)}
{variant === "comet" && <Comet size={size} speed={speed} reduce={reduce} />}
{variant === "scramble" && (
<Scramble
size={size}
speed={speed}
reduce={reduce}
target={t("scramble")}
/>
)}
{variant === "metaballs" && (
<Metaballs size={size} speed={speed} reduce={reduce} label={label} />
)}
{variant === "newton" && <Newton size={size} speed={speed} reduce={reduce} />}
{variant === "helix" && <Helix size={size} speed={speed} reduce={reduce} />}
{variant === "percent" && (
<Percent size={size} speed={speed} reduce={reduce} />
)}
<span className="sr-only">{label}</span>
</span>
);
}
interface PartProps {
size: number;
speed: number;
reduce: boolean;
}
function Spinner({ size, speed, reduce }: PartProps) {
const stroke = Math.max(2, size * 0.09);
const r = (size - stroke) / 2;
return (
<motion.svg
width={size}
height={size}
viewBox={`0 0 ${size} ${size}`}
animate={reduce ? REDUCED.animate : { rotate: 360 }}
transition={
reduce
? REDUCED.transition
: { duration: speed, ease: "linear", repeat: Infinity }
}
>
<circle
cx={size / 2}
cy={size / 2}
r={r}
fill="none"
stroke="currentColor"
strokeOpacity={0.2}
strokeWidth={stroke}
/>
<path
d={`M ${size / 2} ${size / 2 - r} A ${r} ${r} 0 0 1 ${size / 2 + r} ${size / 2}`}
fill="none"
stroke="currentColor"
strokeWidth={stroke}
strokeLinecap="round"
/>
</motion.svg>
);
}
function Dots({ size, speed, reduce }: PartProps) {
const dot = size * 0.24;
return (
<span className="flex items-center" style={{ gap: size * 0.14 }}>
{[0, 1, 2].map((i) => (
<motion.span
key={i}
className="rounded-full bg-current"
style={{ width: dot, height: dot }}
animate={
reduce
? { opacity: [0.4, 1, 0.4] }
: { y: [0, -size * 0.3, 0], opacity: [0.5, 1, 0.5] }
}
transition={{
duration: speed,
ease: EASE_IN_OUT,
repeat: Infinity,
delay: i * speed * 0.16,
}}
/>
))}
</span>
);
}
function Ascii({
frames,
size,
speed,
reduce,
}: PartProps & { frames: string[] }) {
const [frame, setFrame] = useState(0);
useEffect(() => {
// Reduced motion slows the cycle rather than stopping it — it's a glyph
// swap, not on-screen movement.
const step = ((reduce ? speed * 2.5 : speed) / frames.length) * 1000;
const id = setInterval(
() => setFrame((f) => (f + 1) % frames.length),
step,
);
return () => clearInterval(id);
}, [frames.length, speed, reduce]);
return (
<span
className="font-mono leading-none tabular-nums"
style={{ fontSize: size, lineHeight: 1 }}
>
{frames[frame % frames.length]}
</span>
);
}
// Each shape is sampled at the same number of points and emitted as an SVG
// path with identical command structure, so framer tweens the `d` attribute
// point-to-point — a real morph, not a snap. (clip-path polygon strings don't
// interpolate reliably in framer, which left the shapes broken.)
const MORPH_POINTS = 24;
function ngonRadius(ang: number, n: number, phase = 0) {
const seg = (2 * Math.PI) / n;
const a = ang - phase;
const local = (((a % seg) + seg) % seg) - seg / 2;
return Math.cos(Math.PI / n) / Math.cos(local);
}
function morphPath(radiusAt: (ang: number) => number) {
const parts: string[] = [];
for (let i = 0; i < MORPH_POINTS; i++) {
const ang = (i / MORPH_POINTS) * 2 * Math.PI - Math.PI / 2;
const r = Math.min(1.05, radiusAt(ang));
const x = (50 + Math.cos(ang) * 46 * r).toFixed(2);
const y = (50 + Math.sin(ang) * 46 * r).toFixed(2);
parts.push(`${i === 0 ? "M" : "L"}${x} ${y}`);
}
return `${parts.join(" ")} Z`;
}
const MORPH_PATHS = [
morphPath(() => 1), // circle
morphPath((a) => ngonRadius(a, 4, Math.PI / 4)), // square
morphPath((a) => ngonRadius(a, 3)), // triangle
morphPath((a) => ngonRadius(a, 6)), // hexagon
morphPath((a) => ngonRadius(a, 4)), // diamond
];
// Each shape appears twice in a row so it fully forms and HOLDS before the
// next morph. Even keyframe spacing then alternates hold / morph segments.
const MORPH_SEQ = [...MORPH_PATHS.flatMap((p) => [p, p]), MORPH_PATHS[0]];
// Rotation and scale only change across the morph segments, staying put on the
// holds, so a settled shape sits still.
const MORPH_ROT = [0, 0, 72, 72, 144, 144, 216, 216, 288, 288, 360];
const MORPH_SCALE = [1, 1, 0.88, 0.88, 1, 1, 0.88, 0.88, 1, 1, 1];
function Morph({ size, speed, reduce, label }: PartProps & { label: string }) {
return (
<motion.svg
width={size}
height={size}
viewBox="0 0 100 100"
role="img"
animate={
reduce
? { opacity: [1, 0.4, 1] }
: { rotate: MORPH_ROT, scale: MORPH_SCALE }
}
transition={
reduce
? { duration: 1.4, ease: EASE_IN_OUT, repeat: Infinity }
: { duration: speed * 5, ease: EASE_IN_OUT, repeat: Infinity }
}
>
<title>{label}</title>
<motion.path
fill="currentColor"
d={MORPH_PATHS[0]}
// Seeds the motion value; without it motion writes d="undefined"
// before the keyframes start.
initial={{ d: MORPH_PATHS[0] }}
animate={reduce ? undefined : { d: MORPH_SEQ }}
transition={
reduce
? undefined
: { duration: speed * 5, ease: EASE_IN_OUT, repeat: Infinity }
}
/>
</motion.svg>
);
}
const COMET_TRAIL = [0, 1, 2, 3, 4, 5];
function Comet({ size, speed, reduce }: PartProps) {
const head = size * 0.2;
const r = size / 2 - head / 2;
return (
<span className="relative" style={{ width: size, height: size }}>
<motion.span
className="absolute inset-0"
animate={reduce ? REDUCED.animate : { rotate: 360 }}
transition={
reduce
? REDUCED.transition
: { duration: speed, ease: "linear", repeat: Infinity }
}
>
{COMET_TRAIL.map((i) => {
const scale = 1 - i * 0.13;
const sz = head * scale;
return (
<span
key={i}
className="absolute top-1/2 left-1/2 rounded-full bg-current"
style={{
width: sz,
height: sz,
marginLeft: -sz / 2,
marginTop: -sz / 2,
opacity: 1 - i * 0.16,
transform: `rotate(${-i * 15}deg) translateY(${-r}px)`,
}}
/>
);
})}
</motion.span>
</span>
);
}
const SCRAMBLE_GLYPHS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<>/*#@";
function Scramble({ size, speed, reduce, target }: PartProps & { target: string }) {
const [text, setText] = useState(target);
useEffect(() => {
if (reduce) {
setText(target);
return;
}
let tick = 0;
const total = target.length + 4;
const id = setInterval(
() => {
const reveal = tick % total;
let s = "";
for (let i = 0; i < target.length; i++) {
s +=
i < reveal
? target[i]
: SCRAMBLE_GLYPHS[
Math.floor(Math.random() * SCRAMBLE_GLYPHS.length)
];
}
setText(s);
tick++;
},
(speed / target.length) * 1000 * 0.55,
);
return () => clearInterval(id);
}, [speed, reduce, target]);
return (
<span
className="font-mono font-medium tracking-[0.2em] tabular-nums"
style={{ fontSize: size * 0.42 }}
>
{text}
</span>
);
}
function Metaballs({ size, speed, reduce, label }: PartProps & { label: string }) {
const id = useId().replace(/:/g, "");
return (
<svg width={size} height={size} viewBox="0 0 100 100" role="img">
<title>{label}</title>
<defs>
<filter id={id}>
<feGaussianBlur in="SourceGraphic" stdDeviation="5" result="b" />
<feColorMatrix
in="b"
values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 20 -8"
/>
</filter>
</defs>
<g filter={`url(#${id})`} fill="currentColor">
<motion.circle
cy="50"
r="15"
// Seeds the motion value; without it motion writes cx="undefined"
// before the keyframes start.
initial={{ cx: reduce ? 40 : 30 }}
animate={reduce ? { opacity: [0.4, 1, 0.4] } : { cx: [30, 70, 30] }}
transition={{ duration: speed * 1.6, ease: EASE_IN_OUT, repeat: Infinity }}
/>
<motion.circle
cy="50"
r="15"
initial={{ cx: reduce ? 60 : 70 }}
animate={reduce ? { opacity: [0.4, 1, 0.4] } : { cx: [70, 30, 70] }}
transition={{ duration: speed * 1.6, ease: EASE_IN_OUT, repeat: Infinity }}
/>
</g>
</svg>
);
}
const NEWTON_BALLS = [0, 1, 2, 3, 4];
function Newton({ size, speed, reduce }: PartProps) {
const d = size * 0.2;
const out = d * 1.1;
// Only the end balls move: the left slides out and back on the first half,
// then the right on the second half — the impact appears to jump the three
// still middle balls. Pure horizontal slide, no swing, no strings.
const moves: Record<number, { x: number[]; times: number[] }> = {
0: { x: [0, -out, 0, 0], times: [0, 0.28, 0.5, 1] },
4: { x: [0, 0, out, 0], times: [0, 0.5, 0.78, 1] },
};
return (
<span className="flex items-center justify-center" style={{ height: d }}>
{NEWTON_BALLS.map((i) => {
const move = moves[i];
return (
<motion.span
key={i}
className="rounded-full bg-current"
style={{ width: d, height: d }}
animate={reduce || !move ? undefined : { x: move.x }}
transition={
reduce || !move
? undefined
: {
duration: speed * 1.5,
ease: EASE_IN_OUT,
repeat: Infinity,
times: move.times,
}
}
/>
);
})}
</span>
);
}
function Helix({ size, speed, reduce }: PartProps) {
const rows = 7;
const dot = size * 0.14;
const amp = size * 0.32;
return (
<span className="relative" style={{ width: size, height: size }}>
{Array.from({ length: rows }, (_, r) => {
const top = (r / (rows - 1)) * (size - dot);
const delay = (r / rows) * speed;
return (
<span key={`row-${top}`}>
<motion.span
className="absolute rounded-full bg-current"
style={{ width: dot, height: dot, left: size / 2 - dot / 2, top }}
animate={
reduce
? { opacity: [0.4, 1, 0.4] }
: {
x: [amp, -amp, amp],
scale: [1, 0.5, 1],
opacity: [1, 0.45, 1],
}
}
transition={{
duration: speed,
ease: EASE_IN_OUT,
repeat: Infinity,
delay,
}}
/>
<motion.span
className="absolute rounded-full bg-current"
style={{ width: dot, height: dot, left: size / 2 - dot / 2, top }}
animate={
reduce
? { opacity: [0.4, 1, 0.4] }
: {
x: [-amp, amp, -amp],
scale: [0.5, 1, 0.5],
opacity: [0.45, 1, 0.45],
}
}
transition={{
duration: speed,
ease: EASE_IN_OUT,
repeat: Infinity,
delay,
}}
/>
</span>
);
})}
</span>
);
}
function Percent({ size, speed, reduce }: PartProps) {
const [p, setP] = useState(0);
useEffect(() => {
const dur = (reduce ? speed * 2 : speed) * 1000;
const start = { t: 0 };
const tickMs = 40;
const id = setInterval(() => {
start.t += tickMs;
const next = Math.min(100, Math.round((start.t / dur) * 100));
setP(next);
if (next >= 100) start.t = 0;
}, tickMs);
return () => clearInterval(id);
}, [speed, reduce]);
return (
<span
className="flex flex-col items-center"
style={{ gap: size * 0.14, width: size * 1.4 }}
>
<span
className="font-mono font-medium tabular-nums"
style={{ fontSize: size * 0.42, lineHeight: 1 }}
>
{p}%
</span>
<span
className="w-full overflow-hidden rounded-full bg-current/15"
style={{ height: Math.max(3, size * 0.1) }}
>
<span
className="block h-full rounded-full bg-current"
style={{ width: `${p}%` }}
/>
</span>
</span>
);
}
function Bars({ size, speed, reduce }: PartProps) {
const bar = size * 0.16;
return (
<span className="flex items-center" style={{ gap: size * 0.1, height: size }}>
{[0, 1, 2, 3].map((i) => (
<motion.span
key={i}
className="rounded-full bg-current"
style={{ width: bar, height: size, originY: 1 }}
animate={
reduce ? { opacity: [0.4, 1, 0.4] } : { scaleY: [0.3, 1, 0.3] }
}
transition={{
duration: speed,
ease: EASE_IN_OUT,
repeat: Infinity,
delay: i * speed * 0.12,
}}
/>
))}
</span>
);
}
function DotMatrix({ size, speed, reduce }: PartProps) {
const n = 3;
const gap = size * 0.14;
const dot = (size - gap * (n - 1)) / n;
const cells = Array.from({ length: n * n }, (_, idx) => idx);
return (
<span
className="grid"
style={{
gap,
gridTemplateColumns: `repeat(${n}, ${dot}px)`,
}}
>
{cells.map((idx) => {
const x = idx % n;
const y = Math.floor(idx / n);
// Diagonal wave: cells light in order of their distance from the corner.
const delay = ((x + y) / (2 * (n - 1))) * speed;
return (
<motion.span
key={idx}
className="rounded-full bg-current"
style={{ width: dot, height: dot }}
animate={
reduce
? { opacity: [0.3, 1, 0.3] }
: { opacity: [0.2, 1, 0.2], scale: [0.7, 1, 0.7] }
}
transition={{
duration: speed,
ease: EASE_IN_OUT,
repeat: Infinity,
delay,
}}
/>
);
})}
</span>
);
}
// Ordered Bayer 4x4 matrix — the classic dithering threshold pattern. Cells
// light in this order, so the fill shimmers like a dissolving halftone.
const BAYER_4 = [
0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5,
];
function Dither({ size, speed, reduce }: PartProps) {
const n = 4;
const gap = Math.max(1, size * 0.05);
const cell = (size - gap * (n - 1)) / n;
return (
<span
className="grid"
style={{ gap, gridTemplateColumns: `repeat(${n}, ${cell}px)` }}
>
{BAYER_4.map((order, idx) => (
<motion.span
// biome-ignore lint/suspicious/noArrayIndexKey: fixed matrix cells, order never changes
key={idx}
className="bg-current"
style={{ width: cell, height: cell }}
animate={reduce ? { opacity: [0.3, 1, 0.3] } : { opacity: [0.1, 1, 0.1] }}
transition={{
duration: speed,
ease: EASE_IN_OUT,
repeat: Infinity,
delay: (order / BAYER_4.length) * speed,
}}
/>
))}
</span>
);
}
"use client";
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>
);
}
phrases?string[]Phrases cycled through while the agent works.
—variant?"cascade" | "swap" | "scramble"Animation used when the active phrase changes.
cascadeinterval?numberMilliseconds each phrase remains visible.
1800shimmerDuration?numberSeconds taken for one shimmer pass.
2.2indicator?ReactNodeOptional leading visual. Defaults to a terminal-style ASCII loader.
—labels?Partial<{ thinking: string; churning: string; inProgress: string; reasoning: string; phraseThinking: string; phraseReading: string; phraseConnecting: string; phraseForming: string; }>Override any built-in string, including the default phrases.
—className?stringClasses merged onto the root element.
—Todo List
A collapsible agent task plan with morphing status marks, a completion count, compact metadata, and smooth list updates.
Message
Composable conversation primitives for message rows, grouped bubbles, avatars, metadata, live markers, and a mount-only trailing-edge pop-up for newly sent rows.
Prompt Input
An auto-growing agent composer with prompt actions, model selection, keyboard submission, and animated send and stop states.
Updated