Shadcn Streaming Code Components

A code block for AI chat and agent UIs that stays readable while the agent is still writing: lines appear as they stream, the body follows the newest line, a status pill flips from Writing to Ready and copy, diff and Apply or Reject actions are ready when the file is complete. Built on Code Block with Shiki highlighting. Browse our collection of 8 Shadcn Streaming Code variants.

Basic

lib/utils.ts
typescriptWriting
import { clsx, type ClassValue } from "clsx";

Complete with copy

lib/schemas/signup.ts
typescriptReady
import { z } from "zod";
export const signupSchema = z.object({ name: z.string().min(2, "Enter your full name"), email: z.string().email("Enter a valid email"), password: z.string().min(8, "Use at least 8 characters"), plan: z.enum(["free", "pro", "team"]).default("free"),});
export type SignupValues = z.infer<typeof signupSchema>;

Highlighted lines

middleware.ts
typescriptWriting
import { NextResponse, type NextRequest } from "next/server";

Streaming diff

lib/invoices.ts
Writing
export async function getInvoices(teamId: string) {

Multiple files

components/theme-toggle.tsx
tsxWriting
"use client";
Coding agent

Apply or reject

lib/rate-limit.ts
Writing
const hits = new Map<string, number[]>();
Code review

Inside a chat message

The search box fires a request on every key press. Can you debounce it?
Here is a small hook that waits until typing pauses:
hooks/use-debounce.ts
Writing
import { useEffect, useState } from "react";
AI assistant

Terminal output

Terminal
Running
$ pnpm test --run
Agent terminal
Unlock premium components
Get full control of shadcn/ui components, blocks and instances, including future additions.

Explore Shadcn Blocks

Ready-made page sections built with the same components.

View all blocks

Shadcn Streaming Code Component

The Shadcn Streaming Code component is a code block for AI chat and coding agent interfaces that stays readable while the model is still writing. Lines appear as they arrive, the body follows the newest line, and a status pill in the header tells the user whether the agent is still writing or the file is ready to copy or apply.

The examples cover a basic stream with a replay button, a finished file with copy, highlighted lines, a diff that is written line by line, an agent editing two files in tabs, Apply and Reject actions that appear only when the code is complete, a code reply inside a chat message and streaming terminal output. It is a thin layer over the kit's Code Block, highlighted with Shiki and styled with shadcn/ui and Tailwind CSS.

What is the Shadcn Streaming Code Component?

A streaming code block renders a source file that is still growing. You pass the text received so far as code and set streaming while the response is open. Highlighting runs a chunk behind the stream, so the newest characters show as plain text for a moment and gain color right after, instead of the block flashing or going blank.

Use it wherever an assistant or agent writes code in front of the user: chat replies, file edits in a coding agent, generated configuration or command output. For static snippets in documentation, the plain Code Block is enough.

Anatomy of the Shadcn Streaming Code

PartDescription
StreamingCodeThe root. Accepts every CodeBlock prop such as code, language, streaming, highlightedLines and diff, shows line numbers by default and caps the scrolling body with visibleLines (12 by default).
StreamingCodeHeaderA 40px header bar built on CodeBlockHeader that holds the file name, language, status and copy button.
StreamingCodeTitleA file icon and a monospace file name that truncates on narrow screens. Pass icon to replace the icon, for example with a terminal icon.
StreamingCodeStatusA pill that shows a spinner and Writing while streaming and a check with Ready when complete. Rename both states with labels.
StreamingCodeActionsA footer for agent actions such as Apply and Reject. It renders nothing while the code is streaming, so users cannot act on half a file.
CodeBlockLanguage and CodeBlockCopyButtonReused from Code Block: an uppercase language label and a copy button that switches to a check and the Copied label after copying.

8 Shadcn Streaming Code Variants

Each streaming code variant listed here is previewed live at the top of this page, together with its source code and install command.

  • Basic and complete: A file revealed line by line with a Replay button, and the same layout for a finished file with copy.
  • Highlighted lines and diff: A stream that marks the important lines, and a diff whose removed and added lines are tinted as they are written.
  • Multiple files: An agent writing two files in sequence, shown as tabs with a spinner or check per file. The active tab follows the file being written.
  • Apply or reject: Apply and Reject buttons that appear when the code is ready. Apply confirms with a Sonner toast.
  • Chat message and terminal: A code reply inside a Message with Bubble text around it, and ANSI colored test output with Running and Done labels.

How to Install the Shadcn Streaming Code Component

Set up shadcn/ui in your project if you have not already, then add the Shadcn UI Kit registry to your components.json once:

{
  "registries": {
    "@shadcnuikit": "https://shadcnuikit.com/r/{name}.json"
  }
}

Then install a streaming code variant by its name:

npx shadcn@latest add @shadcnuikit/streaming-code1

The same command works with pnpm dlx, yarn dlx and bunx --bun. Each variant has a copy button for every package manager, and the code is added to your project as plain React and Tailwind CSS that you can edit freely.

How to Use the Shadcn Streaming Code Component

Pass the partial text and a streaming flag from your stream, and compose the header parts you need. When the stream ends, the status flips to Ready and the actions appear.

import {
  CodeBlockCopyButton,
  CodeBlockLanguage,
} from "@/components/ui/code-block"
import {
  StreamingCode,
  StreamingCodeHeader,
  StreamingCodeStatus,
  StreamingCodeTitle,
} from "@/components/ui/streaming-code"

export function AgentFile({ code, done }: { code: string; done: boolean }) {
  return (
    <StreamingCode code={code} language="typescript" streaming={!done}>
      <StreamingCodeHeader>
        <StreamingCodeTitle>lib/utils.ts</StreamingCodeTitle>
        <CodeBlockLanguage />
        <StreamingCodeStatus />
        <CodeBlockCopyButton className="rounded-full" />
      </StreamingCodeHeader>
    </StreamingCode>
  )
}

Why Use the Shadcn Streaming Code Component

  • Readable while it streams: New lines fade in, a caret marks the end of the stream and the plain text fallback means the block never goes blank between chunks.
  • Follows the newest line: The body scrolls to the bottom as lines arrive and stops following as soon as the user scrolls up to read, then resumes when they scroll back down.
  • Clear status: The Writing and Ready pill and the aria-busy state tell users and assistive technology when the code is safe to copy or apply.
  • Built on Code Block: Highlighting, diffs, highlighted lines, line numbers and copy all come from Code Block, so both components look and behave the same.
  • Theme aware: Colors come from your theme tokens and Shiki light and dark themes, so the block matches light and dark mode without extra work.

Common Streaming Code Use Cases

  • AI chat replies: Code answers from an assistant, shown inside the message.
  • Coding agents: File edits written one after another, with review actions.
  • Code review: Proposed diffs that the user applies or rejects.
  • Generated config: Schemas, environment files and settings written on demand.
  • Agent terminals: Live output from tests, builds and install commands.

Customizing the Shadcn Streaming Code

Change how many lines are visible before the body scrolls with visibleLines, turn off numbers with showLineNumbers={false} and rename the status with labels. Every class can be overridden with className, and colors follow your theme variables, which you can create with the free Theme Generator.

To render terminal output, convert ANSI text with ansiToLines and pass the result as lines:

import { ansiToLines } from "@/components/ui/code-block"

<StreamingCode
  lines={ansiToLines(output)}
  label="Terminal output"
  streaming={running}
  showLineNumbers={false}
>
  <StreamingCodeHeader>
    <StreamingCodeTitle icon={<TerminalIcon />}>Terminal</StreamingCodeTitle>
    <StreamingCodeStatus labels={{ streaming: "Running", complete: "Done" }} />
  </StreamingCodeHeader>
</StreamingCode>

Streaming Code Accessibility and Keyboard Support

While streaming, the root and the body carry aria-busy, and the body is a polite role="log" region, so screen readers wait instead of reading every token. When the stream ends, a status message announces that code generation is complete with the number of lines. Change it with completeAnnouncement.

The code area is a labelled, focusable region that scrolls with the keyboard. Under reduced motion the spinner stops, new lines do not animate and the body jumps to the newest line instead of smooth scrolling.

KeyAction
TabMoves focus to the copy button, the code area and the actions.
Arrow keysScroll the focused code area vertically and horizontally.
EnterActivates the focused copy, Apply or Reject button.

Streaming Code Best Practices

  • Keep streaming true only while the response is open, so the status and actions reflect the real state.
  • Show actions such as Apply only when the file is complete; StreamingCodeActions does this for you.
  • Cap the height with visibleLines so long files do not push the rest of the chat off screen.
  • Give every block a file name or a label so users and screen readers know what is being written.
  • Use the plain Code Block for static snippets that never stream.

Shadcn Streaming Code for AI Agents

Streaming Code lets an agent show code while it is still being written: the partial text goes in as code, streaming stays true until the response closes, and the block follows the newest line, shows a Writing or Ready pill and reveals Apply and Reject actions only when the file is complete. It also handles streaming diffs, several files in tabs, code inside chat messages and ANSI terminal output.

Install it with an AI agent

Claude Code, Cursor, Codex and other agents can install these with the shadcn CLI, for example npx shadcn@latest add @shadcnuikit/streaming-code1. With the Shadcn UI Kit MCP server, your agent can search every variant by name and add the right one for you. To give it full context, share /components/streaming-code.md, the Markdown version of this page with every variant and install command.

Example prompts

  • “Render every code fence in our assistant replies with StreamingCode, passing streaming only for the fence that is still open, with the file name and a copy button in the header.”
  • “Show each file our coding agent edits as a StreamingCode tab with a diff, and add Apply and Reject buttons in StreamingCodeActions that call our apply endpoint.”
  • “Stream the output of the test run tool into a StreamingCode terminal block using ansiToLines, with Running and Done status labels.”

Customize it with AI

Ask the agent to compose StreamingCode instead of editing components/ui/code-block.tsx, since highlighting, diffs and copy already come from CodeBlock props. Keep the body height with visibleLines rather than wrapping it in another scroll container, because the auto scroll follows the built in body. Put review buttons in StreamingCodeActions so they cannot fire on half a file, and hoist transformers arrays to module scope so the stream does not re-tokenize every chunk.

Components Related to Streaming Code

Shadcn Streaming Code FAQ

Keep the text received so far in state, pass it to StreamingCode as code and set streaming to true until the stream closes. The block highlights as it grows, follows the newest line and flips the status to Ready at the end.

Yes. Following stops as soon as the user scrolls away from the bottom and resumes when they scroll back near the last line.

Put the buttons inside StreamingCodeActions. It renders nothing while streaming is true and shows a footer bar with your buttons once the code is complete.

Yes. Pass a diff prop with the added and removed line numbers, for example diff={{ removed: [3], added: [4, 5] }}. Each line is tinted as soon as it appears.

While streaming it sets aria-busy and a polite log role, so screen readers are not flooded with tokens, and it announces when generation is complete. The spinner, line animation and smooth scrolling are turned off under reduced motion.

Yes. Claude Code, Cursor, Copilot and other assistants can add a streaming code variant with the shadcn CLI or the shadcn MCP server, for example npx shadcn@latest add @shadcnuikit/streaming-code1. With a Pro plan, the Shadcn UI Kit MCP server can also build complete sections and pages around it.

Open /components/streaming-code.md to get this page as Markdown, with every streaming code variant, its description and install command. Paste the link or the text into your AI tool so it uses the real component code instead of guessing.

Looking for something else? Browse all shadcn/ui components or ready-made UI blocks built with them.