CodeBlock

PreviousNext

A beautiful code block component with tabs, copy functionality, and smooth animations

References

Installation

pnpm dlx shadcn@latest add https://cult-ui.com/r/code-block.json

Usage

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

Single Code Block

Display a single code snippet:

<CodeBlock code="console.log('Hello, world!')" language="javascript" />

Multi-Tab Code Block

Display multiple code snippets with tabs:

<CodeBlock tabs={[ { label: "npm", code: "npm install @acme/ui", language: "bash", }, { label: "pnpm", code: "pnpm add @acme/ui", language: "bash", }, { label: "yarn", code: "yarn add @acme/ui", language: "bash", }, ]} />

Package Manager Installation

Common use case for showing different package manager commands:

<CodeBlock tabs={[ { label: "npm", code: "npm install @acme/ui", language: "bash", }, { label: "pnpm", code: "pnpm add @acme/ui", language: "bash", }, { label: "yarn", code: "yarn add @acme/ui", language: "bash", }, { label: "bun", code: "bun add @acme/ui", language: "bash", }, ]} />

Multi-Language Code Examples

Show code examples in different languages:

<CodeBlock tabs={[ { label: ".env.local", code: `NEXT_PUBLIC_API_URL=https://api.example.com API_SECRET_KEY=your-secret-key`, language: "bash", }, { label: "config.ts", code: `export const config = { apiUrl: process.env.NEXT_PUBLIC_API_URL, apiKey: process.env.API_SECRET_KEY, }`, language: "typescript", }, ]} />

API Reference

CodeBlock Props

PropTypeDefaultDescription
tabsCodeTab[]-Array of code tabs for multi-tab display
codestring-Single code string (when not using tabs)
languagestring"bash"Language identifier for syntax highlighting
classNamestring-Additional CSS classes

CodeTab Interface

PropertyTypeDescription
labelstringTab label displayed in the tab bar
codestringCode content for this tab
languagestringOptional language identifier

Features

  • Single & Multi-Tab Support: Use either a single code block or multiple tabs
  • Copy Functionality: Built-in copy button with smooth animations
  • Direction-Aware Animations: Smooth transitions when switching tabs
  • Dark Mode: Automatically adapts to light and dark themes
  • Accessible: Proper ARIA labels and keyboard navigation support
  • Responsive: Scrollable tabs for mobile devices

Examples

Basic JavaScript

<CodeBlock code={`function greet(name) { return \`Hello, \${name}!\` } console.log(greet('World'))`} language="javascript" />

React Component

<CodeBlock code={`import { CodeBlock } from '@/components/code-block' export default function Page() { return ( <CodeBlock code="console.log('Hello, world!')" language="javascript" /> ) }`} language="tsx" />

Configuration Files

<CodeBlock tabs={[ { label: "package.json", code: `{ "name": "my-app", "version": "1.0.0", "dependencies": { "@acme/ui": "^1.0.0" } }`, language: "json", }, { label: "tsconfig.json", code: `{ "compilerOptions": { "target": "ES2020", "module": "commonjs", "strict": true } }`, language: "json", }, ]} />

API Examples

<CodeBlock tabs={[ { label: "curl", code: `curl -X POST https://api.example.com/users \\ -H "Content-Type: application/json" \\ -d '{"name": "John Doe"}'`, language: "bash", }, { label: "JavaScript", code: `const response = await fetch('https://api.example.com/users', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name: 'John Doe' }), })`, language: "javascript", }, { label: "Python", code: `import requests response = requests.post( 'https://api.example.com/users', json={'name': 'John Doe'} )`, language: "python", }, ]} />

Custom Styling

<CodeBlock code="npm install @acme/ui" language="bash" className="my-8" />