AI Reasoning

Show the thinking: a shimmering disclosure with a timer, a thought trail on a rail, and a one-line ticker beside a thinking face.

Installation

Installation

One command adds ai-reasoning 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-reasoning.json
ai-reasoning-demo
components/ui/ai-reasoning-demo.tsx
"use client"

import * as React from "react"

import { AiDemo } from "@/lib/ai-demo"
import { useAiLoop } from "@/lib/ai-kit"
import {
  AiReasoning,
  AiThoughtTicker,
  AiThoughtTrail,
  type AiThought,
} from "@/registry/tween/ai-reasoning"

const clock = (seconds: number) => `0:${String(seconds).padStart(2, "0")}`

const LINES = [
  "The hero has one headline and a paragraph under it.",
  "Revealing both would compete, so animate the headline only.",
  "text-skew-reveal is already installed. No new dependency.",
]

function Disclosure() {
  const root = React.useRef<HTMLDivElement>(null)
  const [count, setCount] = React.useState(LINES.length)
  const [status, setStatus] = React.useState<"thinking" | "done">("thinking")
  const [seconds, setSeconds] = React.useState(3)
  const [answer, setAnswer] = React.useState(false)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setAnswer(false)
      setStatus("thinking")
      setCount(0)
      setSeconds(0)
      for (let at = 1; at <= LINES.length; at++) {
        await sleep(650)
        setCount(at)
        await sleep(650)
        setSeconds(at)
      }
      await sleep(900)
      setSeconds(4)
      setStatus("done")
      await sleep(450)
      setAnswer(true)
      await sleep(2800)
    },
    { gap: 500, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 380px)" }}
      onPointerDown={() => {
        setHands(true)
        setCount(LINES.length)
        setStatus("done")
        setAnswer(true)
      }}
    >
      <AiReasoning
        thoughts={LINES.slice(0, count)}
        status={status}
        elapsed={clock(seconds)}
        summary="Thought for 4s"
      >
        {answer
          ? "I will reveal the headline only and leave the paragraph still."
          : null}
      </AiReasoning>
    </div>
  )
}

const TRAIL: AiThought[] = [
  { text: "Read the hero and its props", time: "0.6s" },
  { text: "Compared three reveal components", time: "1.1s" },
  { text: "Checked reduced motion handling", time: "0.5s" },
  { text: "Choosing the stagger value", time: "0.9s" },
]

function Trail() {
  const root = React.useRef<HTMLDivElement>(null)
  const [count, setCount] = React.useState(TRAIL.length)
  const [current, setCurrent] = React.useState(TRAIL.length - 1)
  const [collapsed, setCollapsed] = React.useState(false)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setCollapsed(false)
      setCount(0)
      setCurrent(0)
      await sleep(500)
      for (let at = 0; at < TRAIL.length; at++) {
        setCount(at + 1)
        setCurrent(at)
        await sleep(1100)
      }
      setCurrent(TRAIL.length)
      await sleep(1100)
      setCollapsed(true)
      await sleep(2400)
    },
    { gap: 500, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 360px)" }}
      onPointerDown={() => {
        setHands(true)
        setCount(TRAIL.length)
        setCurrent(TRAIL.length)
      }}
    >
      <AiThoughtTrail
        thoughts={TRAIL.slice(0, count)}
        current={current}
        total="3.1s"
        collapsed={collapsed}
        onCollapsedChange={setCollapsed}
      />
    </div>
  )
}

const TICKS = [
  "Reading the hero section",
  "Listing reveal components",
  "Comparing three eases",
  "Checking reduced motion",
  "Choosing the stagger value",
]

function Ticker() {
  const root = React.useRef<HTMLDivElement>(null)
  const [step, setStep] = React.useState(3)
  const [done, setDone] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setDone(false)
      for (let at = 1; at <= TICKS.length; at++) {
        setStep(at)
        await sleep(1300)
      }
      setDone(true)
      await sleep(2000)
    },
    { gap: 500 }
  )

  return (
    <div ref={root} style={{ display: "grid", width: "min(100%, 360px)" }}>
      <AiThoughtTicker
        label={done ? "Odo is ready" : "Odo is thinking"}
        thought={done ? "Stagger set to 0.05s" : (TICKS[step - 1] ?? "")}
        step={step}
        steps={TICKS.length}
        status={done ? "done" : "thinking"}
      />
    </div>
  )
}

export function AiReasoningDemo({ only }: { only?: string }) {
  return (
    <AiDemo
      only={only}
      tabs={[
        { name: "Disclosure", node: <Disclosure /> },
        { name: "Thought trail", node: <Trail /> },
        { name: "Ticker", node: <Ticker /> },
      ]}
    />
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-reasoning.json

Usage

import {
  AiReasoning,
  AiThoughtTicker,
  AiThoughtTrail,
} from "@/components/ui/ai-reasoning"
<AiReasoning
  thoughts={lines}
  status={thinking ? "thinking" : "done"}
  elapsed="0:03"
  summary="Thought for 4s"
>
  I will reveal the headline only.
</AiReasoning>

With the AI SDK, split the message's reasoning part into lines for thoughts and use its state for status.

Versions

  • AiReasoning is a disclosure. While thinking, the label shimmers beside a timer and lines stagger in. When status turns done it fades out and folds to summary; the button reopens it, and aria-expanded follows.
  • AiThoughtTrail puts each thought beside a dot on a rail. The current dot pings blue and the rail fills behind finished thoughts. Once everything is done it can fold to a summary chip that reopens the trail.
  • AiThoughtTicker has no disclosure. One line rolls through the current thought beside a thinking face, with a segment per step.

Props

AiReasoning

PropTypeDefaultDescription
thoughtsstring[]The reasoning so far, one line per entry.
status"thinking" | "done"Thinking shimmers the label; done folds the lines.
elapsedstringElapsed time while thinking.
summarystring"Thought it through"The label once done.
openbooleanControls the disclosure. Omit to let it manage itself.
onOpenChange(open: boolean) => voidCalled when the disclosure opens or closes.
childrenReactNodeThe answer shown under the reasoning.

AiThoughtTrail

PropTypeDefaultDescription
thoughts{ text, time? }[]The thoughts that have arrived.
currentnumberIndex of the thought in progress, or the length when all are done.
totalstringTotal time for the summary chip.
collapsedbooleanControls the summary state.
onCollapsedChange(collapsed: boolean) => voidCalled when the trail folds or reopens.

AiThoughtTicker

PropTypeDefaultDescription
labelstringThe small line above the thought.
thoughtstringThe thought in progress. Rolls on change.
stepnumberSteps complete, counting the current one.
stepsnumber5Total steps.
status"thinking" | "done""thinking"Done turns the segments green.