Code Block

A neumorphism-styled code block component with syntax highlighting and copy functionality.

Preview

tsx
function greet(name: string) {
  return `Hello, ${name}!`
}

Installation

bash
npx shadcn@latest add @zerodrive/code-block

Then import the component:

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

Basic Usage

Pass your code as a string to the code prop. The component is an async Server Component that uses Shiki for syntax highlighting.

tsx
<CodeBlock
  code={`const message = "Hello, World!"`}
  lang="tsx"
/>

Languages

Supports multiple languages for syntax highlighting.

TypeScript

typescript
interface User {
  id: number
  name: string
  email: string
}

const users: User[] = []

Bash

bash
npm install @radix-ui/react-slot
pnpm add class-variance-authority

JSON

json
{
  "name": "my-app",
  "version": "1.0.0",
  "dependencies": {
    "react": "^18.0.0"
  }
}

CSS

css
.button {
  background: #f0f0f3;
  border-radius: 8px;
  box-shadow: -1px -1px 3px white,
              1.5px 1.5px 3px rgba(174, 174, 192, 0.4);
}

With Filename

Display a custom filename in the header.

components/button.tsx
export function Button({ children }) {
  return (
    <button className="btn">
      {children}
    </button>
  )
}
tsx
<CodeBlock
  code="..."
  lang="tsx"
  filename="components/button.tsx"
/>

Props

PropTypeDefaultDescription
codestring-The code string to display and highlight.
langstring"tsx"The language for syntax highlighting (e.g., tsx, typescript, bash, json, css).
filenamestring-Optional filename to display in the header. Defaults to the lang value.
showLineNumbersbooleanfalseWhether to display line numbers.
classNamestring-Additional CSS classes to apply.

Features

  • Syntax Highlighting - Powered by Shiki with the GitHub Light theme
  • Copy to Clipboard - Built-in copy button with visual feedback
  • Server Component - Async component for optimal performance
  • Multiple Languages - Supports tsx, typescript, bash, json, css