Ballmac UI home

Code Block

A code panel with filename and language header, line numbers, highlighted lines, a wrap toggle, copy feedback and file tabs. No highlighter bundled; pass Shiki output as children.

components/save-button.tsxtsx
import { Button } from "@/components/ballmac/button" export function SaveButton({ saving }: { saving: boolean }) {  return <Button loading={saving}>Save changes</Button>}

Installation

$ pnpm dlx shadcn@latest add @ballmac/code-block

Usage

import { CodeBlock, CodeBlockTabs } from "@/components/ballmac/code-block"

The full example is in the Code tab above.

Examples

File tabs

tsx
import { Hero } from "@/components/hero" export default function Page() {  return <Hero />}

API reference

<CodeBody>

PropTypeDefault
codestring—
childrenReact.ReactNode—
lineNumbers*boolean—
highlight*number[]—
wrap*boolean—
label*string—
codeRef*React.RefObject<HTMLDivElement | null>—
classNamestring—

<CodeBlock>

PropTypeDefault
code

Source as plain text. Rendered unhighlighted in monospace; required for `highlight`.

string—
children

Pre-highlighted markup (e.g. Shiki's output as JSX) used instead of `code`. Pass `code` too so copy gets clean text.

React.ReactNode—
filename

Shown in the header, e.g. "app/page.tsx".

string—
language

Language label shown in the header, e.g. "tsx".

string—
lineNumbers

Show line numbers.

booleanfalse
highlight

1-based line numbers to highlight (plain `code` only).

number[][]
wrap

Wrap long lines instead of scrolling horizontally (initial value when the toggle is shown).

boolean—
wrapToggle

Show a button that toggles line wrapping.

booleanfalse
copyable

Show the copy button.

booleantrue
bodyClassName

Classes for the scrolling body, e.g. a max height.

string—

<CodeBlockTabs>

PropTypeDefault
files*

One tab per file.

CodeBlockFile[]—
defaultValue

Filename of the tab open at first.

string—
value

Controlled open tab (a filename).

string—
onValueChange

Called with the filename of the newly selected tab.

(filename: string) => void—
lineNumbers

Show line numbers.

booleanfalse
wrap

Wrap long lines instead of scrolling horizontally.

booleanfalse
copyable

Show the copy button (copies the open file).

booleantrue
bodyClassName

Classes for each scrolling body.

string—

Also accepts the standard attributes of its root element.

Accessibility

KeyAction
TabFocuses the scrollable code region (arrow keys scroll long lines), then wrap and copy buttons
← / →In CodeBlockTabs, moves between file tabs
—Copying announces 'Copied to clipboard' through a polite live region

Use with AI

<CodeBlock code filename language lineNumbers highlight={[2,3]} /> renders plain monospace text. For syntax colors, highlight on the server (e.g. Shiki) and pass the result as children plus the raw code for copying. <CodeBlockTabs files={[…]}> shows several files. With the shadcn MCP server set up (guide), ask your agent:

Add the Ballmac UI Code Block (@ballmac/code-block) to this project with the shadcn MCP, then use it where it fits.

Use it for

  • Code samples in docs, READMEs rendered on the web and blog posts
  • Showing a changed file with specific lines highlighted
  • Several related files (component, usage, config) in one panel with tabs

Not for

  • One-line install commands for several package managers (use install-tabs)
  • Terminal sessions with prompts and output (use terminal)
  • Editable code (use a code editor)

Registry JSON: https://ui.ballmac.com/r/code-block.json

Credits

Free to use in personal and commercial projects.