aiellie

Search

Find a component, template or page

GitHub
Components

Code Block

A block of code named by its file, with copy and download in its header, highlighted by Shiki in light and dark, with line numbers, marked lines and a caret while it streams.

app/api/chat/route.ts
import { streamText } from "ai"
export async function POST(request: Request) {  const { messages } = await request.json()
  const result = streamText({    model: "anthropic/claude-sonnet-4.5",    system: "You are a helpful assistant. Answer in one short paragraph.",    messages,  })
  return result.toUIMessageStreamResponse()}
Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/code-block"

shadcn needs the @aiellie registry in components.json first. npx aiellie add code-block adds it on its first run, then installs the same way.

Code Block shows code as a thing on the page rather than a thing inside prose. Its header names the file, with a mark for its language, and carries copy and download. The code itself is coloured by Shiki in GitHub's light and dark themes, can number its lines and mark the ones being talked about, and shows a caret after the last line while more is coming.

Shiki loads the first time a block needs colour, so a page that shows no code never downloads it. Until it arrives, and for any language it doesn't know, the code shows plain.

Getting started

  1. Lay out the block

    A block is its parts: a header with a title and actions, then the content. A title that's a file name or a language gets its mark on its own.

    components/snippet.tsx
    import {
      CodeBlock,
      CodeBlockActions,
      CodeBlockContent,
      CodeBlockCopy,
      CodeBlockHeader,
      CodeBlockTitle,
    } from "@/components/aiellie/code-block"
    
    export function Snippet({ file, code }: { file: string; code: string }) {
      return (
        <CodeBlock>
          <CodeBlockHeader>
            <CodeBlockTitle>{file}</CodeBlockTitle>
            <CodeBlockActions>
              <CodeBlockCopy code={code} />
            </CodeBlockActions>
          </CodeBlockHeader>
          <CodeBlockContent code={code} language="tsx" />
        </CodeBlock>
      )
    }
  2. Point at lines

    lineNumbers adds a gutter that stays put while long lines scroll, and is left out of a copy. highlightLines marks lines by number, counting from 1.

    <CodeBlockContent code={code} language="ts" lineNumbers highlightLines={[4, 5]} />

Anatomy

<figure data-slot="code-block">
  <figcaption data-slot="code-block-header">
    <span data-slot="code-block-title" /> {/* mark and name */}
    <div data-slot="code-block-actions">
      <button data-slot="code-block-download" />
      <button data-slot="code-block-copy" />
    </div>
  </figcaption>
  <pre data-slot="code-block-content" dir="ltr">
    <span data-slot="code-block-line" data-highlighted />
  </pre>
</figure>

The content is always left to right, even on a right-to-left page, since brackets in code reorder otherwise. It scrolls sideways rather than wrapping, and can be reached with Tab so the keyboard can scroll it too. Pass wrap for a narrow column.

useHighlightedLines and CodeTokens are exported too, so other code views, like code-diff and terminal, colour code the same way.

Examples

While it streams

Pass the code so far with streaming and a caret blinks after the last line. Lines already coloured keep their colours while the new ones are highlighted.

app/api/posts/route.ts

Download as a file

CodeBlockDownload saves the code under filename, so the name in the header is the name on disk.

<CodeBlockActions>
  <CodeBlockDownload code={code} filename="route.ts" />
  <CodeBlockCopy code={code} />
</CodeBlockActions>

A taller file

Cap the height with a class and the block scrolls both ways.

<CodeBlockContent code={code} language="tsx" className="max-h-80" />

API reference

CodeBlockContent

PropTypeDefaultDescription
codestringrequiredThe code to show.
languagestringAny language Shiki knows, like tsx or bash. Left out, the code stays plain.
lineNumbersbooleanfalseNumbers each line.
highlightLinesnumber[]Lines to mark, counting from 1.
wrapbooleanfalseWraps long lines instead of scrolling them.
streamingbooleanfalseShows a caret after the last line.

All other pre props are forwarded.

CodeBlockTitle

PropTypeDefaultDescription
iconReactNodeA mark of your own, or null for none. Left out, it's read off a title that's a file name or language.

All other span props are forwarded.

CodeBlockCopy

PropTypeDefaultDescription
codestringrequiredWhat goes on the clipboard.
tooltipstring"Copy"Its name, until it's copied.

All other TooltipIconButton props are forwarded.

CodeBlockDownload

PropTypeDefaultDescription
codestringrequiredWhat goes in the file.
filenamestring"snippet.txt"The name it's saved as.
tooltipstring"Download"Its name.

All other TooltipIconButton props are forwarded.

useHighlightedLines

ArgumentTypeDefaultDescription
codestringrequiredThe code to colour.
languagestringShiki's name for its language.

Returns each line's tokens, plain until Shiki has coloured them. Render a line with CodeTokens.