CodeViewer

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

Status: beta. Category: Viewers.

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

PropTypeDefaultDescription
borderedbooleanDraw the viewer as a bordered card. Default true.
classNamestring
codestringThe code to show.
fileNamestringShown in the header; also used to guess the language.
heightstring | numberHeight of the viewer; content scrolls inside. Grows with the code by default.
hideHeaderbooleanHide the header bar (file name, language, wrap and copy).
highlightLinesstring | readonly (number | readonly [number, number])[]Line numbers to highlight, e.g. [3, [6, 9]] or "3,6-9".
languagestringLanguage id or file extension: tsx, python, sql… Guessed from fileName when unset.
lineMarksReadonly<Record<number, LineMark>>Marks for individual lines, by line number. Wins over highlightLines.
lineNumbersbooleanShow line numbers. Default true.
sourceViewerSourceA source file to load instead of code: a URL, a File/Blob, or bytes.
startLinenumberNumber of the first line. Default 1.
wrapbooleanWrap 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.

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.

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.

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