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

proptypedefault
as?ElementType"div"the element or component to render
snippetstringthe source; trimmed before rendering
title?stringthe label in the bar, usually the language
className?stringmerged over the defaults, so it wins

Examples

Usage

tsx
<Snippet title="tsx" snippet={USAGE_SNIPPET} />
previewtry the copy button
tsx
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.

css
:root {
  --margo-code-keyword: #a5309a;
  --margo-code-string: #0a7d3f;
}

.dark {
  --margo-code-keyword: #ff7bd5;
  --margo-code-string: #7ee787;
}
token
--margo-code-surfacebackground of the code area
--margo-code-barbackground of the title bar
--margo-code-plaintext with no token type
--margo-code-commentcomments, rendered italic
--margo-code-stringstrings and template literals
--margo-code-keywordlanguage keywords
--margo-code-numbernumeric literals
--margo-code-tagJSX tag names
--margo-code-attrattributes and object keys
--margo-code-fncall expressions
--margo-code-typecapitalised identifiers
--margo-code-punctpunctuation