Code Block

A syntax-highlighted code block with an editor-style tab, line-number gutter, highlight accents, and a copy button with an animated check.

Code Block

A theme-aware, syntax-highlighted code block with an editor-style tab, a line-number gutter, per-line highlight accents, and a copy button that swaps to an animated check.

Press the copy button in the tab strip — the icon springs into a check and the label flips to "Copied" for two seconds.

Installation

Install the component with the Craft-UI Blocks CLI — it drops code-block.tsx into your project and wires up its runtime dependencies:

npx mvpblocks add code-block
pnpm dlx mvpblocks add code-block
yarn dlx mvpblocks add code-block
bunx mvpblocks add code-block

File Structure

The CLI saves the component to your project's components/ui folder:

Import it from @/components/ui/code-block.

Usage

CodeBlock is a client component — pass the source as code:

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

const snippet = `export function greet(name: string) {
  return \`Hello, \${name}!\`;
}`;

export function Example() {
  return (
    <CodeBlock
      code={snippet}
      language="typescript"
      filename="greet.ts"
      highlightLines={[1]}
    />
  );
}

With a filename, the copy button pins to the right edge of the editor-style tab strip; without one, it floats over the top-right corner of the code instead.

Theming

The block follows your exact theme with no configuration: the shell uses bg-background, border-border, and the muted surfaces, while the token palette ships dedicated light and dark stops — light colors on white (rose-600 keywords, emerald-600 strings, sky-600 properties) with brighter counterparts in dark mode (rose-400, emerald-400, sky-400). The line-number gutter tracks --color-background and flips to zinc-950 in dark mode, and highlighted rows share the indigo accent used across the app.

Props

Prop

Type

Animations

  • Copy feedback: the Copy icon swaps to a check through a transform-only spring (stiffness: 600) inside an AnimatePresence crossfade — nothing touches the layout thread, and the "Copy"/"Copied" labels overlay a fixed-width slot so the button never shifts width.
  • Reset: the check and "Copied" label revert after 2 seconds.
  • Highlight rows: emphasized lines fade their tint in over 100 ms.
  • Reduced motion: the icon spring and tap-scale are skipped when prefers-reduced-motion is set.

Registry Bundle

Install the exact registry entry when you want code-block.tsx and its declared runtime dependencies together. Dependencies: motion, lucide-react.

Contact

If you find any bug or issue, feel free to raise an issue.

Last updated on