AI Loader
Waiting states with character: typing dots, an orbit around a thinking face, and echo bars with a shimmering label.
"use client"
import * as React from "react"
import {
AiKitStyles,
AiStyle,
cx,
Face,
Roll,
toneColor,
type AiMood,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
const CSS = `
.aik-ld-typ{display:inline-flex;align-items:flex-end;gap:8px;max-width:100%}
.aik-ld-bub{display:inline-flex;align-items:center;gap:10px;min-height:38px;padding:0 14px;border-radius:18px 18px 18px 6px;background:var(--aik-card);box-shadow:var(--aik-sh-card);min-width:0}
.aik-ld-dots{display:inline-flex;gap:4px;flex:none}
.aik-ld-dots i{width:6px;height:6px;border-radius:50%;background:var(--aik-text-3);animation:aik-ld-dot 1.2s var(--aik-ease-soft) infinite}
.aik-ld-dots i:nth-child(2){animation-delay:.15s}
.aik-ld-dots i:nth-child(3){animation-delay:.3s}
.aik-ld-lab{font-size:13px;line-height:18px;color:var(--aik-text-2);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
@keyframes aik-ld-dot{0%,60%,100%{transform:none;opacity:.45}30%{transform:translateY(-4px);opacity:1}}
.aik-ld-orb{display:inline-grid;justify-items:center;gap:14px;max-width:100%}
.aik-ld-ring{position:relative;display:grid;place-items:center;width:calc(var(--aik-ld-s)*1.6);height:calc(var(--aik-ld-s)*1.6)}
.aik-ld-ring::before{content:"";position:absolute;inset:3px;border-radius:50%;border:1px solid var(--aik-line-2)}
.aik-ld-sat{position:absolute;inset:0;animation:aik-spin 2.6s linear infinite}
.aik-ld-sat::before{content:"";position:absolute;left:50%;top:0;width:7px;height:7px;margin-left:-3.5px;border-radius:50%;background:var(--aik-ld-c)}
.aik-ld-sat:nth-child(2){animation-delay:-.2s}.aik-ld-sat:nth-child(2)::before{width:5px;height:5px;margin:1px 0 0 -2.5px;opacity:.6}
.aik-ld-sat:nth-child(3){animation-delay:-.38s}.aik-ld-sat:nth-child(3)::before{width:4px;height:4px;margin:1.5px 0 0 -2px;opacity:.32}
.aik-ld-orb .aik-roll{font-size:13.5px;line-height:20px;color:var(--aik-text-2);justify-items:center;max-width:100%}
.aik-ld-echo{display:inline-flex;align-items:center;gap:12px;height:44px;padding:0 14px 0 12px;border-radius:14px;background:var(--aik-card);box-shadow:var(--aik-sh-card);max-width:100%}
.aik-ld-bars{display:inline-flex;align-items:center;gap:3px;height:20px;flex:none}
.aik-ld-bars i{width:3px;height:20px;border-radius:2px;background:var(--aik-blue);transform:scaleY(.25);animation:aik-ld-echo 1.1s var(--aik-ease-move) infinite alternate;animation-delay:calc(var(--i)*110ms)}
.aik-ld-echo .aik-shimmer{min-width:0;font-size:13.5px;line-height:20px;font-weight:500;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-ld-time{flex:none;margin-left:4px;font:11.5px/16px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums}
@keyframes aik-ld-echo{from{transform:scaleY(.25);opacity:.45}to{transform:scaleY(1);opacity:1}}
@media (prefers-reduced-motion: reduce){
.aik-ld-dots i,.aik-ld-sat,.aik-ld-bars i{animation:none}
.aik-ld-dots i:nth-child(2){transform:translateY(-3px);opacity:1}
.aik-ld-sat:nth-child(2){transform:rotate(-28deg)}.aik-ld-sat:nth-child(3){transform:rotate(-52deg)}
.aik-ld-bars i:nth-child(odd){transform:scaleY(.5)}.aik-ld-bars i:nth-child(even){transform:scaleY(.9)}
}
`
interface LoaderLook {
/** Face colour: a named tone or any CSS colour. */
tone?: AiTone | (string & {})
shape?: AiShape
}
export interface AiTypingDotsProps
extends LoaderLook, React.HTMLAttributes<HTMLDivElement> {
/** A short line after the dots, such as "Mira is typing". */
label?: string
/** Shows the agent's face beside the bubble. */
face?: boolean
}
/** Typing dots in a chat bubble, with an optional face and label. */
export function AiTypingDots({
label,
face = true,
tone = "blue",
shape = "orb",
className,
...rest
}: AiTypingDotsProps) {
return (
<div
{...rest}
role="status"
aria-label={label ?? "Typing"}
className={cx("aik aik-ld-typ", className)}
>
<AiKitStyles />
<AiStyle id="ai-loader" css={CSS} />
{face ? <Face size={28} tone={tone} shape={shape} mood="talk" /> : null}
<span className="aik-ld-bub">
<span className="aik-ld-dots" aria-hidden="true">
<i />
<i />
<i />
</span>
{label ? <span className="aik-ld-lab">{label}</span> : null}
</span>
</div>
)
}
export interface AiOrbitLoaderProps
extends LoaderLook, React.HTMLAttributes<HTMLDivElement> {
/** What the agent is thinking about. Rolls over in place when it changes. */
label?: string
/** The face's edge length in pixels. The orbit scales with it. */
size?: number
mood?: AiMood
}
/** A comet of three dots orbiting a thinking face. */
export function AiOrbitLoader({
label,
size = 56,
mood = "think",
tone = "violet",
shape = "blob",
className,
style,
...rest
}: AiOrbitLoaderProps) {
const color = toneColor(tone)
return (
<div
{...rest}
role="status"
aria-label={label ?? "Thinking"}
className={cx("aik aik-ld-orb", className)}
style={
{
"--aik-ld-s": `${size}px`,
"--aik-ld-c": color,
...style,
} as React.CSSProperties
}
>
<AiKitStyles />
<AiStyle id="ai-loader" css={CSS} />
<span className="aik-ld-ring">
<i className="aik-ld-sat" />
<i className="aik-ld-sat" />
<i className="aik-ld-sat" />
<Face size={size} tone={tone} shape={shape} mood={mood} />
</span>
{label ? <Roll>{label}</Roll> : null}
</div>
)
}
export interface AiEchoLoaderProps extends React.HTMLAttributes<HTMLDivElement> {
/** The shimmering line, such as "Searching the registry". */
label: string
/** Elapsed time, already formatted. */
elapsed?: string
/** How many bars to draw. */
bars?: number
}
/** Echo bars beside a shimmering label and an elapsed timer. */
export function AiEchoLoader({
label,
elapsed,
bars = 5,
className,
...rest
}: AiEchoLoaderProps) {
return (
<div
{...rest}
role="status"
aria-label={label}
className={cx("aik aik-ld-echo", className)}
>
<AiKitStyles />
<AiStyle id="ai-loader" css={CSS} />
<span className="aik-ld-bars" aria-hidden="true">
{Array.from({ length: Math.max(1, bars) }, (_, index) => (
<i key={index} style={{ "--i": index } as React.CSSProperties} />
))}
</span>
<span className="aik-shimmer">{label}</span>
{elapsed ? <span className="aik-ld-time">{elapsed}</span> : null}
</div>
)
}
Installation
One command adds ai-loader and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
$npx shadcn@latest add https://tweenui.dev/r/ai-loader.json
ai-loader-demo
"use client"
import * as React from "react"
import { AiDemo } from "@/lib/ai-demo"
import { useAiLoop } from "@/lib/ai-kit"
import {
AiEchoLoader,
AiOrbitLoader,
AiTypingDots,
} from "@/registry/tween/ai-loader"
const clock = (seconds: number) => `0:${String(seconds).padStart(2, "0")}`
function Dots() {
return (
<div style={{ display: "grid", gap: 16, justifyItems: "start" }}>
<AiTypingDots label="Mira is typing" />
<AiTypingDots tone="violet" shape="blob" />
<AiTypingDots face={false} label="Three agents are replying" />
</div>
)
}
const THOUGHTS = [
"Reading the hero section",
"Comparing three reveals",
"Checking reduced motion",
"Writing the plan",
]
function Orbit() {
const root = React.useRef<HTMLDivElement>(null)
const [at, setAt] = React.useState(0)
useAiLoop(
root,
async ({ sleep }) => {
for (let index = 0; index < THOUGHTS.length; index++) {
setAt(index)
await sleep(1700)
}
},
{ gap: 0, still: () => setAt(1) }
)
return (
<div ref={root} style={{ display: "grid", justifyItems: "center" }}>
<AiOrbitLoader label={THOUGHTS[at] ?? THOUGHTS[0]} size={60} />
</div>
)
}
function Echo() {
const root = React.useRef<HTMLDivElement>(null)
const [seconds, setSeconds] = React.useState(3)
useAiLoop(
root,
async ({ sleep }) => {
for (let tick = 0; tick < 12; tick++) {
setSeconds(tick)
await sleep(1000)
}
},
{ gap: 0 }
)
return (
<div ref={root} style={{ display: "grid", gap: 14, justifyItems: "start" }}>
<AiEchoLoader
label={seconds < 6 ? "Searching the registry" : "Reading 4 components"}
elapsed={clock(seconds)}
/>
<AiEchoLoader label="Waiting for the model" bars={3} />
</div>
)
}
export function AiLoaderDemo({ only }: { only?: string }) {
return (
<AiDemo
only={only}
tabs={[
{ name: "Dots", node: <Dots /> },
{ name: "Orbit", node: <Orbit /> },
{ name: "Echo", node: <Echo /> },
]}
/>
)
}
Installation
Usage
import {
AiEchoLoader,
AiOrbitLoader,
AiTypingDots,
} from "@/components/ui/ai-loader"<AiTypingDots label="Mira is typing" />
<AiOrbitLoader label="Comparing three reveals" tone="violet" />
<AiEchoLoader label="Searching the registry" elapsed="0:04" />Versions
AiTypingDotsis three dots in a chat bubble, with an optional face beside it and a short label after the dots.AiOrbitLoadersends a small comet of dots around a thinking face. Its label rolls over in place whenever it changes.AiEchoLoaderis a row of bars that rise and fall in turn beside a shimmering label and an elapsed timer.
Each one is a status region with an accessible label. With reduced motion the loops stop and each shows a still frame.
Props
AiTypingDots
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | A short line after the dots. | |
face | boolean | true | Shows the agent's face beside the bubble. |
tone | tone or CSS colour | "blue" | Face colour. |
shape | face shape | "orb" | Face shape. |
AiOrbitLoader
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | What the agent is thinking about. | |
size | number | 56 | The face's size in pixels. The orbit follows. |
mood | mood | "think" | What the face acts out. |
tone | tone or CSS colour | "violet" | Face and dot colour. |
shape | face shape | "blob" | Face shape. |
AiEchoLoader
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | The shimmering line. | |
elapsed | string | Elapsed time, already formatted. | |
bars | number | 5 | How many bars to draw. |