DocsSupportPricing
Roadmap (has updates coming soon)XFigma3.6K
Sign inGet All-Access
  1. Components
  2. Code Block

Shadcn Code Block: Highlight, Streaming, Diffs, AI Chat and more

629 Pro blocksView docsShare

Browse 27 production-ready shadcn code block components for streaming agent responses, AI chat with markdown code fences, diff review, focus walkthroughs, line selection, gutter actions, terminal output, and multi-file patches: a custom ReUI component built on Shiki and composable with Card, Frame, Tabs, and Badge.

Shadcn Code Block: Streaming, Diffs, and AI Chat

ReUI Code Block is a custom shadcn component, not part of the base shadcn/ui library, for rendering source code with syntax highlighting from Shiki. It handles the cases an AI product actually needs: code streaming in token by token, agent-proposed diffs, diagnostics on specific lines, and per-line actions, alongside the ordinary documentation cases.

27 components cover the range: a streaming agent response with a caret and stick-to-bottom scrolling, an AI chat message that renders markdown code fences as they arrive, diff review with added and removed lines, focus walkthroughs, line selection for asking an assistant about a range, terminal output with no highlighting, and multi-file patches in tabs.

The component is composable rather than configured. The root renders the code surface and children supply the chrome, so the same block works bare, inside a shadcn Card, or inside a ReUI Frame without a doubled border.

What is Shadcn Code Block?

ReUI Code Block renders source code as real React elements rather than as an HTML string. Shiki tokenizes the code and the result is normalized into a plain array of lines, which is what makes per-line interaction possible: hover actions in the gutter, click and shift-click line selection, diff markers, and diagnostic backgrounds all attach to elements instead of being baked into markup.

The happy path is one tag. Pass code and a language and you get a complete block with syntax highlighting in both light and dark themes. Everything else, the header, the filename, the language label, the copy button, the wrap toggle and the expand control, is composed as children.

Streaming code and AI chat

When code arrives from a model it grows one chunk at a time. The component re-tokenizes against a deferred value and the highlighter returns the same line objects for lines that did not change, so appending a token re-renders one line rather than the whole file. Lines the highlighter has not caught up to render as plain text, so the newest token is on screen immediately and gains color a frame later.

Most chat UIs hold a markdown message rather than a raw code string. Two helpers cover that: markdownFences splits a message into prose and fenced code and reports a fence whose closing delimiter has not streamed in yet, and markdownCodeProps reads the language and source out of the props react-markdown gives a pre element. An unknown language and a half-finished fence both render as plain text instead of throwing.

Server rendering and bundle cost

The Shiki engine lives in its own module with no client directive, so a React Server Component can call highlightCode and pass the resulting lines down as plain JSON. The browser then never downloads a grammar. The same shape works without React Server Components: return lines from a Remix or TanStack Start loader and pass them to the component.

Highlighting uses Shiki's JavaScript regex engine rather than the WebAssembly one, so installing the component does not force wasm-unsafe-eval into your Content Security Policy. Shiki loads on first highlight and never on import, each language is its own chunk resolved through a static map, and passing pre-highlighted lines or turning highlighting off requests none of it.

Diffs, highlights, and focus

Line state can come from props or from Shiki notation comments. Highlighted lines, highlighted words, added and removed diff lines, focused lines and error, warning or info levels all accept either an array of line numbers or a range string such as 2-4,7. The transformers prop is passed straight to Shiki, so the notation comments from @shikijs/transformers produce the same state without a second code path.

Every piece of state is published as a data attribute on the line, which means restyling any of it is a plain CSS selector and never needs an important flag. Line numbers and diff glyphs are generated content, so they stay out of text selection: selecting code and pasting it gives exact source with no gutter digits.

Frequently Asked Questions

Does the code block work in React Server Components?

Yes. The Shiki engine is a separate module with no client directive, so a server component can await highlightCode and pass the resulting lines to the component. The lines are plain JSON, so the browser never downloads a grammar or the highlighter. The same approach works in Remix and TanStack Start through a loader.

How do I render code from an AI chat message?

Use markdownFences to split the message into prose and fenced code, or markdownCodeProps with react-markdown to override the pre element. Both tolerate a fence that is still streaming, so a half-finished block renders as plain text rather than throwing. Pass streaming to show a caret and keep the viewport pinned to the newest line.

Will installing this make my bundle large?

Shiki is the only npm dependency and it loads on first highlight, never on import. Each language is a separate chunk resolved through a static map, so you download only the grammars you render. Passing pre-highlighted lines or setting highlight to false requests nothing at all.

Does it need wasm-unsafe-eval in my Content Security Policy?

No. Highlighting uses Shiki's JavaScript regex engine rather than the WebAssembly Oniguruma engine, so there is no WebAssembly to allow. This matters for teams with a strict CSP, where a WebAssembly-based highlighter would otherwise force a policy change.

