Code Block React component for Tailwind CSS v4
v1.0.1Updated in 1.2.1
A React code block with syntax highlighting, line numbers, highlighted lines and a copy button, themed from a single accent color.
Pick an accent to re-shade the whole block from that one color: keywords, strings, comments, selection and the highlighted-line wash are all tints of it. Hit the copy button to put the code on the clipboard and watch the icon spring into a check. Long lines scroll sideways inside the block, and the code area is focusable so arrow keys scroll it too.
Code Block is a free React component in the React data display components category, written in TypeScript for React 19 and Tailwind CSS v4. It installs as source with npx wingo-ui@latest add code-block, together with UIProvider and the npm packages lucide-react, motion and prism-react-renderer.
Add to a project
Requirements: React 19 and Tailwind CSS v4, in Next.js (App Router or Pages), Vite or React Router. Installation guide · Next.js setup · Vite setup
npx wingo-ui@latest add code-blockUsage
Import CodeBlock from @/components/ui/code-block.
import CodeBlock from "@/components/ui/code-block"
export function Demo() {
return (
<CodeBlock
code={`const greet = (name: string) => \`Hello, \${name}!\``}
language="ts"
accent="#F75001"
filename="greet.ts"
highlightLines={[1]}
className="max-h-96 w-full max-w-xl"
/>
)
}Props
code*stringThe source code to render. Leading blank lines and trailing whitespace are trimmed.
languagedefault"tsx"stringPrism language id, e.g. "tsx", "css", "json", "python". Also shown in the header when there is no filename. Bundled: markup, jsx, tsx, js, ts, css, json, python, go, rust, sql, yaml, graphql, swift, kotlin, cpp, markdown.
accentdefault"#F75001"stringAny hex color. The whole theme is shades of it: keywords take the accent itself, strings, numbers and functions are lighter or darker tints, and selection and highlighted lines get a soft wash of it.
modedefault"auto""auto" | "dark" | "light"Color scheme. Auto follows the page theme (html dark/light class, data-theme, or OS preference). Pass dark or light to pin a palette: dark puts light tints of the accent on a dark surface, light flips the ramp.
filenamedefault""stringFilename or path shown on the left of the header. Falls back to the language id when empty or omitted.
sizedefault"md""sm" | "md" | "lg"Code at 12 / 13 / 14px on 20 / 24 / 28px lines, with a 36 / 40 / 44px header.
radiusdefault"md""none" | "sm" | "md" | "lg" | "full"Corner radius of the frame: 0, 12, 16 or 24px; full falls back to 24px.
backgrounddefault""stringAny CSS color for the framed surface. Empty uses the neutral surface of the mode (near black or near white). Token colors are tuned for 4.5:1 on those defaults, so pair a dark background with mode dark and a light one with mode light.
showFramedefaulttruebooleanToggles the outer frame: background, border, rounded corners, and header. Turn off to render nothing but the highlighted code.
showHeaderdefaulttruebooleanToggles the header bar. When hidden, the copy button floats over the top-right corner instead. Ignored when showFrame is off.
showLineNumbersdefaulttruebooleanToggles the line-number gutter.
showCopyButtondefaulttruebooleanToggles the copy-to-clipboard button.
wrapdefaultfalsebooleanWraps long lines inside the block instead of scrolling sideways; line numbers stay on the first row of each line.
maxHeightdefault0numberCaps the block's height in pixels and scrolls the code inside it. 0 means no limit.
copyLabeldefault"Copy code"stringAccessible name of the copy button.
copiedLabeldefault"Copied"stringAccessible name of the copy button right after copying.
copyResetMsdefault1800numberHow long the check stays on the copy button, in milliseconds.
highlightLinesnumber[]Optional 1-based line numbers to highlight with a soft accent wash. Off when omitted.
classNamestringExtra classes merged onto the root element (data-slot="code-block"). Use it for width and max-height; the code area scrolls inside.
Examples
Highlighted lines
Pass highlightLines to wash a few lines in the accent, here the request and its error path.
Long file with line numbers
maxHeight caps the block at 260px and scrolls the code inside; line numbers help when you point at a line.
API response in light mode
mode light pins the light palette whatever the page theme, here with a green accent and the small size.
Wrapped query
wrap breaks a long line inside the block instead of scrolling it sideways.
Inside a docs paragraph
size sm without line numbers, wrapped to fit: a short snippet that sits in running text.
Dependencies
Files
- components/ui/code-block.tsx
FAQ
Is the React Code Block component free?
Yes. The source is free and needs no account: copy it from the Code tab or install it with the CLI.
How do I install Code Block?
Run npx wingo-ui@latest add code-block. It also adds the Wingo UI item it builds on (UIProvider) and the npm packages lucide-react, motion and prism-react-renderer. With shadcn, run npx shadcn@latest add https://wingo-ui.com/r/code-block.json.
See also
Used in
More React data display components
Articles using this component
- React Terminal Component: Typing Animation Without Jank
Component guides8 min read
- Nextjs llms.txt: Generate llms-full.txt and .md Pages
Building UI with AI agents10 min read
- React Diff Viewer in React 19: Split, Unified, Word Diffs
Component guides9 min read
- React JSON Viewer: Search, Copy Paths and Big Payloads
Component guides9 min read