# Use Highlighted Code

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

## Installation

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

[Registry JSON](https://reactdocs.canceydejean.dev/r/use-highlighted-code.json)

## Preview

```tsx
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 Preview() {
  const lines = useHighlightedCode(source, "typescript");

  return (
    <CodeBlock className="max-w-xl">
      <CodeBlockContent>
        {lines.map((line, index) => (
          
          <CodeBlockLine key={index}>{line}</CodeBlockLine>
        ))}
      </CodeBlockContent>
    </CodeBlock>
  );
}
```


## Source

### hooks/use-highlighted-code.ts

```ts
import * as React from "react";

import { loadHighlighter } from "@/components/ui/code-block/highlighter";

export function useHighlightedCode(value: string, language: string) {
  // A trailing newline would render as an extra empty line.
  const code = value.endsWith("\n") ? value.slice(0, -1) : value;
  const [loaded, setLoaded] = React.useState<{
    language: string;
    highlighter: Awaited<ReturnType<typeof loadHighlighter>>;
  } | null>(null);
  const highlighter = loaded?.language === language ? loaded.highlighter : null;

  React.useEffect(() => {
    let cancelled = false;

    loadHighlighter(language)
      .then((next) => {
        if (!cancelled) {
          setLoaded({ language, highlighter: next });
        }
      })
      .catch(() => {});

    return () => {
      cancelled = true;
    };
  }, [language]);

  // The complete lines, highlighted. While the code streams, only the new
  // lines are tokenized, starting from the grammar state of the last one.
  const [done, setDone] = React.useState<{
    highlighter: typeof highlighter;
    text: string;
    lines: React.ReactNode[];
    state?: Parameters<NonNullable<typeof highlighter>["highlight"]>[1];
  }>({ highlighter: null, text: "", lines: [] });

  let next = done;

  if (next.highlighter !== highlighter || !code.startsWith(next.text)) {
    next = { highlighter, text: "", lines: [] };
  }

  const end = code.lastIndexOf("\n") + 1;

  if (highlighter && end > next.text.length) {
    const result = highlighter.highlight(code.slice(next.text.length, end - 1), next.state);

    next = {
      highlighter,
      text: code.slice(0, end),
      lines: [...next.lines, ...result.lines],
      state: result.state,
    };
  }

  if (next !== done) {
    setDone(next);
  }

  // The last line is still being written, so it is tokenized on every render.
  const tail = code.slice(end);

  return React.useMemo(() => {
    if (!highlighter) {
      return code.split("\n");
    }

    return [...next.lines, highlighter.highlight(tail, next.state).lines[0]];
  }, [code, highlighter, next, tail]);
}
```



## 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.

```tsx
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>
  );
}
```

