Code Block

Provides syntax highlighting, line numbers, and copy to clipboard functionality for code blocks.

Installation

npx love-ui@latest add code-block

Usage

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

Highlighted lines

Highlighted words

Diff

Focus

Hidden line numbers

No syntax highlighting

Custom Theme

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';