Snippet
A block of code, highlighted and copyable. Self-contained: the tokenizer and the palette ship with it, and there is no external highlighter to load.
API
Props
| prop | type | default | |
|---|---|---|---|
as? | ElementType | "div" | the element or component to render |
snippet | string | — | the source; trimmed before rendering |
title? | string | — | the label in the bar, usually the language |
className? | string | — | merged over the defaults, so it wins |
Examples
Usage
<Snippet title="tsx" snippet={USAGE_SNIPPET} />const USAGE_SNIPPET = `
<Guard guardIf={!user} thenRender={SignInPrompt}>
<Dashboard user={user} />
</Guard>
`;The source is a string, so it lives wherever your other content lives — a constant next to the page, a file, a CMS field. Every snippet on this site is a constant in a -constants module, which keeps the components free of long literals.
Details
The copy button
It is always there and takes no configuration: it writes the trimmed source to the clipboard and swaps its icon to a check for a second and a half. There is no visible text on purpose — a label would be one more string for every consumer to translate, for a control whose icon is unambiguous. It carries an aria-label so it is still announced.
Highlighting
The tokenizer is a single regular expression that recognises comments, strings, JSX tags, keywords, numbers, attributes, calls, capitalised identifiers and punctuation. It is tuned for TypeScript and TSX, and it is lossless: concatenating the tokens gives back the source exactly.
title is a label, not a mode: passing bash or css names the block for the reader, but the colours still come from the TypeScript rules. For short shell and CSS snippets the result reads fine; a project that needs true multi-language highlighting wants a grammar-based highlighter instead.
The trade is deliberate. A full highlighter means shipping grammars and a WASM engine — a heavy dependency for a component that renders a handful of curated snippets, and heavier still on a serverless function, where it lands in the bundle and the cold start.
The palette
Colours are tokens like everything else in the kit, with a light and a dark set, so the block follows the theme without the component knowing about it. Override them the way you would any other token.
:root {
--margo-code-keyword: #a5309a;
--margo-code-string: #0a7d3f;
}
.dark {
--margo-code-keyword: #ff7bd5;
--margo-code-string: #7ee787;
}| token | |
|---|---|
--margo-code-surface | background of the code area |
--margo-code-bar | background of the title bar |
--margo-code-plain | text with no token type |
--margo-code-comment | comments, rendered italic |
--margo-code-string | strings and template literals |
--margo-code-keyword | language keywords |
--margo-code-number | numeric literals |
--margo-code-tag | JSX tag names |
--margo-code-attr | attributes and object keys |
--margo-code-fn | call expressions |
--margo-code-type | capitalised identifiers |
--margo-code-punct | punctuation |