Code coloured by the foundations' syntax tokens, in over 30 languages.
1import { cn } from "@/lib/utils"2
3// Every look lives in the style map; the source only names it.4export function Badge({5 className,6 variant = "default",7 ...props8}: React.ComponentProps<"span"> & { variant?: "default" | "outline" }) {9 return (10 <span11 data-slot="badge"12 data-variant={variant}13 className={cn("cn-badge inline-flex items-center", className)}14 {...props}15 />16 )17}Give it the code and a language, or just a file name, and it colours the code with the syntax colours: six hues that follow the colour roles and two neutrals, contrast-checked on every surface in light and dark. A title bar shows the file and a Copy button; the code can number its lines, mark lines, show what was added and removed, and wrap.
Syntax Highlight is part of the Fabricator style. It's built on plain HTML, so the same component ships for every base, and the same tokenizer runs in the React Native version.
Installation#
bunx --bun fabricator-ui@latest add syntax-highlight
Usage#
import {
SyntaxHighlight,
SyntaxHighlightContent,
SyntaxHighlightCopy,
SyntaxHighlightHeader,
SyntaxHighlightIcon,
SyntaxHighlightTitle,
} from "@/components/ui/syntax-highlight"<SyntaxHighlight code={code} filename="app/page.tsx">
<SyntaxHighlightHeader>
<SyntaxHighlightIcon />
<SyntaxHighlightTitle />
<SyntaxHighlightCopy />
</SyntaxHighlightHeader>
<SyntaxHighlightContent showLineNumbers />
</SyntaxHighlight>With no children it renders just the code:
<SyntaxHighlight code="bun add refractor" language="bash" />Composition#
SyntaxHighlight the code, its language and its tokens
├── SyntaxHighlightHeader the title bar
│ ├── SyntaxHighlightIcon a file, or a terminal for shell code
│ ├── SyntaxHighlightTitle the file name, else the language's name
│ ├── SyntaxHighlightLanguage the language's name
│ └── SyntaxHighlightCopy copies the code
└── SyntaxHighlightContent the lines: numbers, marks, diff, wrappingEvery part reads the root through context, so they can sit anywhere inside it. For parts of your own, useSyntaxHighlight() returns the code, the language, the filename and the tokenized lines.
Examples#
Languages#
The language comes from language, a name or an alias (ts, sh, yml, html), or from the extension of filename when language is left out. Dockerfile, Makefile, .gitignore and .env files are read by their name.
{ "name": "acme-dashboard", "private": true, "scripts": { "dev": "next dev", "build": "next build" }, "dependencies": { "fabricator-ui": "^0.6.0" }}@import "tailwindcss";
:root { --radius: 0.5rem;}
.card:hover { background-color: var(--surface-3);}#!/usr/bin/env bashset -euo pipefail
# Ship only from a clean main.VERSION=$(node -p "require('./package.json').version")git tag "v$VERSION" && git push --tagsecho "Released v$VERSION"from dataclasses import dataclass
@dataclassclass Member: email: str is_admin: bool = False
def seed() -> list[Member]: return [Member("gabs@fabricator.dev", is_admin=True)]name: CIon: [push]jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: bun install && bun run test# Acme Dashboard
Built with **Fabricator UI**. Run `bun dev` and open[localhost:3000](http://localhost:3000).
- Light and dark themes- _Every_ component from the registryLine numbers#
showLineNumbers numbers the lines, and startLine sets the first number, for an excerpt. The numbers can't be selected, so copying the code leaves them out.
42export function getInitials(name: string) {43 return name44 .split(" ")45 .map((part) => part[0]?.toUpperCase() ?? "")46 .slice(0, 2)47 .join("")48}Highlighted lines#
highlightLines takes line numbers and inclusive ranges: [2, [6, 7]].
1import { Button } from "@/components/ui/button"2import { Spinner } from "@/components/ui/spinner"3
4export function SaveButton({ isSaving }: { isSaving: boolean }) {5 return (6 <Button disabled={isSaving}>7 {isSaving ? <Spinner data-icon="inline-start" /> : null}8 Save changes9 </Button>10 )11}Diff#
A diff or a .patch marks its added and removed lines on its own. Give a diff the filename of the file it changes and its lines keep that file's colours; language="diff-json" does the same without one. To show a change without a diff, mark lines with insertedLines and deletedLines.
@@ -1,5 +1,5 @@ { "name": "acme-dashboard",- "version": "0.5.1",+ "version": "0.6.0", "private": true }1export function Greeting({ name }: { name: string }) {2 return <p>Hello, {name}</p>3 return <p className="text-muted-foreground">Welcome back, {name}</p>4}Wrapping#
wrap wraps long lines at the panel's edge instead of scrolling sideways.
1const url = "https://fabricator-ui.vercel.app/r/fabricator/base-nova/syntax-highlight.json?icons=fabricator"2const message = "Long lines wrap at the panel's edge instead of scrolling sideways, and a hanging line keeps its number."Long code#
Give SyntaxHighlightContent a maximum height and it scrolls, fading at the top and bottom edges while there is more.
1export const members = [2 { id: 1, email: "member1@fabricator.dev", role: "admin" },3 { id: 2, email: "member2@fabricator.dev", role: "member" },4 { id: 3, email: "member3@fabricator.dev", role: "member" },5 { id: 4, email: "member4@fabricator.dev", role: "member" },6 { id: 5, email: "member5@fabricator.dev", role: "admin" },7 { id: 6, email: "member6@fabricator.dev", role: "member" },8 { id: 7, email: "member7@fabricator.dev", role: "member" },9 { id: 8, email: "member8@fabricator.dev", role: "member" },10 { id: 9, email: "member9@fabricator.dev", role: "admin" },11 { id: 10, email: "member10@fabricator.dev", role: "member" },12 { id: 11, email: "member11@fabricator.dev", role: "member" },13 { id: 12, email: "member12@fabricator.dev", role: "member" },14 { id: 13, email: "member13@fabricator.dev", role: "admin" },15 { id: 14, email: "member14@fabricator.dev", role: "member" },16 { id: 15, email: "member15@fabricator.dev", role: "member" },17 { id: 16, email: "member16@fabricator.dev", role: "member" },18 { id: 17, email: "member17@fabricator.dev", role: "admin" },19 { id: 18, email: "member18@fabricator.dev", role: "member" },20 { id: 19, email: "member19@fabricator.dev", role: "member" },21 { id: 20, email: "member20@fabricator.dev", role: "member" },22 { id: 21, email: "member21@fabricator.dev", role: "admin" },23 { id: 22, email: "member22@fabricator.dev", role: "member" },24 { id: 23, email: "member23@fabricator.dev", role: "member" },25 { id: 24, email: "member24@fabricator.dev", role: "member" },26 { id: 25, email: "member25@fabricator.dev", role: "admin" },27 { id: 26, email: "member26@fabricator.dev", role: "member" },28 { id: 27, email: "member27@fabricator.dev", role: "member" },29 { id: 28, email: "member28@fabricator.dev", role: "member" },30 { id: 29, email: "member29@fabricator.dev", role: "admin" },31 { id: 30, email: "member30@fabricator.dev", role: "member" },32 { id: 31, email: "member31@fabricator.dev", role: "member" },33 { id: 32, email: "member32@fabricator.dev", role: "member" },34 { id: 33, email: "member33@fabricator.dev", role: "admin" },35 { id: 34, email: "member34@fabricator.dev", role: "member" },36 { id: 35, email: "member35@fabricator.dev", role: "member" },37 { id: 36, email: "member36@fabricator.dev", role: "member" },38 { id: 37, email: "member37@fabricator.dev", role: "admin" },39 { id: 38, email: "member38@fabricator.dev", role: "member" },40 { id: 39, email: "member39@fabricator.dev", role: "member" },41 { id: 40, email: "member40@fabricator.dev", role: "member" },42]Command#
Copy can sit anywhere inside the root, here beside a one-line command.
npx fabricator-ui@latest add syntax-highlightColours#
Each token kind takes one of eight foundation tokens, installed with every Fabricator component (bg-syntax-keyword, text-syntax-string, …):
| Token | Follows | Colours |
|---|---|---|
--syntax-keyword | its own hue and chroma | keywords, at-rules, markdown headings |
--syntax-function | the info role | functions, methods, properties and keys, links |
--syntax-string | the success role | strings, characters, attribute values |
--syntax-constant | the warning role | numbers, booleans, constants, attribute names |
--syntax-type | its own hue and chroma | types, classes, built-ins, regular expressions |
--syntax-tag | the destructive role | tags, selectors, variables |
--syntax-comment | the surface tint | comments, line numbers |
--syntax-punctuation | the surface tint | punctuation and operators |
Retint a role and its tokens turn with it; set a role to strength 0 and they turn to the foreground colour. Keyword and type have no role, so they take their own variables:
:root {
--syntax-keyword-hue: 315;
--syntax-keyword-chroma: 0.16;
--syntax-type-hue: 195;
--syntax-type-chroma: 0.08;
}Every colour is the same lightness at any hue, so no token outshouts another, and every one keeps 4.5:1 against the first four surfaces, tinted or not, for any role setting. Added and removed lines use the success and destructive roles.
Languages#
These languages are bundled: Bash, C, C++, C#, CSS, Diff, Dockerfile, Go, GraphQL, HTML, XML and SVG, Ignore files, INI, Java, JavaScript, JSON, JSX, Kotlin, Lua, Makefile, Markdown, PHP, Python, Ruby, Rust, SCSS, SQL, Swift, TOML, TSX, TypeScript and YAML. Anything else renders as plain text.
refractor has grammars for around 300 more. Register one once, anywhere before it renders:
import elixir from "refractor/elixir"
import { registerLanguage } from "@/lib/syntax-highlight-model"
registerLanguage(elixir)To trim the bundle, remove languages from BUNDLED_LANGUAGES in lib/syntax-highlight-model.ts. The tokenizer is plain data with no DOM: highlightCode(code, language) returns the lines and their tokens, for a renderer of your own.
API Reference#
SyntaxHighlight#
All other props go to the root <div>.
| Prop | Type | Default |
|---|---|---|
code | string | required |
language | string | read from filename, else plain text |
filename | string | - |
children | ReactNode | <SyntaxHighlightContent /> |
SyntaxHighlightContent#
All other props go to the <pre>.
| Prop | Type | Default |
|---|---|---|
showLineNumbers | boolean | false |
startLine | number | 1 |
highlightLines | Array<number | [number, number]> | - |
insertedLines | Array<number | [number, number]> | - |
deletedLines | Array<number | [number, number]> | - |
wrap | boolean | false |
SyntaxHighlightCopy#
All other props go to the <button>. A handler that calls event.preventDefault() stops the copy.
| Prop | Type | Default |
|---|---|---|
value | string | the code |
onCopied | (value: string) => void | - |
SyntaxHighlightTitle, SyntaxHighlightLanguage#
Children replace the text: the file name or the language's name.
Data attributes#
| Attribute | Element | Present when |
|---|---|---|
data-slot="syntax-highlight" | root | always |
data-language | root | always: the language read, or text |
data-slot="syntax-highlight-content" | the <pre> | always |
data-wrap | the <pre> | wrap is true |
data-slot="syntax-highlight-line" | a line | always |
data-highlighted | a line | its number is in highlightLines |
data-diff="inserted" | "deleted" | a line | added or removed |
data-slot="syntax-highlight-line-number" | a line's number | showLineNumbers |
data-token | a token | the token's kind (keyword, …) |
data-copied | Copy | for two seconds after a copy |
Accessibility#
- The code is a
<pre>that takes focus, so a keyboard can scroll it sideways and down with the arrow keys. - Line numbers are hidden from screen readers and can't be selected, so neither reading nor copying the code includes them.
- Copy is a button named "Copy code". After a copy it's named "Copied", and a polite live region says "Copied to clipboard". When the page has no clipboard (an insecure page, a denied permission), it stays as it was.
- Every token colour, comments included, keeps 4.5:1 contrast against the code's surface in light and dark. Added and removed lines also carry a coloured edge, and a diff keeps its
+and-, so the change doesn't rest on colour alone.