Skip to main content

Installation

    Usage

    Display code with beautiful syntax highlighting powered by Shiki.
    import { CodeBlock } from "@/components/ui/code-block";
    
    export default function Example() {
      const code = `function hello(name: string) {
      console.log(\`Hello, \${name}!\`);
      return true;
    }`;
    
      return (
        <CodeBlock language="typescript">
          {code}
        </CodeBlock>
      );
    }
    

    Features

    Syntax Highlighting

    Shiki provides beautiful, accurate syntax highlighting using VS Code’s grammar engine:
    <CodeBlock language="python">
    {`def fibonacci(n):
        if n <= 1:
            return n
        return fibonacci(n-1) + fibonacci(n-2)
    
    print(fibonacci(10))`}
    </CodeBlock>
    

    Line Numbers

    Enable line numbers for easier reference:
    <CodeBlock language="javascript" showLineNumbers>
    {`const app = express();
    app.use(express.json());
    app.listen(3000);`}
    </CodeBlock>
    

    File Names

    Show the filename in the header:
    <CodeBlock language="tsx" filename="app/page.tsx">
    {`export default function Page() {
      return <div>Hello World</div>;
    }`}
    </CodeBlock>
    

    Language Icons

    Icons automatically appear for recognized languages using Devicon:
    <CodeBlock language="rust">       {/* Shows Rust icon */}
    <CodeBlock language="go">         {/* Shows Go icon */}
    <CodeBlock language="python">     {/* Shows Python icon */}
    <CodeBlock language="typescript"> {/* Shows TypeScript icon */}
    

    Copy & Download

    Built-in copy and download buttons:
    • Click the copy icon to copy code to clipboard
    • Click the download icon to save as a file
    • Copy button shows a checkmark on success

    Supported Languages

    The component supports 40+ languages including:
    • Web: JavaScript, TypeScript, HTML, CSS, JSX, TSX
    • Backend: Python, Java, Go, Rust, PHP, Ruby, C, C++, C#
    • Data: JSON, XML, YAML, SQL
    • Shell: Bash, Shell
    • Other: Markdown, Kotlin, Swift, Dockerfile

    Props

    Styling

    The component uses Shiki’s GitHub themes:
    • Light mode: github-light
    • Dark mode: github-dark
    Both themes automatically adapt based on your color scheme.

    Accessibility

    • Copy and download buttons have proper aria-label attributes
    • Language displayed as text when icon unavailable
    • Keyboard accessible controls
    • Code content is selectable and copyable
    • Pre-formatted text maintains proper semantic structure

    Build docs developers (and LLMs) love