# `<llumi-code>`

Syntax-highlighted code block (Prism via `refractor`), with 4 themes, copy and
download buttons, line-range dimming and a size guard. Renders in **shadow DOM**.
JSON is just `language="json"`. React: `Code` in `code.react.tsx`.

## Usage

Load the base stylesheet once per page (it styles the embedded buttons), then
the component:

```html
<link rel="stylesheet" href="https://esm.sh/@llumi/ui@1/base.css" />
<script type="module" src="https://esm.sh/@llumi/ui@1/code.mjs"></script>

<!-- as text content -->
<llumi-code language="rust">fn main() { println!("hi"); }</llumi-code>

<!-- or via the `code` property -->
<llumi-code language="json" download-button filename="data.json"></llumi-code>
<script>
  document.querySelector("llumi-code").code = JSON.stringify(data, null, 2);
</script>
```

## Properties / attributes

| Name | Attr | Type | Default | Description |
|------|------|------|---------|-------------|
| `code` | `code` | `string` | `""` | Source; falls back to the element's text content. |
| `language` | `language` | `string` | `""` | Prism language id (e.g. `typescript`, `json`). Aliases: `ts`, `js`, `py`, `rs`, `sh`, `md`, `html`. Unsupported → plain text + a console warning. |
| `theme` | `theme` | `ghcolors \| oneLight \| oneDark \| dracula` | `ghcolors` | Color theme. |
| `highlight` | — (property) | `[number, number][]` | — | 1-based line ranges. When set, line numbers show and other lines dim. |
| `maxSize` | `max-size` | `number \| false` | `102400` | UTF-8 byte limit. Above it, a "Code too large" notice replaces the highlighted code (buttons keep working). `max-size="false"` disables the limit. |
| `copyButton` | `copy-button` | `boolean` | `true` | Copy button. Disable with `copy-button="false"`. |
| `downloadButton` | `download-button` | `boolean` | `false` | Download button. Enable with the bare `download-button` attribute. |
| `filename` | `filename` | `string` | — | Download filename (default `code.<language>`); shown as the header label instead of the language. |

## Events

All bubble and are composed (they cross the shadow boundary).

| Event | Detail |
|-------|--------|
| `llumi-copy` / `llumi-copy-error` | `{ text }` / `{ error }` |
| `llumi-download` / `llumi-download-error` | `{ filename }` / `{ error }` |
| `llumi-code-too-large` | `{ size, maxSize }` — the code exceeds `maxSize` |

## Supported languages

`markup`/`html`/`xml`, `css`, `javascript`/`js`, `jsx`, `typescript`/`ts`, `tsx`,
`json`, `bash`/`sh`/`shell`, `python`/`py`, `rust`/`rs`, `go`, `java`, `yaml`,
`markdown`/`md`, `sql`, `diff`. Grammars load on first use.

## Dependencies

`lit`, `refractor`; embeds `<llumi-copy-button>` and `<llumi-download-button>`.

## React

```tsx
import { Code } from "./code.react";

<Code code={JSON.stringify(data, null, 2)} language="json" downloadButton filename="data.json" />
<Code code={source} language="ts" highlight={[[3, 5]]} maxSize={false} onCopy={(text) => track(text)} />
```

Props are the properties above, plus `onCopy(text)`, `onCopyError(error)`,
`onDownload(filename)`, `onDownloadError(error)`, `onTooLarge({ size, maxSize })`,
`ref` (the `LlumiCode` element), `className` and `style`.
