Code Block

Syntax-highlighted source with an optional title or file path, copy, inline editing, line numbers and collapsing.

src/components/ui/code-block.tsx

Install

npx astralyx-ui add code-block

Usage

import { CodeBlock } from '@/components/ui/code-block'

<CodeBlock
  filePath="src/components/ui/button.tsx"
  language="tsx"
  lineNumbers
  code={source}
/>

Composer

Change the props and the source below follows.

src/components/ui/stack.tsx
tsx
import { cn } from '@/lib/utils'

export function Stack({ className, ...props }) {
  return (
    <div
      className={cn('flex flex-col gap-2', className)}
      {...props}
    />
  )
}
Props
tsx
<CodeBlock
  language="tsx"
  filePath="src/components/ui/stack.tsx"
  code={source}
/>

Examples

Footer controls

Default

Header — title, file path, language chip

Editable

Line numbers and highlighted lines

Collapsed past maxLines

Bare — no header, not copyable

Other languages

Wrapped

API

Generated from the source, so it cannot fall behind the code. One table per exported component, then hooks, functions and types.

CodeBlock

Extends Omit< ComponentProps<'div'>, 'children' | 'onChange' | 'defaultValue' >.

PropTypeDefaultDescription
titlestring—Free-form header label. Falls back to the language name.
codestringrequiredThe source to render. Acts as the initial value when editable.
languageLanguage'tsx'Grammar to highlight with. Each one loads as its own chunk.
filePathstring—A path in the same header slot as title, with the directory muted and the file emphasized.
showLanguagebooleanderivedLanguage chip on the right. On whenever the header has a label.
copyablebooleantrueCopy button, showing a checkmark for 1.5s.
editablebooleanfalseType into the block; highlighting follows every keystroke via a transparent overlay.
footerReactNode—A control bar under the code — a Run button, a language switch, a status line. Rendered below the expand row, so "show more" stays attached to the code it expands.
onCodeChange(code: string) => void—Fires on every edit.
resettablebooleaneditableRevert button, shown only once the code differs from the prop.
lineNumbersbooleanfalseCounter gutter down the left edge.
wrapbooleanfalseSoft-wrap long lines instead of scrolling horizontally.
highlightLinesnumber[]—1-based lines to emphasize with an accent background and ring bar.
maxLinesnumber—Collapse past this many lines behind an animated Show more toggle.
minLinesnumber—Keep the code surface at least this tall, in lines. Mostly for editable: an editor that starts one line high and grows is a fiddly target, and the caret has nowhere to land when the buffer is empty.
headerbooleanderivedForce the header on or off. On whenever it would hold anything.