Agent Progress
An activity glyph, an action verb and a live timer for long agent tasks.
Preview
"use client";
import { AgentProgress } from "@/components/agents/loading-states/agent-progress";
export function AgentProgressPreview() {
return (
<AgentProgress
label="Churning"
initialSeconds={151.6}
className="text-base"
/>
);
}
"use client";
// beui.dev/components/agents/loading-states/agent-progress
import { motion, useReducedMotion } from "motion/react";
import { useEffect, useState } from "react";
import { EASE_IN_OUT } from "@/lib/ease";
import { useLocale, useMessages } from "@/lib/i18n";
import { cn } from "@/lib/utils";
import { type LoadingStatesMessages, loadingStatesMessages } from "./messages";
const GRID_CELLS = [
{ id: "top-left", delay: 0 },
{ id: "top-center", delay: 0.14 },
{ id: "top-right", delay: 0.28 },
{ id: "middle-left", delay: 0.42 },
{ id: "middle-center", delay: 0.56 },
{ id: "middle-right", delay: 0.7 },
{ id: "bottom-left", delay: 0.84 },
{ id: "bottom-center", delay: 0.98 },
{ id: "bottom-right", delay: 1.12 },
];
export interface AgentProgressProps {
/** Verb describing the agent's current activity. */
label?: string;
/** Controlled elapsed time in seconds. */
elapsedSeconds?: number;
/** Starting time for the internal timer, in seconds. */
initialSeconds?: number;
/** Whether the internal timer should advance. Ignored when elapsedSeconds is provided. */
running?: boolean;
/** Override any built-in string. `{label}` fills `inProgress`. */
labels?: Partial<LoadingStatesMessages>;
/** Classes merged onto the root element. */
className?: string;
}
function formatElapsed(totalSeconds: number, locale?: string) {
const safeSeconds = Math.max(0, totalSeconds);
const minutes = Math.floor(safeSeconds / 60);
const remainder = safeSeconds % 60;
// Without a locale keep the exact English text so server and client match.
if (!locale) {
const seconds = remainder.toFixed(1);
return minutes > 0 ? `${minutes}m ${seconds}s` : `${seconds}s`;
}
const seconds = new Intl.NumberFormat(locale, {
style: "unit",
unit: "second",
unitDisplay: "narrow",
minimumFractionDigits: 1,
maximumFractionDigits: 1,
}).format(remainder);
if (minutes === 0) return seconds;
const minuteText = new Intl.NumberFormat(locale, {
style: "unit",
unit: "minute",
unitDisplay: "narrow",
}).format(minutes);
return `${minuteText} ${seconds}`;
}
export function AgentProgress({
label: labelProp,
elapsedSeconds,
initialSeconds = 0,
running = true,
labels,
className,
}: AgentProgressProps) {
const t = useMessages("loadingStates", loadingStatesMessages, labels);
const { locale } = useLocale();
const label = labelProp ?? t("churning");
const reduce = useReducedMotion() ?? false;
const [internalSeconds, setInternalSeconds] = useState(initialSeconds);
useEffect(() => {
if (elapsedSeconds !== undefined || !running) return;
const startedAt = performance.now() - initialSeconds * 1000;
const timer = window.setInterval(() => {
setInternalSeconds((performance.now() - startedAt) / 1000);
}, 100);
return () => window.clearInterval(timer);
}, [elapsedSeconds, initialSeconds, running]);
const elapsed = elapsedSeconds ?? internalSeconds;
return (
<span
role="status"
aria-label={t("inProgress", { label })}
className={cn(
"inline-flex items-center gap-3 font-mono text-sm text-muted-foreground",
className,
)}
>
<span
aria-hidden="true"
className="grid size-5 shrink-0 grid-cols-3 gap-[2px]"
>
{GRID_CELLS.map(({ id, delay }) => (
<motion.span
key={id}
className="rounded-[1px] bg-current"
animate={
reduce
? { opacity: [0.35, 0.8, 0.35] }
: {
opacity: [0.28, 1, 0.28],
scale: [0.72, 1, 0.72],
}
}
transition={{
duration: 1.55,
ease: EASE_IN_OUT,
repeat: Infinity,
delay,
}}
/>
))}
</span>
<span className="font-sans font-medium">{label}</span>
<span
aria-hidden="true"
className="tabular-nums text-muted-foreground/70"
>
{formatElapsed(elapsed, locale)}
</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 { 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/agent-progress
import { motion, useReducedMotion } from "motion/react";
import { useEffect, useState } from "react";
import { EASE_IN_OUT } from "@/lib/ease";
import { useLocale, useMessages } from "@/lib/i18n";
import { cn } from "@/lib/utils";
import { type LoadingStatesMessages, loadingStatesMessages } from "./messages";
const GRID_CELLS = [
{ id: "top-left", delay: 0 },
{ id: "top-center", delay: 0.14 },
{ id: "top-right", delay: 0.28 },
{ id: "middle-left", delay: 0.42 },
{ id: "middle-center", delay: 0.56 },
{ id: "middle-right", delay: 0.7 },
{ id: "bottom-left", delay: 0.84 },
{ id: "bottom-center", delay: 0.98 },
{ id: "bottom-right", delay: 1.12 },
];
export interface AgentProgressProps {
/** Verb describing the agent's current activity. */
label?: string;
/** Controlled elapsed time in seconds. */
elapsedSeconds?: number;
/** Starting time for the internal timer, in seconds. */
initialSeconds?: number;
/** Whether the internal timer should advance. Ignored when elapsedSeconds is provided. */
running?: boolean;
/** Override any built-in string. `{label}` fills `inProgress`. */
labels?: Partial<LoadingStatesMessages>;
/** Classes merged onto the root element. */
className?: string;
}
function formatElapsed(totalSeconds: number, locale?: string) {
const safeSeconds = Math.max(0, totalSeconds);
const minutes = Math.floor(safeSeconds / 60);
const remainder = safeSeconds % 60;
// Without a locale keep the exact English text so server and client match.
if (!locale) {
const seconds = remainder.toFixed(1);
return minutes > 0 ? `${minutes}m ${seconds}s` : `${seconds}s`;
}
const seconds = new Intl.NumberFormat(locale, {
style: "unit",
unit: "second",
unitDisplay: "narrow",
minimumFractionDigits: 1,
maximumFractionDigits: 1,
}).format(remainder);
if (minutes === 0) return seconds;
const minuteText = new Intl.NumberFormat(locale, {
style: "unit",
unit: "minute",
unitDisplay: "narrow",
}).format(minutes);
return `${minuteText} ${seconds}`;
}
export function AgentProgress({
label: labelProp,
elapsedSeconds,
initialSeconds = 0,
running = true,
labels,
className,
}: AgentProgressProps) {
const t = useMessages("loadingStates", loadingStatesMessages, labels);
const { locale } = useLocale();
const label = labelProp ?? t("churning");
const reduce = useReducedMotion() ?? false;
const [internalSeconds, setInternalSeconds] = useState(initialSeconds);
useEffect(() => {
if (elapsedSeconds !== undefined || !running) return;
const startedAt = performance.now() - initialSeconds * 1000;
const timer = window.setInterval(() => {
setInternalSeconds((performance.now() - startedAt) / 1000);
}, 100);
return () => window.clearInterval(timer);
}, [elapsedSeconds, initialSeconds, running]);
const elapsed = elapsedSeconds ?? internalSeconds;
return (
<span
role="status"
aria-label={t("inProgress", { label })}
className={cn(
"inline-flex items-center gap-3 font-mono text-sm text-muted-foreground",
className,
)}
>
<span
aria-hidden="true"
className="grid size-5 shrink-0 grid-cols-3 gap-[2px]"
>
{GRID_CELLS.map(({ id, delay }) => (
<motion.span
key={id}
className="rounded-[1px] bg-current"
animate={
reduce
? { opacity: [0.35, 0.8, 0.35] }
: {
opacity: [0.28, 1, 0.28],
scale: [0.72, 1, 0.72],
}
}
transition={{
duration: 1.55,
ease: EASE_IN_OUT,
repeat: Infinity,
delay,
}}
/>
))}
</span>
<span className="font-sans font-medium">{label}</span>
<span
aria-hidden="true"
className="tabular-nums text-muted-foreground/70"
>
{formatElapsed(elapsed, locale)}
</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;
label?stringVerb describing the agent's current activity.
—elapsedSeconds?numberControlled elapsed time in seconds.
—initialSeconds?numberStarting time for the internal timer, in seconds.
0running?booleanWhether the internal timer should advance. Ignored when elapsedSeconds is provided.
truelabels?Partial<{ thinking: string; churning: string; inProgress: string; reasoning: string; phraseThinking: string; phraseReading: string; phraseConnecting: string; phraseForming: string; }>Override any built-in string. `{label}` fills `inProgress`.
—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