āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š shadcn/directory/nolly-studio/cult-ui/components/code-block ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
<ComponentPreview name="code-block-demo" className="[&_.preview>[data-orientation=vertical]]:sm:max-w-[70%]" description="All variations" />
npx shadcn@latest add https://cult-ui.com/r/code-block.json
</TabsContent>
<TabsContent value="manual">
<Steps>
<Step>Copy and paste the following code into your project.</Step>
<ComponentSource name="code-block" /><Step>Update the import paths to match your project setup.</Step>
</Steps> </TabsContent> </CodeTabs>import { CodeBlock } from "@/components/ui/code-block"
Display a single code snippet:
<CodeBlock code="console.log('Hello, world!')" language="javascript" />
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",
},
]}
/>
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",
},
]}
/>
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",
},
]}
/>
| Prop | Type | Default | Description |
| ----------- | ----------- | -------- | ------------------------------------------- |
| tabs | CodeTab[] | - | Array of code tabs for multi-tab display |
| code | string | - | Single code string (when not using tabs) |
| language | string | "bash" | Language identifier for syntax highlighting |
| className | string | - | Additional CSS classes |
| Property | Type | Description |
| ---------- | -------- | ---------------------------------- |
| label | string | Tab label displayed in the tab bar |
| code | string | Code content for this tab |
| language | string | Optional language identifier |
<CodeBlock
code={`function greet(name) {
return \`Hello, \${name}!\`
}
console.log(greet('World'))`}
language="javascript"
/>
<CodeBlock
code={`import { CodeBlock } from '@/components/code-block'
export default function Page() {
return (
<CodeBlock
code="console.log('Hello, world!')"
language="javascript"
/>
)
}`}
language="tsx"
/>
<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",
},
]}
/>
<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",
},
]}
/>
<CodeBlock code="npm install @acme/ui" language="bash" className="my-8" />
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā