About
Terminal output with real ANSI colors and styles: a test run streams in with colored passes and failures, a failure block with its source lines, and a summary, while the header shows running then the exit code.
Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Ansi Output to your project and fits it to your theme.
Code
How Ansi Output is used: the demo's code. The component's own source comes with a free account.
Usage.tsx
"use client"
import * as React from "react"
import { AnsiOutput } from "@/components/ui/ansi-output"
const FIT_CSS = "html,body{overflow:hidden}[data-fit-small] [data-fit-hide]{display:none!important}"
/**
* 21st shows a demo in a small frame: about 674 by 421 on a component page, 358 by 224 on a
* phone and 245 by 139 on a profile card. A demo taller than its frame gets scrollbars, which
* render white on dark pages. FitFrame fills the frame exactly instead.
*
* - "scale": the content shrinks until it fits, centered. For visuals such as orbs.
* - "crop": the content stays at full size from the top and fades out at the bottom. For text
* that streams in, where a changing scale would make the page breathe.
*
* `minWidth` lays out a wide demo (a table, a two column panel) at that width and scales it down
* to the frame, instead of letting it collapse to a narrow layout. The frame centers it, so it
* shrinks evenly on both sides.
*
* Anything marked `data-fit-hide` (state buttons, the chatcn.dev line) drops out in frames
* shorter than `hideBelow`, so a profile card shows the visual alone.
*/
function FitFrame({
children,
mode = "scale",
hideBelow = 360,
minWidth,
}: {
children: React.ReactNode
mode?: "scale" | "crop"
hideBelow?: number
minWidth?: string
}) {
const content = React.useRef<HTMLDivElement>(null)
const [cropped, setCropped] = React.useState(false)
React.useLayoutEffect(() => {
const el = content.current
if (!el) return
const measure = () => {
const vw = window.innerWidth
const vh = window.innerHeight
el.toggleAttribute("data-fit-small", vh < hideBelow)
// Layout sizes ignore the transform, so measuring never feeds back.
const w = Math.max(el.offsetWidth, el.scrollWidth)
const h = Math.max(el.offsetHeight, el.scrollHeight)
const scale = mode === "scale" ? Math.min(1, vw / w, vh / h) : 1
el.style.transform = scale < 1 ? `scale(${scale})` : ""
setCropped(mode === "crop" && h > vh + 1)
}
measure()
const observer = new ResizeObserver(measure)
observer.observe(el)
// Content that changes inside a fixed width box (a headline that cycles words) never resizes
// the box itself, so changes below it measure again on the next frame.
let frame = 0
const mutations = new MutationObserver(() => {
cancelAnimationFrame(frame)
frame = requestAnimationFrame(measure)
})
mutations.observe(el, { subtree: true, childList: true, characterData: true })
window.addEventListener("resize", measure)
return () => {
observer.disconnect()
mutations.disconnect()
cancelAnimationFrame(frame)
window.removeEventListener("resize", measure)
}
}, [mode, hideBelow])
const fade = "linear-gradient(to bottom, #000 calc(100% - 40px), transparent)"
return (
<div
className="flex w-full justify-center overflow-hidden"
style={{
height: "100dvh",
alignItems: mode === "crop" ? "flex-start" : "center",
maskImage: cropped ? fade : undefined,
WebkitMaskImage: cropped ? fade : undefined,
}}
>
<style>{FIT_CSS}</style>
<div
ref={content}
className="w-full shrink-0"
style={{ transformOrigin: mode === "crop" ? "top center" : "center", minWidth }}
>
{children}
</div>
</div>
)
}
const E = "\u001b["
const LINES = [
`${E}1m${E}46m RUN ${E}49m${E}22m ${E}36mv3.2.4${E}39m ${E}90m/Users/mira/code/checkout${E}39m`,
"",
` ${E}32m✓${E}39m src/lib/money.test.ts ${E}90m(14 tests)${E}39m ${E}90m12ms${E}39m`,
` ${E}32m✓${E}39m src/lib/partial-json.test.ts ${E}90m(38 tests)${E}39m ${E}90m41ms${E}39m`,
` ${E}32m✓${E}39m src/api/intents.test.ts ${E}90m(9 tests)${E}39m ${E}33m208ms${E}39m`,
` ${E}31m❯${E}39m src/api/webhooks.test.ts ${E}90m(6 tests | ${E}31m1 failed${E}39m)${E}39m ${E}90m96ms${E}39m`,
` ${E}32m✓${E}39m verifies the Stripe signature`,
` ${E}31m×${E}39m retries a failed delivery with backoff`,
"",
`${E}31m⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯${E}39m`,
"",
`${E}41m${E}1m FAIL ${E}22m${E}49m src/api/webhooks.test.ts > retries a failed delivery with backoff`,
`${E}31m${E}1mAssertionError${E}22m: expected 2 retries, received 3${E}39m`,
"",
`${E}36m ❯ src/api/webhooks.test.ts:${E}2m88:22${E}22m${E}39m`,
` ${E}90m 86|${E}39m await deliver(event, { maxRetries: 2 })`,
` ${E}90m 87|${E}39m `,
` ${E}31m${E}1m 88|${E}22m${E}39m expect(fetchMock).toHaveBeenCalledTimes(3)`,
"",
` ${E}2mTest Files ${E}22m ${E}1m${E}31m1 failed${E}39m${E}22m${E}2m | ${E}22m${E}1m${E}32m3 passed${E}39m${E}22m ${E}90m(4)${E}39m`,
` ${E}2m Tests ${E}22m ${E}1m${E}31m1 failed${E}39m${E}22m${E}2m | ${E}22m${E}1m${E}32m66 passed${E}39m${E}22m ${E}90m(67)${E}39m`,
` ${E}2m Duration ${E}22m 1.84s`,
]
/** Streams a Vitest run line by line, ends with exit code 1, then starts over. */
function AnsiOutputDemo() {
const [count, setCount] = React.useState(0)
const done = count >= LINES.length
React.useEffect(() => {
const id = window.setTimeout(
() => setCount((c) => (c >= LINES.length ? 0 : c + 1)),
done ? 3200 : count === 0 ? 500 : 140,
)
return () => window.clearTimeout(id)
}, [count, done])
return (
<div className="w-full max-w-2xl">
<AnsiOutput
command="pnpm vitest run src"
text={LINES.slice(0, count)
.map((l) => `${l}\n`)
.join("")}
streaming={!done}
exitCode={done ? 1 : undefined}
durationMs={done ? 2310 : undefined}
maxHeight={360}
/>
</div>
)
}
/** The line under each 21st demo that points back to the kit. */
function MoreOn({ slug, children }: { slug: string; children: React.ReactNode }) {
return (
<p data-fit-hide className="text-[0.75rem]/[1rem] text-(color:--chatcn-fg-faint) text-center text-balance">
{children}{" "}
<a
href={`https://chatcn.dev/docs/components/${slug}`}
target="_blank"
rel="noopener"
className="text-(color:--chatcn-fg-subtle) hover:text-(color:--chatcn-fg) underline underline-offset-2"
>
chatcn.dev
</a>
</p>
)
}
/** The kit's own example, framed for the 21st preview. */
export default function AnsiOutputPreview() {
return (
<FitFrame minWidth="38rem">
<div className="flex w-full flex-col items-center justify-center gap-4 p-6">
<div className="flex h-[24rem] w-full max-w-2xl items-start justify-center">
<AnsiOutputDemo />
</div>
<MoreOn slug="ansi-output">Terminal output with real ANSI colors and styles. On</MoreOn>
</div>
</FitFrame>
)
}








