# Code View

> Show a file's code or a diff of it, with line numbers and a copy button.

Source: https://www.honestui.com/docs/ai/code-view

```tsx
import { CodeView } from "@/components/ai/code-view"

const sampleCode = `function exportTotal(rows: Row[]) {
  const total = rows.reduce((sum, row) => sum + row.amount, 0)
  return roundTotal(total)
}`

export function CodeViewDemo() {
  return (
    <div className="w-full max-w-lg min-w-0">
      <CodeView className="[&_pre]:no-scrollbar" filename="export.ts" code={sampleCode} />
    </div>
  )
}

```

## Overview [#overview]

Code View shows code an agent wrote or changed, in a compact panel. Pass `code` for a numbered listing, or `diff` for a unified diff where each changed line carries a sign and a tint.

Lines are colored with [Honest Prose Syntax](/docs/ai/prose-syntax), in the language of the file name's extension. Pass `language` when the name has no extension, or `"plaintext"` for no colors. If your product already has a highlighter, pass `renderLine` to color each line with yours instead; a second highlighter would disagree with it.

Code View shares its frame with [Prose Code Block](/docs/ai/prose-code-block): a shell with the header, around a panel with the code.

## Anatomy [#anatomy]

<Anatomy title="CodeView">
  <AnatomyItem name="Shell" description="The frame, with the file name, the diff counts, your actions, and Copy" />

  <AnatomyItem name="Panel" description="Numbered lines in a scrolling block with a hairline edge" />

  <AnatomyItem name="Sign" description="A plus or minus before each line of a diff" />
</Anatomy>

Installing `code-view` brings [Button](/docs/components/button) and [Honest Prose Syntax](/docs/ai/prose-syntax)'s React hook and theme along as registry dependencies. Import the installed `styles/prose-syntax-theme.css` once in your application root so the colors apply.

## Behavior [#behavior]

### Listing and diff [#listing-and-diff]

A listing numbers every line. In a diff, removed lines take no number, so the numbers match the new file. A changed line has a light tint, and its sign sits in a stronger column at the start of the line.

### Copy [#copy]

Copy puts the code on the clipboard. For a diff, it copies the new file's lines and leaves removed lines out. Its label reads "Copied", with a check, for a moment. If the browser refuses clipboard access, the block says "Copy failed" instead of pretending it worked.

### Actions [#actions]

`actions` puts your own controls in the header, before Copy. Pass a button for each action your application handles, such as downloading the file. An embedded Code View has no header, so it shows none.

### Long lines [#long-lines]

Lines do not wrap. The block scrolls sideways. A thin scrollbar and a fade at the end edge show there is more to see, and the fade clears when you reach the end.

### Highlighting [#highlighting]

The source stays visible while it is highlighted, so nothing shifts or flashes. In a diff, the removed and added lines are highlighted together, so each keeps its colors.

### Embedded [#embedded]

`embedded` drops the frame and the header, leaving the numbered lines. Use it inside a surface that already names the file, such as an [Artifact Panel](/docs/ai/artifact-panel). The header's Copy button and a diff's counts go with it, so supply Copy from the surrounding surface. The code region is still named by `filename`.

## Accessibility [#accessibility]

The block is a figure with the file name as its caption. When its lines are wider than the block, the code region can take focus, so a keyboard user can scroll them, and it is named by the file. A listing that fits adds no tab stop.

Copy announces "Copied" or "Copy failed" through a polite status. Diff counts are read as "2 added" and "1 removed". A changed line is marked by its sign as well as its tint, and keeps the primary text color.

| Key                                  | Action                                                         |
| ------------------------------------ | -------------------------------------------------------------- |
| <kbd>Tab</kbd>                       | Move to your actions, then Copy, then the code when it scrolls |
| Arrow keys                           | Scroll the focused code                                        |
| <kbd>Enter</kbd> or <kbd>Space</kbd> | Copy                                                           |

