Isometric line figures that answer the pointer. For React and for anything with a DOM.
Docs: code is highlighted in greys through shiki's CSS-variables theme, and the inspector's snippet uses the same variables
Lucas Marques9471651515d7
diff --git a/apps/site/app/globals.css b/apps/site/app/globals.cssindex 5222dde..2b5e9ca 100644--- a/apps/site/app/globals.css+++ b/apps/site/app/globals.css@@ -18,6 +18,18 @@--lift-lg: 0 1px 2px #00000008, 0 2px 6px #00000008, 0 32px 64px -34px #00000038;--ease-out: cubic-bezier(0.23, 1, 0.32, 1);--ease-icon: cubic-bezier(0.2, 0, 0, 1);+ /* Code in greys, each at least 4.5:1 on the code frame's #fafafa. shiki writes these names. */+ --code-foreground: #0a0a0a;+ --code-background: transparent;+ --code-token-constant: #0a0a0a;+ --code-token-function: #404040;+ --code-token-parameter: #404040;+ --code-token-string: #525252;+ --code-token-string-expression: #525252;+ --code-token-keyword: #6b6b6b;+ --code-token-comment: #6b6b6b;+ --code-token-punctuation: #6b6b6b;+ --code-token-link: #6b6b6b;}html { color-scheme: light; scroll-behavior: smooth; }@@ -119,9 +131,11 @@ em { font-style: italic; }/* The inspector's snippet, in greys: only the tag name carries weight. */.p-code { flex: 1; min-width: 0; margin: 0; overflow-x: auto; font: 400 13px/1.65 var(--font-mono); color: var(--color-ink); white-space: pre; }-.t-kw { color: var(--color-muted); }-.t-tag { font-weight: 500; }-.t-attr { color: #525252; }+.t-kw { color: var(--code-token-keyword); }+.t-str { color: var(--code-token-string); }+.t-tag { color: var(--code-token-constant); font-weight: 500; }+.t-attr { color: var(--code-token-function); }+.t-val { color: var(--code-foreground); }/* The inspector: the figure on the page itself, one dock under it for every control. */.s-inspector { display: grid; grid-template-columns: minmax(0, 1fr); justify-items: center; }diff --git a/apps/site/lib/highlight.ts b/apps/site/lib/highlight.tsindex 64c457f..5c1d14c 100644--- a/apps/site/lib/highlight.ts+++ b/apps/site/lib/highlight.ts@@ -1,11 +1,16 @@-import { codeToHtml } from "shiki";+import { codeToHtml, createCssVariablesTheme } from "shiki";/** Highlighting happens here, at build, so no highlighter reaches the browser. */+/** shiki writes var(--code-…) instead of colours; globals.css sets them, in greys. */+const theme = createCssVariablesTheme({ name: "hairline", variablePrefix: "--code-" });+const escape = (s: string) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");+/** The block around the code is its tab stop, so shiki's own tabindex on the pre goes. */export async function highlight(code: string, lang: string): Promise<string> {- return codeToHtml(code.trimEnd(), { lang, theme: "min-light" });+ const html = await codeToHtml(code.trimEnd(), { lang, theme });+ return html.replace(' tabindex="0"', "");}/** Plain text in the same wrapper the highlighter writes. */diff --git a/apps/site/test/docs.test.ts b/apps/site/test/docs.test.tsindex 015a5dd..f783f7c 100644--- a/apps/site/test/docs.test.ts+++ b/apps/site/test/docs.test.ts@@ -8,7 +8,8 @@ import { TABLE } from "../../../packages/hairline/src/intensity";import { FIGURES, INTENSITY, OPTIONS } from "@/lib/figures";import { llms, scale } from "@/lib/llms";import { tiny } from "@/lib/size";-import { PASTE, install, snippet } from "@/lib/snippets";+import { highlight } from "@/lib/highlight";+import { CDN, CSS, PASTE, REACT, VANILLA, install, snippet } from "@/lib/snippets";/** The docs describe the package, and these check that nothing was written by hand around it. */@@ -116,3 +117,26 @@ describe("/llms.txt", () => {expect(prose).not.toMatch(/undefined|NaN|\[object/);});});++/** Every snippet the docs highlight, with its language. */+const SAMPLES: [code: string, lang: string][] = [[REACT, "tsx"], [VANILLA, "ts"], [CDN, "html"], [CSS, "css"]];++describe("highlighting", () => {+ it("colours tokens with the --code- variables only, and leaves the block one tab stop", async () => {+ for (const [code, lang] of SAMPLES) {+ const html = await highlight(code, lang);+ expect(html).toContain("var(--code-");+ // the CSS sample holds #ffffff as text, so only colours set in a style count+ expect(html).not.toMatch(/(?:color|background-color):\s*#/i);+ expect(html).not.toContain("tabindex");+ }+ });++ it("writes only variables globals.css defines", async () => {+ const css = readFileSync(new URL("../app/globals.css", import.meta.url), "utf8");+ const used = new Set<string>();+ for (const [code, lang] of SAMPLES) for (const m of (await highlight(code, lang)).matchAll(/var\((--code-[a-z-]+)\)/g)) used.add(m[1]);+ expect(used.size).toBeGreaterThan(3);+ for (const name of used) expect(css).toContain(`${name}:`);+ });+});diff --git a/apps/site/test/site.spec.ts b/apps/site/test/site.spec.tsindex c52b237..75a1ea0 100644--- a/apps/site/test/site.spec.ts+++ b/apps/site/test/site.spec.ts@@ -52,6 +52,17 @@ test("the docs prerender six empty boxes, then draw one figure per row with a clexpect(noise).toEqual([]);});+test("the docs' code is in greys: every token's colour has equal red, green and blue", async ({ page }) => {+ await page.goto("/docs");+ const colours = await page.locator("main pre span").evaluateAll((spans) => spans.map((s) => getComputedStyle(s).color));+ expect(colours.length).toBeGreaterThan(10);+ const tinted = colours.filter((c) => {+ const [r, g, b] = c.match(/\d+(\.\d+)?/g)!.map(Number);+ return !(r === g && g === b);+ });+ expect(tinted).toEqual([]);+});+test("the inspector's figure picker picks the figure, and the snippet follows", async ({ page }) => {await page.goto("/");const inspector = page.locator("[data-inspector]");