Collaboration
Project Management
Finance
Code Block
Provides syntax highlighting, line numbers, and copy to clipboard functionality for code blocks.
Installation
npx love-ui@latest add code-blockUsage
import { CodeBlock, CodeBlockHeader, CodeBlockFilename, CodeBlockCopyButton, CodeBlockBody, CodeBlockContent } from "@/components/code-block"<CodeBlock>
<CodeBlockHeader>
<CodeBlockFilename>example.tsx</CodeBlockFilename>
<CodeBlockCopyButton code="const example = 'Hello World';" />
</CodeBlockHeader>
<CodeBlockBody>
<CodeBlockContent code="const example = 'Hello World';" language="typescript" />
</CodeBlockBody>
</CodeBlock>Features
- Syntax highlighting
- Line numbers
- Copy to clipboard
- Support for filenames
- Highlighted lines and words
- Focused line
- Diff
- Language detection
- Theme support
- Customizable styles
Examples
No header
function MyComponent(props) { return ( <div> <h1>Hello, {props.name}!</h1> <p>This is an example React component.</p> </div> );}"use client";
import type { BundledLanguage } from "../../../../../packages/code-block";
import {
CodeBlock,
CodeBlockBody,
CodeBlockContent,
CodeBlockItem,
} from "../../../../../packages/code-block";
const code = [
{
language: "jsx",
filename: "MyComponent.jsx",
code: `function MyComponent(props) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
{
language: "tsx",
filename: "MyComponent.tsx",
code: `function MyComponent(props: { name: string }) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
];
const Example = () => (
<CodeBlock data={code} defaultValue={code[0].language}>
<CodeBlockBody>
{(item) => (
<CodeBlockItem key={item.language} value={item.language}>
<CodeBlockContent language={item.language as BundledLanguage}>
{item.code}
</CodeBlockContent>
</CodeBlockItem>
)}
</CodeBlockBody>
</CodeBlock>
);
export default Example;
Highlighted lines
MyComponent.jsx
jsx
function MyComponent(props) { // [!code highlight] return ( <div> <h1>Hello, {props.name}!</h1> // [!code highlight] <p>This is an example React component.</p> </div> );}"use client";
import type { BundledLanguage } from "../../../../../packages/code-block";
import {
CodeBlock,
CodeBlockBody,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockFilename,
CodeBlockFiles,
CodeBlockHeader,
CodeBlockItem,
CodeBlockSelect,
CodeBlockSelectContent,
CodeBlockSelectItem,
CodeBlockSelectTrigger,
CodeBlockSelectValue,
} from "../../../../../packages/code-block";
const code = [
{
language: "jsx",
filename: "MyComponent.jsx",
code: `function MyComponent(props) { // [!code highlight]
return (
<div>
<h1>Hello, {props.name}!</h1> // [!code highlight]
<p>This is an example React component.</p>
</div>
);
}`,
},
{
language: "tsx",
filename: "MyComponent.tsx",
code: `function MyComponent(props: { name: string }) { // [!code highlight]
return (
<div>
<h1>Hello, {props.name}!</h1> // [!code highlight]
<p>This is an example React component.</p>
</div>
);
}`,
},
];
const Example = () => (
<CodeBlock data={code} defaultValue={code[0].language}>
<CodeBlockHeader>
<CodeBlockFiles>
{(item) => (
<CodeBlockFilename key={item.language} value={item.language}>
{item.filename}
</CodeBlockFilename>
)}
</CodeBlockFiles>
<CodeBlockSelect>
<CodeBlockSelectTrigger>
<CodeBlockSelectValue />
</CodeBlockSelectTrigger>
<CodeBlockSelectContent>
{(item) => (
<CodeBlockSelectItem key={item.language} value={item.language}>
{item.language}
</CodeBlockSelectItem>
)}
</CodeBlockSelectContent>
</CodeBlockSelect>
<CodeBlockCopyButton
onCopy={() => console.log("Copied code to clipboard")}
onError={() => console.error("Failed to copy code to clipboard")}
/>
</CodeBlockHeader>
<CodeBlockBody>
{(item) => (
<CodeBlockItem key={item.language} value={item.language}>
<CodeBlockContent language={item.language as BundledLanguage}>
{item.code}
</CodeBlockContent>
</CodeBlockItem>
)}
</CodeBlockBody>
</CodeBlock>
);
export default Example;
Highlighted words
MyComponent.jsx
jsx
function MyComponent(props) { return ( <div> // [!code word:props.name] <h1>Hello, {props.name}!</h1> <p>This is an example React component.</p> </div> );}"use client";
import type { BundledLanguage } from "../../../../../packages/code-block";
import {
CodeBlock,
CodeBlockBody,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockFilename,
CodeBlockFiles,
CodeBlockHeader,
CodeBlockItem,
CodeBlockSelect,
CodeBlockSelectContent,
CodeBlockSelectItem,
CodeBlockSelectTrigger,
CodeBlockSelectValue,
} from "../../../../../packages/code-block";
const code = [
{
language: "jsx",
filename: "MyComponent.jsx",
code: `function MyComponent(props) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
{
language: "tsx",
filename: "MyComponent.tsx",
code: `function MyComponent(props: { name: string }) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
];
const Example = () => (
<CodeBlock data={code} defaultValue={code[0].language}>
<CodeBlockHeader>
<CodeBlockFiles>
{(item) => (
<CodeBlockFilename key={item.language} value={item.language}>
{item.filename}
</CodeBlockFilename>
)}
</CodeBlockFiles>
<CodeBlockSelect>
<CodeBlockSelectTrigger>
<CodeBlockSelectValue />
</CodeBlockSelectTrigger>
<CodeBlockSelectContent>
{(item) => (
<CodeBlockSelectItem key={item.language} value={item.language}>
{item.language}
</CodeBlockSelectItem>
)}
</CodeBlockSelectContent>
</CodeBlockSelect>
<CodeBlockCopyButton
onCopy={() => console.log("Copied code to clipboard")}
onError={() => console.error("Failed to copy code to clipboard")}
/>
</CodeBlockHeader>
<CodeBlockBody>
{(item) => (
<CodeBlockItem key={item.language} value={item.language}>
<CodeBlockContent language={item.language as BundledLanguage}>
{item.code}
</CodeBlockContent>
</CodeBlockItem>
)}
</CodeBlockBody>
</CodeBlock>
);
export default Example;
Diff
utils.js
js
function calculateTotal(items) { let total = 0; for (let i = 0; i < items.length; i++) { total += items[i].price * items[i].quantity; // [!code --] const itemTotal = items[i].price * items[i].quantity; // [!code ++] total += itemTotal; // [!code ++] } return total;}"use client";
import type { BundledLanguage } from "../../../../../packages/code-block";
import {
CodeBlock,
CodeBlockBody,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockFilename,
CodeBlockFiles,
CodeBlockHeader,
CodeBlockItem,
CodeBlockSelect,
CodeBlockSelectContent,
CodeBlockSelectItem,
CodeBlockSelectTrigger,
CodeBlockSelectValue,
} from "../../../../../packages/code-block";
const code = [
{
language: "js",
filename: "utils.js",
code: `function calculateTotal(items) {
let total = 0;
for (let i = 0; i < items.length; i++) {
total += items[i].price * items[i].quantity; // [!code --]
const itemTotal = items[i].price * items[i].quantity; // [!code ++]
total += itemTotal; // [!code ++]
}
return total;
}`,
},
{
language: "ts",
filename: "utils.ts",
code: `interface Item {
price: number;
quantity: number;
}
function calculateTotal(items: Item[]): number {
let total = 0;
for (let i = 0; i < items.length; i++) {
total += items[i].price * items[i].quantity; // [!code --]
const itemTotal = items[i].price * items[i].quantity; // [!code ++]
total += itemTotal; // [!code ++]
}
return total;
}`,
},
];
const Example = () => (
<CodeBlock data={code} defaultValue={code[0].language}>
<CodeBlockHeader>
<CodeBlockFiles>
{(item) => (
<CodeBlockFilename key={item.language} value={item.language}>
{item.filename}
</CodeBlockFilename>
)}
</CodeBlockFiles>
<CodeBlockSelect>
<CodeBlockSelectTrigger>
<CodeBlockSelectValue />
</CodeBlockSelectTrigger>
<CodeBlockSelectContent>
{(item) => (
<CodeBlockSelectItem key={item.language} value={item.language}>
{item.language}
</CodeBlockSelectItem>
)}
</CodeBlockSelectContent>
</CodeBlockSelect>
<CodeBlockCopyButton
onCopy={() => console.log("Copied code to clipboard")}
onError={() => console.error("Failed to copy code to clipboard")}
/>
</CodeBlockHeader>
<CodeBlockBody>
{(item) => (
<CodeBlockItem key={item.language} value={item.language}>
<CodeBlockContent language={item.language as BundledLanguage}>
{item.code}
</CodeBlockContent>
</CodeBlockItem>
)}
</CodeBlockBody>
</CodeBlock>
);
export default Example;
Focus
utils.js
js
function calculateDiscount(price, percentage) { const discount = price * (percentage / 100); // [!code focus] return price - discount;}// Example usageconst finalPrice = calculateDiscount(100, 20);console.log(finalPrice); // 80"use client";
import type { BundledLanguage } from "../../../../../packages/code-block";
import {
CodeBlock,
CodeBlockBody,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockFilename,
CodeBlockFiles,
CodeBlockHeader,
CodeBlockItem,
CodeBlockSelect,
CodeBlockSelectContent,
CodeBlockSelectItem,
CodeBlockSelectTrigger,
CodeBlockSelectValue,
} from "../../../../../packages/code-block";
const code = [
{
language: "js",
filename: "utils.js",
code: `function calculateDiscount(price, percentage) {
const discount = price * (percentage / 100); // [!code focus]
return price - discount;
}
// Example usage
const finalPrice = calculateDiscount(100, 20);
console.log(finalPrice); // 80`,
},
{
language: "ts",
filename: "utils.ts",
code: `function calculateDiscount(price: number, percentage: number): number {
const discount = price * (percentage / 100); // [!code focus]
return price - discount;
}
// Example usage
const finalPrice: number = calculateDiscount(100, 20);
console.log(finalPrice); // 80`,
},
];
const Example = () => (
<CodeBlock data={code} defaultValue={code[0].language}>
<CodeBlockHeader>
<CodeBlockFiles>
{(item) => (
<CodeBlockFilename key={item.language} value={item.language}>
{item.filename}
</CodeBlockFilename>
)}
</CodeBlockFiles>
<CodeBlockSelect>
<CodeBlockSelectTrigger>
<CodeBlockSelectValue />
</CodeBlockSelectTrigger>
<CodeBlockSelectContent>
{(item) => (
<CodeBlockSelectItem key={item.language} value={item.language}>
{item.language}
</CodeBlockSelectItem>
)}
</CodeBlockSelectContent>
</CodeBlockSelect>
<CodeBlockCopyButton
onCopy={() => console.log("Copied code to clipboard")}
onError={() => console.error("Failed to copy code to clipboard")}
/>
</CodeBlockHeader>
<CodeBlockBody>
{(item) => (
<CodeBlockItem key={item.language} value={item.language}>
<CodeBlockContent language={item.language as BundledLanguage}>
{item.code}
</CodeBlockContent>
</CodeBlockItem>
)}
</CodeBlockBody>
</CodeBlock>
);
export default Example;
Hidden line numbers
MyComponent.jsx
jsx
function MyComponent(props) { return ( <div> <h1>Hello, {props.name}!</h1> <p>This is an example React component.</p> </div> );}"use client";
import type { BundledLanguage } from "../../../../../packages/code-block";
import {
CodeBlock,
CodeBlockBody,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockFilename,
CodeBlockFiles,
CodeBlockHeader,
CodeBlockItem,
CodeBlockSelect,
CodeBlockSelectContent,
CodeBlockSelectItem,
CodeBlockSelectTrigger,
CodeBlockSelectValue,
} from "../../../../../packages/code-block";
const code = [
{
language: "jsx",
filename: "MyComponent.jsx",
code: `function MyComponent(props) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
{
language: "tsx",
filename: "MyComponent.tsx",
code: `function MyComponent(props: { name: string }) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
];
const Example = () => (
<CodeBlock data={code} defaultValue={code[0].language}>
<CodeBlockHeader>
<CodeBlockFiles>
{(item) => (
<CodeBlockFilename key={item.language} value={item.language}>
{item.filename}
</CodeBlockFilename>
)}
</CodeBlockFiles>
<CodeBlockSelect>
<CodeBlockSelectTrigger>
<CodeBlockSelectValue />
</CodeBlockSelectTrigger>
<CodeBlockSelectContent>
{(item) => (
<CodeBlockSelectItem key={item.language} value={item.language}>
{item.language}
</CodeBlockSelectItem>
)}
</CodeBlockSelectContent>
</CodeBlockSelect>
<CodeBlockCopyButton
onCopy={() => console.log("Copied code to clipboard")}
onError={() => console.error("Failed to copy code to clipboard")}
/>
</CodeBlockHeader>
<CodeBlockBody>
{(item) => (
<CodeBlockItem
key={item.language}
lineNumbers={false}
value={item.language}
>
<CodeBlockContent language={item.language as BundledLanguage}>
{item.code}
</CodeBlockContent>
</CodeBlockItem>
)}
</CodeBlockBody>
</CodeBlock>
);
export default Example;
No syntax highlighting
MyComponent.jsx
jsx
function MyComponent(props) { return ( <div> <h1>Hello, {props.name}!</h1> <p>This is an example React component.</p> </div> );}"use client";
import type { BundledLanguage } from "../../../../../packages/code-block";
import {
CodeBlock,
CodeBlockBody,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockFilename,
CodeBlockFiles,
CodeBlockHeader,
CodeBlockItem,
CodeBlockSelect,
CodeBlockSelectContent,
CodeBlockSelectItem,
CodeBlockSelectTrigger,
CodeBlockSelectValue,
} from "../../../../../packages/code-block";
const code = [
{
language: "jsx",
filename: "MyComponent.jsx",
code: `function MyComponent(props) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
{
language: "tsx",
filename: "MyComponent.tsx",
code: `function MyComponent(props: { name: string }) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
];
const Example = () => (
<CodeBlock data={code} defaultValue={code[0].language}>
<CodeBlockHeader>
<CodeBlockFiles>
{(item) => (
<CodeBlockFilename key={item.language} value={item.language}>
{item.filename}
</CodeBlockFilename>
)}
</CodeBlockFiles>
<CodeBlockSelect>
<CodeBlockSelectTrigger>
<CodeBlockSelectValue />
</CodeBlockSelectTrigger>
<CodeBlockSelectContent>
{(item) => (
<CodeBlockSelectItem key={item.language} value={item.language}>
{item.language}
</CodeBlockSelectItem>
)}
</CodeBlockSelectContent>
</CodeBlockSelect>
<CodeBlockCopyButton
onCopy={() => console.log("Copied code to clipboard")}
onError={() => console.error("Failed to copy code to clipboard")}
/>
</CodeBlockHeader>
<CodeBlockBody>
{(item) => (
<CodeBlockItem key={item.language} value={item.language}>
<CodeBlockContent
language={item.language as BundledLanguage}
syntaxHighlighting={false}
>
{item.code}
</CodeBlockContent>
</CodeBlockItem>
)}
</CodeBlockBody>
</CodeBlock>
);
export default Example;
Custom Theme
MyComponent.jsx
jsx
function MyComponent(props) { return ( <div> <h1>Hello, {props.name}!</h1> <p>This is an example React component.</p> </div> );}"use client";
import type { BundledLanguage } from "../../../../../packages/code-block";
import {
CodeBlock,
CodeBlockBody,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockFilename,
CodeBlockFiles,
CodeBlockHeader,
CodeBlockItem,
CodeBlockSelect,
CodeBlockSelectContent,
CodeBlockSelectItem,
CodeBlockSelectTrigger,
CodeBlockSelectValue,
} from "../../../../../packages/code-block";
const code = [
{
language: "jsx",
filename: "MyComponent.jsx",
code: `function MyComponent(props) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
{
language: "tsx",
filename: "MyComponent.tsx",
code: `function MyComponent(props: { name: string }) {
return (
<div>
<h1>Hello, {props.name}!</h1>
<p>This is an example React component.</p>
</div>
);
}`,
},
];
const Example = () => (
<CodeBlock data={code} defaultValue={code[0].language}>
<CodeBlockHeader>
<CodeBlockFiles>
{(item) => (
<CodeBlockFilename key={item.language} value={item.language}>
{item.filename}
</CodeBlockFilename>
)}
</CodeBlockFiles>
<CodeBlockSelect>
<CodeBlockSelectTrigger>
<CodeBlockSelectValue />
</CodeBlockSelectTrigger>
<CodeBlockSelectContent>
{(item) => (
<CodeBlockSelectItem key={item.language} value={item.language}>
{item.language}
</CodeBlockSelectItem>
)}
</CodeBlockSelectContent>
</CodeBlockSelect>
<CodeBlockCopyButton
onCopy={() => console.log("Copied code to clipboard")}
onError={() => console.error("Failed to copy code to clipboard")}
/>
</CodeBlockHeader>
<CodeBlockBody>
{(item) => (
<CodeBlockItem key={item.language} value={item.language}>
<CodeBlockContent
language={item.language as BundledLanguage}
themes={{
light: "vitesse-light",
dark: "vitesse-dark",
}}
>
{item.code}
</CodeBlockContent>
</CodeBlockItem>
)}
</CodeBlockBody>
</CodeBlock>
);
export default Example;
Notes
React Server Component
You can use the RSC version of the Code Block component by importing the CodeBlockContent component from code-block/server i.e.
import {
CodeBlock,
CodeBlockHeader,
// ... the rest of the components
CodeBlockBody,
// CodeBlockContent,
} from '@/components/love-ui/code-block';
import { CodeBlockContent } from '@/components/love-ui/code-block/server';