## Installation [#installation]


  

  
    <CliBlock commands="[&#x22;@honestai/code-view&#x22;]" />
  

  
    
      
        Add the components it is built from.
      

      <CliBlock commands="[&#x22;button&#x22;]" />

      
        Add the highlighter it uses.
      

      <CliBlock commands="[&#x22;@honestai/prose-syntax-react&#x22;]" />

      
        Copy and paste this file into your project.
      

      ### components/ai/code-view.tsx

```tsx
"use client"

import * as React from "react"
import { Check as CheckIcon, Copy as CopyIcon } from "honestui/icons"

import { cn } from "@/lib/utils"
import { Button } from "@/components/honest-ui/ui/button"
import { SyntaxLine, useSyntax } from "@/lib/prose-syntax-react"

export type CodeDiffLine = {
  text: string
  /** Defaults to "context". */
  kind?: "context" | "added" | "removed"
}

export type CodeViewLabels = {
  copy: string
  copied: string
  copyFailed: string
  added: (count: number) => string
  removed: (count: number) => string
}

export const defaultCodeViewLabels: CodeViewLabels = {
  copy: "Copy",
  copied: "Copied",
  copyFailed: "Copy failed",
  added: (count) => `${count} added`,
  removed: (count) => `${count} removed`,
}

const COPIED_MILLISECONDS = 1500

// A half-strength tint marks the line, and a full-strength sign column carries
// the change, so the code stays the brightest thing on the line in both themes.
const lineTone: Record<NonNullable<CodeDiffLine["kind"]>, string> = {
  context: "",
  added: "bg-[color-mix(in_oklch,var(--hui-color-background-success-primary)_50%,transparent)]",
  removed: "bg-[color-mix(in_oklch,var(--hui-color-background-danger-primary)_50%,transparent)]",
}

const signTone: Record<NonNullable<CodeDiffLine["kind"]>, string> = {
  context: "",
  added: "bg-[var(--hui-color-background-success-primary)] text-[var(--hui-color-foreground-success-primary-hover)]",
  removed: "bg-[var(--hui-color-background-danger-primary)] text-[var(--hui-color-foreground-danger-primary)]",
}

const lineSign: Record<NonNullable<CodeDiffLine["kind"]>, string> = {
  context: " ",
  added: "+",
  removed: "−",
}

export type CodeViewProps = Omit<React.ComponentProps<"figure">, "children" | "onCopy"> & {
  /** The file name or a short title, shown in the header. */
  filename: string
  /** The source to list, with line numbers. Ignored when `diff` is passed. */
  code?: string
  /** A unified diff. Each changed line is marked with a sign and a tint. */
  diff?: CodeDiffLine[]
  /**
   * The language to highlight with Honest Prose Syntax, such as `"ts"`.
   * Defaults to the file name's extension. Pass `"plaintext"` for no colors.
   */
  language?: string
  /** Colors one line with your own highlighter. `index` counts the listed lines from zero. It replaces the built-in highlighting. */
  renderLine?: (text: string, index: number) => React.ReactNode
  /** Controls for the header, placed before Copy. */
  actions?: React.ReactNode
  /** Runs after the text reaches the clipboard. */
  onCopy?: (text: string) => void
  /** Drops the frame and the header, for a surface that supplies its own, such as an Artifact Panel. */
  embedded?: boolean
  labels?: Partial<CodeViewLabels>
}

/** The extension after the last dot, or nothing for names such as "Terminal". */
function languageFromFilename(filename: string) {
  const dot = filename.lastIndexOf(".")
  return dot > 0 && dot < filename.length - 1 ? filename.slice(dot + 1).toLowerCase() : undefined
}

/**
 * A file's code, or a diff of it, with line numbers and a copy button. Lines
 * are highlighted with Honest Prose Syntax unless `renderLine` supplies colors.
 */
function CodeView({
  filename,
  code = "",
  diff,
  language: languageProp,
  renderLine,
  actions,
  onCopy,
  embedded = false,
  labels: labelsProp,
  className,
  ...props
}: CodeViewProps) {
  const labels = React.useMemo(
    () => ({ ...defaultCodeViewLabels, ...labelsProp }),
    [labelsProp]
  )
  const [copyState, setCopyState] = React.useState<"idle" | "copied" | "failed">("idle")
  const resetTimer = React.useRef<number | undefined>(undefined)
  React.useEffect(() => () => window.clearTimeout(resetTimer.current), [])

  const lines: CodeDiffLine[] = diff ?? code.split("\n").map((text) => ({ text }))
  // Copy what the file would contain: a diff's removed lines are left out.
  const copyText = diff
    ? diff
        .filter((line) => line.kind !== "removed")
        .map((line) => line.text)
        .join("\n")
    : code
  const language = languageProp ?? languageFromFilename(filename)
  // A diff is highlighted as one listing, removed lines included, so each line keeps its colors.
  const syntax = useSyntax(
    diff ? diff.map((line) => line.text).join("\n") : code,
    language,
    !renderLine
  )
  const addedCount = diff?.filter((line) => line.kind === "added").length ?? 0
  const removedCount = diff?.filter((line) => line.kind === "removed").length ?? 0

  const copy = async () => {
    try {
      await navigator.clipboard.writeText(copyText)
      setCopyState("copied")
      onCopy?.(copyText)
    } catch {
      setCopyState("failed")
    }
    window.clearTimeout(resetTimer.current)
    resetTimer.current = window.setTimeout(() => setCopyState("idle"), COPIED_MILLISECONDS)
  }

  // Removed lines are not in the new file, so they take no number.
  const lineNumbers: (number | null)[] = []
  for (const line of lines) {
    const previous = lineNumbers.findLast((value) => value !== null) ?? 0
    lineNumbers.push(line.kind === "removed" ? null : previous + 1)
  }

  const [preRef, overflowing] = useOverflowing<HTMLPreElement>()

  const listing = (
    // A thin scrollbar and a fade at the end edge show there is more to scroll.
    // The fade follows the block's scroll position and clears at the end;
    // where scroll-driven animation is unsupported, or nothing overflows, it
    // stays hidden.
    <div className="relative min-w-0 [timeline-scope:--code-view-scroll]">
      {/* A tab stop only while it overflows, so a keyboard user can scroll long lines. */}
      <pre
        ref={preRef}
        tabIndex={overflowing ? 0 : undefined}
        aria-label={filename}
        className="m-0 overflow-x-auto py-[var(--hui-space-4)] font-mono outline-none [font-variant-ligatures:none] [font-size:var(--hui-font-size-mono-small)] [letter-spacing:var(--hui-letter-spacing-mono-small)] [line-height:var(--hui-line-height-mono-regular)] [scroll-timeline:--code-view-scroll_inline] supports-[selector(::-webkit-scrollbar)]:[scrollbar-color:auto]! supports-[selector(::-webkit-scrollbar)]:[scrollbar-width:auto]! [&::-webkit-scrollbar]:h-1.5 [&::-webkit-scrollbar-track]:mx-[var(--hui-space-4)] [&::-webkit-scrollbar-track]:mb-[var(--hui-space-1)] [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-[var(--hui-color-overlay-base-a5)] focus-visible:[outline:var(--hui-focus-ring)] focus-visible:outline-offset-[var(--hui-focus-ring-offset-inset)]"
      >
        <code
          className={cn(
            "block w-max min-w-full",
            // The theme colors the tokens; the surface and line height stay the block's own.
            !renderLine && "honest-syntax overflow-visible [--prose-syntax-background:transparent] [--prose-syntax-leading:var(--hui-line-height-mono-regular)]"
          )}
        >
          {lines.map((line, index) => {
            const kind = line.kind ?? "context"
            const tokens = syntax?.lines[index]
            return (
              <span key={index} data-kind={kind} className={cn("flex", lineTone[kind])}>
                <span
                  aria-hidden="true"
                  className="w-11 shrink-0 select-none pe-[var(--hui-space-4)] text-end tabular-nums text-[var(--hui-color-foreground-base-secondary)]"
                >
                  {lineNumbers[index] ?? ""}
                </span>
                {diff && (
                  <span className={cn("me-[var(--hui-space-3)] w-[3ch] shrink-0 select-none text-center", signTone[kind])}>
                    {lineSign[kind]}
                  </span>
                )}
                <span className="pe-[var(--hui-space-6)] whitespace-pre">
                  {renderLine ? renderLine(line.text, index) : tokens ? <SyntaxLine tokens={tokens} /> : line.text}
                  {"\n"}
                </span>
              </span>
            )
          })}
        </code>
      </pre>
      <div
        aria-hidden="true"
        className="pointer-events-none absolute inset-y-0 end-0 w-[var(--hui-space-8)] bg-[linear-gradient(to_left,var(--hui-color-background-base-primary),transparent)] opacity-0 rtl:bg-[linear-gradient(to_right,var(--hui-color-background-base-primary),transparent)]"
        style={{ animation: "hui-scroll-cue linear both", animationTimeline: "--code-view-scroll" }}
      />
    </div>
  )

  if (embedded) {
    return (
      <figure
        data-slot="code-view"
        data-embedded=""
        // `not-prose` keeps a prose around this figure from restyling its code.
        className={cn("not-prose m-0 w-full min-w-0 text-[var(--hui-color-foreground-base-primary)]", className)}
        {...props}
      >
        {listing}
      </figure>
    )
  }

  return (
    <figure
      data-slot="code-view"
      className={cn(
        // One plate: a slim header over the code, split by a hairline.
        "not-prose m-0 w-full min-w-0 overflow-hidden rounded-[var(--hui-radius-3)] border-[0.5px] border-[var(--hui-color-border-base-primary)] bg-[var(--hui-color-background-base-primary)] text-[var(--hui-color-foreground-base-primary)] shadow-[var(--hui-shadow-feather)]",
        className
      )}
      {...props}
    >
      <figcaption className="flex min-h-[var(--hui-space-10)] min-w-0 items-center gap-[var(--hui-space-3)] border-b-[0.5px] border-[var(--hui-color-border-base-primary)] ps-[var(--hui-space-5)] pe-[var(--hui-space-3)]">
        <span className="min-w-0 truncate font-mono text-[var(--hui-color-foreground-base-secondary)] [font-size:var(--hui-font-size-mono-small)] [letter-spacing:var(--hui-letter-spacing-mono-small)] [line-height:var(--hui-line-height-mono-small)]">
          {filename}
        </span>
        {diff && (
          <span className="shrink-0 [font-size:var(--hui-font-size-small)] [letter-spacing:var(--hui-letter-spacing-small)] [line-height:var(--hui-line-height-small)]">
            <span className="text-[var(--hui-color-foreground-success-primary-hover)]">
              <span aria-hidden="true">+<span className="tabular-nums">{addedCount}</span></span>
              <span className="sr-only">{labels.added(addedCount)}</span>
            </span>{" "}
            <span className="text-[var(--hui-color-foreground-danger-primary)]">
              <span aria-hidden="true">−<span className="tabular-nums">{removedCount}</span></span>
              <span className="sr-only">{labels.removed(removedCount)}</span>
            </span>
          </span>
        )}
        <div className="ms-auto flex shrink-0 items-center gap-[var(--hui-space-2)]">
          {actions}
          {/* Icon only: the header already says what is copied. The name stays
              the same while the check shows; the status below announces it. */}
          <Button
            type="button"
            variant="ghost"
            size="icon-sm"
            aria-label={labels.copy}
            title={labels.copy}
            className="text-[var(--hui-color-foreground-base-secondary)] hover:text-[var(--hui-color-foreground-base-primary)] [&_svg]:size-3.5"
            onClick={() => void copy()}
          >
            {copyState === "copied" ? (
              <CheckIcon aria-hidden="true" className="text-[var(--hui-color-foreground-success-primary)]" />
            ) : (
              <CopyIcon aria-hidden="true" />
            )}
          </Button>
        </div>
        <span role="status" className="sr-only">
          {copyState === "copied" ? labels.copied : copyState === "failed" ? labels.copyFailed : ""}
        </span>
      </figcaption>
      <div className="min-w-0">{listing}</div>
      {copyState === "failed" && (
        <p className="m-0 border-t-[0.5px] border-[var(--hui-color-border-base-primary)] px-[var(--hui-space-5)] py-[var(--hui-space-3)] text-[var(--hui-color-foreground-danger-primary)] [font-size:var(--hui-font-size-small)] [letter-spacing:var(--hui-letter-spacing-small)] [line-height:var(--hui-line-height-small)]">
          {labels.copyFailed}
        </p>
      )}
    </figure>
  )
}

/**
 * Whether an element overflows, so it is a keyboard tab stop only while it has
 * something to scroll. The observer reports once on `observe`, then on resize.
 */
function useOverflowing<T extends HTMLElement>() {
  const ref = React.useRef<T>(null)
  const [overflowing, setOverflowing] = React.useState(false)
  React.useEffect(() => {
    const node = ref.current
    if (!node) return
    if (typeof ResizeObserver === "undefined") return
    const update = () => setOverflowing(node.scrollWidth > node.clientWidth + 1)
    const observer = new ResizeObserver(update)
    observer.observe(node)
    if (node.firstElementChild) observer.observe(node.firstElementChild)
    return () => observer.disconnect()
  }, [])
  return [ref, overflowing] as const
}

export { CodeView }

```

      
        Update the import paths to match your project setup.
      
    
  