How do I add a language or change the theme?

Languages are a written-out map in code-block-highlight.tsx. Add one line importing the grammar from shiki/langs and it becomes available. Themes work the same way in the themes map; changing the two defaults there restyles every block. The map is deliberately not a template import, because that would make bundlers include every grammar Shiki ships.

Can I use my own syntax highlighter instead of Shiki?

Yes. The component renders from a plain array of lines, each holding tokens with a light and a dark color. Map the output of Prism, highlight.js or anything else into that shape, pass it as the lines prop, and Shiki is never loaded.

Does copying include the line numbers?

No. Line numbers and diff glyphs are CSS generated content rather than DOM elements, so they cannot enter a text selection. Selecting the code and copying gives exact source, and the copy button additionally strips any Shiki notation comments from the payload.
Previous22 Shadcn Checkbox Components
Next10 Shadcn Collapsible Components

Start building with ReUI Pro today.

Every Pro block, every style, and every future addition, with one license for unlimited projects.

Get All-Access

From $249, once. Lifetime updates.

Application

  • App Shell
  • Auth
  • Card
  • ChartAdded 17 Chart blocks for cron jobs, deploys, firewall traffic and SLOs
  • Dashboard
  • Dialog
  • Empty State
  • Event Calendar
  • Flow
  • Form
  • Gantt
  • Kanban Board
  • List
  • Navbar
  • Onboarding
  • Profile
  • Rich Text EditorNew Rich Text Editor category with 5 Tiptap editor blocks
  • Schedule
  • Settings
  • SheetAdded 13 Sheet blocks for cron jobs, deploys, firewall rules, SLOs and data
  • Stats
  • Timeline
  • WhiteboardNew Whiteboard category with 2 Excalidraw board blocks
  • Wizard

Solutions

  • Dev OpsNew Dev Ops category with 6 platform console blocks
  • AI Ops
  • CRMAdded 2 CRM blocks: a forecast call workspace and a deal desk
  • Agents
  • Analytics
  • Billing
  • Bookings
  • Files
  • Inventory
  • Users

AI & Agents

  • AI ChatAdded a corner chat popup block
  • Agent Activity

Templates

  • E-commerce
  • SaaS
  • Dashboard
  • Landing
  • All templates

eCommerce

  • Category Card
  • Checkout
  • Comparison
  • Coupon
  • Filter Sidebar
  • Product Card
  • Product Detail
  • Product Grid
  • Receipt
  • Review
  • Shopping Cart
  • Wishlist
  • Shop Hero

Data Grid

  • Base
  • Columns
  • Drag & Drop
  • EditingAdded a spreadsheet reorder plan Data Grid block
  • Expansion
  • Filtering
  • Grouping
  • Virtualization

Marketing

  • Blog
  • Compare
  • Contact
  • CTA
  • FAQ
  • Hero
  • How It Works
  • PricingAdded 7 Pricing blocks: plan cards, comparison tables and upgrade dialogs

Resources

  • Components
  • Blocks
  • Icons
  • MCP for Agents
  • Docs
  • Support
  • Pricing
  • Tailwind Plus Alternative
  • Roadmap(has updates coming soon)
  • AffiliateSoon

Legal

  • Privacy Policy
  • Terms & Conditions
  • License
  • Refunds
  • Cookies

© 2026 ReUI. All rights reserved.

3.6K

Code block with header, filename, language and copy button.

Headerless block with a pinned copy button and dual-axis scrolling.

Agent writing code in real time with a caret and stick-to-bottom.

Streaming assistant message rendering markdown code fences.

A props-driven diff with added and removed lines and a change summary.

A diff expressed through a shiki transformer instead of props.

Line ranges and word matches highlighted together.

Stepping through regions of a file with the rest dimmed.

Unhighlighted command output with a wrap toggle.

Collapsed at a line budget with an expand control and a line offset.

Error and warning lines with an action button on the active line.

Selecting lines to ask an assistant about them.

Live search marking every match, with a counter that reads the block through useCodeBlockConfig.

A framed file switcher loading one grammar chunk per selected language.

Lines built without shiki, the shape a server component passes down.

Two blocks stacked in a frame for a refactor review.

Long single-line content with a wrap toggle.

A git patch parsed into files with dual old/new gutters, counters and a download.

Syntax colors driven by CSS variables mapped to design tokens.

A block that stays dark in both site themes, with window chrome and every line state.

ANSI-style colored terminal output built from plain lines.

Agent-proposed diff with accept and reject actions.

Fold regions detected from indentation, with nesting and fold-all controls.

A line action scoped to the diagnostic line that applies the patch.

A gutter button that turns lines or a selection into file:line reference badges in a chat composer.

A changed-file list where one shadcn ScrollArea scrolls every block at once.

Agent stdout with SGR colour codes parsed by ansiToLines, retintable via --code-ansi-* variables.