Stream Box.
A collapsible box for streamed model output with a title and character count. It follows the end of the text as it grows until the reader scrolls up.
"use client";
import * as React from "react";
import { RotateCcwIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import { StreamBox } from "@/components/ui/stream-box";
const THINKING = `The player wants a strip mine at y=-59. Diamonds are most common between -64 and -54, so -59 is a good middle.
Three branches of 32 blocks, spaced 3 apart so no ore is skipped between them.
Torches every 8 blocks keep mobs from spawning in the tunnels.
td.mine.stairs can walk down first. Then td.run wraps the loop so a full inventory triggers the trip home and unload.
Each step: check(), dig and move forward, clear the block above for a 2-high tunnel.
After a branch, turn right, move 3 over, turn right again for the next branch.
Fuel: about 3 fuel per block with the stairs, so roughly 480 for the whole job. Testy has 3,602.`;
export default function StreamBoxDemo() {
const [length, setLength] = React.useState(0);
const streaming = length < THINKING.length;
React.useEffect(() => {
if (!streaming) return;
const id = setTimeout(() => setLength((n) => Math.min(THINKING.length, n + 6)), 40);
return () => clearTimeout(id);
}, [length, streaming]);
return (
<div className="grid w-full max-w-md gap-3">
<StreamBox title={streaming ? "Thinking" : "Thought for 0:18"} text={THINKING.slice(0, length)} streaming={streaming} />
<Button size="sm" variant="ghost" className="justify-self-start" onClick={() => setLength(0)} disabled={streaming}>
<RotateCcwIcon /> Stream again
</Button>
</div>
);
}Installation#
With the CLI
$npx lanterncn add stream-boxThis also installs the Lantern theme and any Lantern UI components it depends on. The shadcn CLI works directly too: npx shadcn@latest add httptim/lantern-ui/stream-box. See Installation if your project is not set up yet.
Manually
Install the dependencies
npm install lucide-reactCopy the source into your project
components/ui/stream-box.tsx"use client"; import * as React from "react"; import { ChevronRightIcon } from "lucide-react"; import { cn } from "@/lib/utils"; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"; /** 950 -> "950", 2300 -> "2.3K", 21400 -> "21.4K". */ function formatCount(n: number) { if (n < 1000) return String(n); if (n < 1_000_000) return `${(n / 1000).toFixed(n < 100_000 ? 1 : 0).replace(/\.0$/, "")}K`; return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`; } /** * A collapsible box for streamed model output (thinking, code being written, logs). * It follows the end of the text as it grows, until the reader scrolls up. */ function StreamBox({ className, title, text = "", meta, streaming = false, showCount = true, open, defaultOpen = true, onOpenChange, maxHeight = 160, children, ...props }: Omit<React.ComponentProps<typeof Collapsible>, "title"> & { title: React.ReactNode; /** The streamed text so far. */ text?: string; /** Extra text on the right of the header, e.g. "whole request 2:16". */ meta?: React.ReactNode; /** Shows a pulsing dot and keeps following new text. */ streaming?: boolean; /** Show the character count after the title. */ showCount?: boolean; /** Height of the scroll area, e.g. 160 or "12rem". */ maxHeight?: number | string; }) { const scrollRef = React.useRef<HTMLDivElement>(null); const stick = React.useRef(true); function onScroll() { const el = scrollRef.current; if (!el) return; stick.current = el.scrollHeight - el.scrollTop - el.clientHeight < 24; } React.useLayoutEffect(() => { const el = scrollRef.current; if (el && stick.current) el.scrollTop = el.scrollHeight; }, [text, children]); return ( <Collapsible data-slot="stream-box" data-streaming={streaming || undefined} open={open} defaultOpen={defaultOpen} onOpenChange={(next) => { if (next) stick.current = true; onOpenChange?.(next); }} className={cn("group/stream min-w-0 rounded-md border bg-background", className)} {...props} > <CollapsibleTrigger data-slot="stream-box-trigger" className={cn( "flex w-full min-w-0 cursor-pointer items-center gap-2 px-2.5 py-2 text-left font-mono text-[10px] tracking-[0.12em] text-muted-foreground uppercase outline-none", "rounded-md hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/40", )} > <ChevronRightIcon aria-hidden="true" className="size-3 shrink-0 transition-transform group-data-[state=open]/stream:rotate-90" /> {streaming && ( <span aria-hidden="true" className="size-1.5 shrink-0 animate-lantern-pulse rounded-full bg-primary [animation-duration:1.2s]" /> )} <span className="min-w-0 truncate"> {title} {showCount && text.length > 0 && ( <span data-slot="stream-box-count" className="tabular-nums"> {" / "} {formatCount(text.length)} chars </span> )} </span> {meta != null && ( <span data-slot="stream-box-meta" className="ml-auto shrink-0 tabular-nums max-sm:hidden"> {meta} </span> )} </CollapsibleTrigger> <CollapsibleContent> <div ref={scrollRef} data-slot="stream-box-content" onScroll={onScroll} aria-busy={streaming} tabIndex={0} className="overflow-y-auto overscroll-contain border-t px-2.5 py-2 outline-none focus-visible:ring-2 focus-visible:ring-ring/40 focus-visible:ring-inset" style={{ maxHeight }} > {children ?? ( <pre className="m-0 font-mono text-[11px] leading-relaxed break-words whitespace-pre-wrap text-muted-foreground"> {text} {streaming && ( <span aria-hidden="true" className="ml-px inline-block h-3 w-1.5 translate-y-0.5 animate-lantern-blink bg-primary/80" /> )} </pre> )} </div> </CollapsibleContent> </Collapsible> ); } export { StreamBox, formatCount };Update the import paths to match your project
The source imports
cnfrom@/lib/utilsand uses collapsible.
Usage#
import { StreamBox, formatCount } from "@/components/ui/stream-box";<StreamBox title="Thinking" text={thinking} streaming />
<StreamBox title="Thought for 1:31" meta="Whole request 2:16" text={thinking} defaultOpen={false} />Pass the text received so far and re-render as chunks arrive. While streaming the header shows a pulsing dot and the text ends in a blinking cursor. Scrolling up stops the follow; scrolling back to the bottom or reopening the box resumes it.
The content is not a live region, so screen readers are not flooded while tokens arrive. Announce the finished result elsewhere. Pass children instead of text to render your own content, such as highlighted code.
Examples#
Collapsed summary line
import { StreamBox } from "@/components/ui/stream-box";
const THOUGHT = `Guestbook entries live in ctx.read("entries"). Validate both fields before writing.
Keep the newest 10 so the page stays short. Redirect back to / after a post so a refresh does not sign twice.`;
export default function StreamBoxSummary() {
return (
<StreamBox
className="w-full max-w-md"
title="Thought for 1:31"
meta="Whole request 2:16"
text={THOUGHT}
defaultOpen={false}
/>
);
}