Skip to content
GetUrUI

Code Block

.md

AI-style code block with filename header, copy button, line numbers, line highlights and a tiny highlighter; long code collapses behind a height spring.

aicodesyntaxhighlightcopysnippet

Installation

$npx shadcn@latest add @geturui/code-block
Download source (.zip)

Also installs: Copy Button

Usage

usage.tsx
1import { CodeBlock } from "@/components/ai/code-block";
2
3<CodeBlock
4  filename="app/page.tsx"
5  language="tsx"
6  highlightLines={[3]}
7  code={`export default function Page() {
8  return <h1>Hello</h1>;
9}`}
10/>

Props

PropTypeDefaultDescription
code*string—Source to show and copy.
languagestring"tsx"Label and highlighting rules (tsx, ts, js, json, bash, css, python…).
filenamestring—Shown in the header.
showLineNumbersbooleantrueGutter with line numbers.
highlightLinesnumber[]—1-based lines to highlight.
maxLinesnumber14Collapse longer code behind “Show all”. 0 disables.
defaultExpandedbooleanfalseStart expanded.
hideCopyboolean—Hide the copy button.

Accessibility

  • The scroll area is focusable so keyboard users can scroll long lines.
  • Line numbers are aria-hidden so copied or read text stays clean.
  • The collapse toggle uses aria-expanded and aria-controls.

Related