Atoms
Code Snippet
A read-only syntax-highlighted code viewer powered by Shiki with CSS variable theming.
FormanceCustom component built for Formance products.
Installation#
npx shadcn@latest add https://ds.formance.com/r/code-snippet.jsonInstall Code Block, with its use-highlighted-code hook. Code Snippet is built on them.
Install the following dependencies:
npm install shiki class-variance-authorityCopy and paste the following code into your project.
Add the --shiki-* CSS variables to your global CSS.
The component uses CSS custom properties for syntax colors. Add these to your :root and .dark selectors:
:root { --shiki-foreground: var(--foreground); --shiki-background: transparent; --shiki-token-constant: var(--emerald-600); --shiki-token-string: var(--lilac-700); --shiki-token-comment: var(--emerald-500); --shiki-token-keyword: var(--mint-800); --shiki-token-parameter: var(--emerald-600); --shiki-token-function: var(--mint-800); --shiki-token-string-expression: var(--lilac-700); --shiki-token-punctuation: var(--foreground); --shiki-token-link: var(--emerald-600); --shiki-token-monetary: var(--gold-600);}
.dark { --shiki-foreground: var(--emerald-200); --shiki-background: var(--emerald-800); --shiki-token-constant: var(--mint-700); --shiki-token-string: var(--lilac-500); --shiki-token-comment: var(--emerald-400); --shiki-token-keyword: var(--mint-600); --shiki-token-parameter: var(--mint-700); --shiki-token-function: var(--mint-600); --shiki-token-string-expression: var(--lilac-500); --shiki-token-punctuation: var(--emerald-300); --shiki-token-link: var(--mint-700); --shiki-token-monetary: var(--gold-300);}Update the import paths to match your project setup.
Usage#
import { CodeSnippet } from '@/components/code/code-snippet';<CodeSnippet code={`const x = 42;`} language="typescript" />Examples#
Header bar#
Set showHeader to render a top bar with the language label on the left and the copy button on the right, instead of the floating hover copy button.
Numscript#
Numscript playground#
Use headerActions to render extra controls next to the copy button. Here a Numscript snippet gets an "open in playground" link, rendered as a Button (asChild) so it matches the built-in copy button. headerActions also works in the floating layout (without showHeader).