StorybookGitHub

Use Highlighted Code

Streams Shiki-highlighted React nodes for a code string and language, including incremental updates.

const greeting = "hello";console.log(greeting);

Installation

npx shadcn@latest add https://reactdocs.canceydejean.dev/r/use-highlighted-code.json

Usage

Pass the source string and a language key supported by code-block's highlighter. While the highlighter loads, plain text lines are returned; afterward you get one React node per line.

import {
  CodeBlock,
  CodeBlockContent,
  CodeBlockLine,
} from "@/components/ui/code-block/code-block";
import { useHighlightedCode } from "@/hooks/use-highlighted-code";

const source = `const greeting = "hello";
console.log(greeting);`;

export function Example() {
  const lines = useHighlightedCode(source, "typescript");

  return (
    <CodeBlock>
      <CodeBlockContent>
        {lines.map((line, index) => (
          <CodeBlockLine key={index}>{line}</CodeBlockLine>
        ))}
      </CodeBlockContent>
    </CodeBlock>
  );
}