Thinking
The agent's reasoning: a line that ripples while it thinks, “Thought for 12s” when it's done, and the full reasoning one click away.
npx shadcn@latest add https://captain-ui.vercel.app/r/thinking.jsoncheckout.spec.ts is flaky again. Can you find out why?
…
"use client";
// Captain UI · Thinking, MIT. The agent's reasoning: a shimmering line while it thinks, "Thought for 12s" when done, open it to read.
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useEffect, useId, useRef, useState } from "react";
import { EASE, SPRING, cx } from "@/registry/captain/ui/captain";
import { Icon } from "@/registry/captain/ui/icons";
type ThinkingProps = {
/** True while the model is still reasoning. */
active: boolean;
/** The reasoning text so far. It can grow while active. */
children?: string;
/** When reasoning started, for the timer. Defaults to when `active` first became true. */
startedAt?: number;
/** Seconds it took, when you already know. */
seconds?: number;
/** Open while active, so people can watch it think. */
openWhileActive?: boolean;
defaultOpen?: boolean;
className?: string;
};
function useElapsed(active: boolean, startedAt?: number) {
const start = useRef<number | null>(startedAt ?? null);
const [s, setS] = useState(0);
useEffect(() => {
if (!active) return;
if (start.current == null) start.current = startedAt ?? Date.now();
const tick = () => setS(Math.max(0, Math.floor((Date.now() - (start.current ?? Date.now())) / 1000)));
tick();
const t = setInterval(tick, 250);
return () => clearInterval(t);
}, [active, startedAt]);
return s;
}
export function Thinking({ active, children = "", startedAt, seconds, openWhileActive = false, defaultOpen = false, className }: ThinkingProps) {
const elapsed = useElapsed(active, startedAt);
const [open, setOpen] = useState(defaultOpen || (active && openWhileActive));
const reduce = useReducedMotion();
const id = useId();
const took = seconds ?? elapsed;
const body = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!active && openWhileActive) setOpen(false);
}, [active, openWhileActive]);
// Keep the newest reasoning in view while it streams.
useEffect(() => {
if (active && open && body.current) body.current.scrollTop = body.current.scrollHeight;
}, [children, active, open]);
const label = active ? "Thinking" : took < 1 ? "Thought for a moment" : `Thought for ${took}s`;
return (
<div className={cx("w-full max-w-[560px]", className)}>
<button
type="button"
aria-expanded={open}
aria-controls={id}
onClick={() => setOpen((o) => !o)}
className="group -ml-2 flex h-9 items-center gap-1.5 rounded-[8px] px-2 text-[14px] transition-colors hover:bg-hover focus-visible:bg-hover"
>
<span className={cx("relative", active ? "text-ink" : "text-ink-2 group-hover:text-ink")} aria-label={label}>
{active && !reduce
? Array.from(label).map((ch, i) => (
<span key={i} aria-hidden="true" className="cu-wave" style={{ animationDelay: `${i * 70}ms` }}>
{ch}
</span>
))
: label}
</span>
{active && elapsed > 0 ? <span className="tnum text-[13px] text-ink-3">{elapsed}s</span> : null}
<motion.span animate={{ rotate: open ? 90 : 0 }} transition={reduce ? { duration: 0 } : SPRING} className="grid place-items-center text-ink-3">
<Icon name="chevronRight" size={15} />
</motion.span>
</button>
<AnimatePresence initial={false}>
{open ? (
<motion.div
id={id}
key="body"
initial={{ height: 0, opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={reduce ? { duration: 0 } : { height: SPRING, opacity: { duration: 0.18, ease: EASE } }}
className="overflow-hidden"
>
<div ref={body} className="mt-1 mb-2 max-h-[220px] overflow-y-auto pl-2">
<div className="whitespace-pre-wrap text-[13.5px] leading-[1.6] text-ink-3" aria-live={active ? "polite" : undefined}>
{children || (active ? "…" : "")}
</div>
</div>
</motion.div>
) : null}
</AnimatePresence>
<style>{`
.cu-wave{display:inline-block;white-space:pre;animation:cu-wave 1.4s ease-in-out infinite}
@keyframes cu-wave{0%,100%{opacity:.38}35%{opacity:1}}
`}</style>
</div>
);
}Usage
import { Thinking } from "@/components/ui/thinking"
<Thinking active={part.state === "streaming"}>{part.text}</Thinking>Props
| Prop | Type | Default | What it does |
|---|---|---|---|
| active | boolean | True while the model is still reasoning. | |
| children | string | The reasoning so far. It can grow while active. | |
| startedAt | number | Epoch ms when reasoning started, for the timer. | |
| seconds | number | How long it took, if you already know. | |
| openWhileActive | boolean | false | Show the reasoning as it streams, then fold it away. |
| defaultOpen | boolean | false | Start open. |
Keyboard
- Enter / Space
- Open or close the reasoning
Accessibility
- A disclosure button with aria-expanded.
- The label is read once, not letter by letter.
- Streaming text is a polite live region while open.
- The ripple stops under reduced motion.