# CodeViewer

> Source code with syntax colors from your theme, line numbers, highlighted lines, a wrap toggle and copy.

Status: beta. Category: Viewers.

```ts
import { CodeViewer } from '@glyphui/viewers/code'
```

Exports: `CodeViewer`, `codeViewerStyles`, `parseLineRanges`, `highlightCode`, `languageFromFileName`, `resolveLanguage`.

## Notes

- Install the optional peer dependency first: npm install shiki.
- Colors are CSS variables (--shiki-token-*) mapped to your theme in @glyphui/viewers/styles.css, so code follows light and dark mode and every preset.
- Grammars load on first use, one language at a time, with Shiki’s JavaScript engine (no WebAssembly).

## CodeViewer props

| Prop | Type | Default | Description |
|---|---|---|---|
| `bordered` | `boolean` |  | Draw the viewer as a bordered card. Default true. |
| `className` | `string` |  |  |
| `code` | `string` |  | The code to show. |
| `fileName` | `string` |  | Shown in the header; also used to guess the language. |
| `height` | `string \| number` |  | Height of the viewer; content scrolls inside. Grows with the code by default. |
| `hideHeader` | `boolean` |  | Hide the header bar (file name, language, wrap and copy). |
| `highlightLines` | `string \| readonly (number \| readonly [number, number])[]` |  | Line numbers to highlight, e.g. `[3, [6, 9]]` or `"3,6-9"`. |
| `language` | `string` |  | Language id or file extension: `tsx`, `python`, `sql`… Guessed from `fileName` when unset. |
| `lineMarks` | `Readonly<Record<number, LineMark>>` |  | Marks for individual lines, by line number. Wins over `highlightLines`. |
| `lineNumbers` | `boolean` |  | Show line numbers. Default true. |
| `source` | `ViewerSource` |  | A source file to load instead of `code`: a URL, a File/Blob, or bytes. |
| `startLine` | `number` |  | Number of the first line. Default 1. |
| `wrap` | `boolean` |  | Wrap long lines instead of scrolling sideways. Users can toggle it. Default false. |

## Example: A source file

The language comes from the file name; lines 14–16 are highlighted.

```tsx
import { CodeViewer } from '@glyphui/viewers/code'
import { fixture } from '../../../lib/fixtures'

export default function Example() {
  return <CodeViewer source={fixture('reminders.ts')} highlightLines="14-16" className="w-full" />
}
```

## Example: A snippet

Pass code as a string, with wrapping on.

```tsx
import { CodeViewer } from '@glyphui/viewers/code'

const code = `def monthly_recurring_revenue(subscriptions):
    """Sum active subscriptions, normalizing annual plans to a monthly amount."""
    total = 0
    for sub in subscriptions:
        if sub.status != "active":
            continue
        total += sub.amount / 12 if sub.interval == "year" else sub.amount
    return round(total, 2)`

export default function Example() {
  return <CodeViewer code={code} language="python" fileName="metrics.py" wrap className="w-full" />
}
```

## Example: Without the header

Just the code, for embedding in your own layout.

```tsx
import { CodeViewer } from '@glyphui/viewers/code'

export default function Example() {
  return (
    <CodeViewer
      code={
        "SELECT customer, SUM(total) AS revenue\nFROM invoices\nWHERE status = 'paid'\nGROUP BY customer\nORDER BY revenue DESC;"
      }
      language="sql"
      hideHeader
      className="w-full max-w-xl"
    />
  )
}
```

Live page: https://glyphui.xyz/components/code-viewer