## Usage [#usage]

```tsx
import { CodeView } from "@/components/ai/code-view"

<CodeView filename={file.path} code={file.contents} />

<CodeView filename={edit.path} diff={edit.lines} />

<CodeView filename="Terminal" language="bash" code={commands} />

<CodeView filename={file.path} code={file.contents} renderLine={(text) => highlight(text, "ts")} />
```

## Examples [#examples]

### Diff [#diff]

Two lines added and one removed. The numbers follow the new file.

```tsx
import { CodeView } from "@/components/ai/code-view"

export function CodeViewDiffExample() {
  return (
    <div className="w-full max-w-lg min-w-0">
      <CodeView
        filename="export.ts"
        diff={[
          { text: "function exportTotal(rows: Row[]) {" },
          { text: "  return rows.map(roundRow).reduce(sum, 0)", kind: "removed" },
          { text: "  const total = rows.reduce(sum, 0)", kind: "added" },
          { text: "  return roundTotal(total)", kind: "added" },
          { text: "}" },
        ]}
      />
    </div>
  )
}

```

## API reference [#api-reference]

### CodeView [#codeview]

Accepts native figure props beyond those listed.

| Prop         | Type                                         | Default                   | Description                                                                                                         |
| ------------ | -------------------------------------------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| `filename`   | string                                       | Required                  | The file name or a short title.                                                                                     |
| `code`       | string                                       | `""`                      | The source to list. Ignored when `diff` is passed.                                                                  |
| `diff`       | `CodeDiffLine[]`                             | None                      | A unified diff.                                                                                                     |
| `language`   | string                                       | The file name's extension | The language to highlight, such as `"ts"`. `"plaintext"` turns colors off.                                          |
| `renderLine` | `(text: string, index: number) => ReactNode` | None                      | Colors one line with your own highlighter, in place of the built-in one. `index` counts the listed lines from zero. |
| `actions`    | ReactNode                                    | None                      | Controls for the header, placed before Copy.                                                                        |
| `onCopy`     | `(text: string) => void`                     | None                      | Runs after the text reaches the clipboard.                                                                          |
| `embedded`   | boolean                                      | `false`                   | Drops the frame and the header, including Copy.                                                                     |
| `labels`     | `Partial<CodeViewLabels>`                    | English                   | Copy states and the names read for counts.                                                                          |

### Types [#types]

| Export         | Shape                                                                 |
| -------------- | --------------------------------------------------------------------- |
| `CodeDiffLine` | `text`, and optional `kind` of `"context"`, `"added"`, or `"removed"` |
