Skip to content

Isometric line figures that answer the pointer. For React and for anything with a DOM.

Docs: one page of nine sections, code in a frame with line numbers and a remembered tab, the figures live with their intensity table

Lucas Marques030922282d99
diff --git a/apps/site/app/docs/page.tsx b/apps/site/app/docs/page.tsx
index 8e0979b..ffaa9ad 100644
--- a/apps/site/app/docs/page.tsx
+++ b/apps/site/app/docs/page.tsx
@@ -1,102 +1,50 @@
import type { Metadata } from "next";
-import type { ReactNode } from "react";
-import { Exploded, Phosphor, Riffle, Slow, Terrain, Turntable } from "@lucasmarkes/hairline/react";
import { Footer, Topbar } from "@/components/chrome";
-import { Install } from "@/components/install";
-import { Tabs } from "@/components/tabs";
-import { CARDS, FIGURES, OPTIONS } from "@/lib/figures";
+import { LINKS } from "@/lib/figures";
import { highlight } from "@/lib/highlight";
import { tiny } from "@/lib/size";
-import { PASTE, install } from "@/lib/snippets";
+import { CDN, CSS, QUICKSTART, REACT, REACT_SIGNATURE, VANILLA, VANILLA_SIGNATURE, install } from "@/lib/snippets";
+import { Api, GettingStarted, Reference } from "./sections";
const SITE = process.env.NEXT_PUBLIC_SITE_URL!;
-const SMALL = { riffle: Riffle, terrain: Terrain, exploded: Exploded, phosphor: Phosphor, slow: Slow, turntable: Turntable };
export const metadata: Metadata = { title: "Docs · hairline", alternates: { canonical: "/docs" } };
-function Heading({ id, title, children }: { id: string; title: ReactNode; children?: ReactNode }) {
- return (
- <header className="mb-10 max-w-[56ch]">
- <h2 id={id} className="scroll-mt-20 text-[30px] font-medium leading-[1.1] tracking-[-0.03em] md:text-[36px]">{title}</h2>
- {children ? <p className="mt-3 text-[16px] leading-[1.55] text-muted">{children}</p> : null}
- </header>
- );
-}
-
/**
- * The docs: install and paste, the options, and every figure small. A Server
- * Component, so each figure on it goes through server rendering and
+ * The docs: one page of sections. A Server Component, so every snippet is
+ * highlighted at build and each figure goes through server rendering and
* hydration on every deploy, as on the home.
*/
export default async function Docs() {
- const commands = install(SITE);
- const paste = await Promise.all(PASTE.map(async (p) => ({ label: p.label, html: await highlight(p.code, p.lang) })));
- const size = tiny();
+ const [quickstart, reactSignature, react, vanillaSignature, vanilla, cdn, css] = await Promise.all([
+ Promise.all(QUICKSTART.map(async (q) => ({ label: q.label, file: q.file, html: await highlight(q.code, q.lang) }))),
+ highlight(REACT_SIGNATURE, "tsx"),
+ highlight(REACT, "tsx"),
+ highlight(VANILLA_SIGNATURE, "ts"),
+ highlight(VANILLA, "ts"),
+ highlight(CDN, "html"),
+ highlight(CSS, "css"),
+ ]);
return (
<>
<Topbar />
- <main className="mx-auto grid max-w-[1080px] gap-32 px-[clamp(18px,5vw,28px)] pb-24 pt-16 md:pt-24 [&>*]:min-w-0">
- <section aria-labelledby="quickstart">
- <h1 id="quickstart" className="mb-10 scroll-mt-20 text-[36px] font-medium leading-[1.05] tracking-[-0.035em] md:text-[44px]">Get started</h1>
- <ol className="grid gap-12">
- <li className="step">
- <h2 className="step-title"><span className="step-n">1</span>Install</h2>
- <div className="max-w-[460px]"><Install commands={commands} /></div>
- </li>
- <li className="step">
- <h2 className="step-title"><span className="step-n">2</span>Paste</h2>
- <p className="mb-4 max-w-[60ch] text-[14px] leading-[1.55] text-muted">
- A figure fills its parent&rsquo;s width at a 5:4 aspect ratio. The React entry is a client module, so a Server Component can render it as it is.
- </p>
- <Tabs tabs={paste} label="Paste" />
- </li>
- </ol>
- <div className="mt-14 overflow-x-auto">
- <table className="props" data-options>
- <thead>
- <tr><th>Option</th><th>Type</th><th>Default</th><th>What it does</th></tr>
- </thead>
- <tbody>
- {OPTIONS.map((row) => (
- <tr key={row.name}><td>{row.name}</td><td>{row.type}</td><td>{row.default}</td><td>{row.description}</td></tr>
- ))}
- </tbody>
- </table>
- </div>
- </section>
-
- <section aria-labelledby="figures">
- <Heading id="figures" title="One prop, six figures">
- Every figure takes <code className="font-mono text-[14px] text-ink">intensity</code>, from 0 to 1. Here is what it turns up.
- </Heading>
- <ul className="grid">
- {FIGURES.map((doc) => {
- const Small = SMALL[doc.id];
- return (
- <li key={doc.id} className="figure-row" data-row={doc.id}>
- <div>
- <h3 className="text-[20px] font-medium leading-none tracking-[-0.02em]">{doc.name}</h3>
- <p className="mt-3 text-[16px] leading-[1.5]">{doc.stronger}</p>
- <p className="mt-1 max-w-[52ch] text-[14px] leading-[1.55] text-muted">{doc.summary}</p>
- </div>
- <div className="tile w-full max-w-[240px] justify-self-end"><Small /></div>
- </li>
- );
- })}
- </ul>
- </section>
-
- <section aria-label="Why" className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
- {CARDS.map((card) => (
- <div key={card.title} className="card" data-card={card.title}>
- <h3 className="text-[15px] font-medium">{card.title}</h3>
- {card.title === "Tiny" ? <p className="mt-3 text-[30px] font-medium leading-none tracking-[-0.03em] tabular-nums" data-size>{size}</p> : null}
- <p className="mt-2 text-[14px] leading-[1.5] text-muted">{card.body}</p>
- </div>
- ))}
- </section>
- </main>
+ <div className="docs">
+ <main className="docs-main">
+ <header className="max-w-[64ch]">
+ <h1 className="text-[36px] font-medium leading-[1.05] tracking-[-0.035em] md:text-[44px]">Six figures, one set of options.</h1>
+ <p className="mt-4 text-[16px] leading-[1.55] text-muted">
+ Every figure takes the same four options and draws itself in SVG, with no dependencies. Install the package, paste a figure, and turn <code className="doc-code">intensity</code> up or down.
+ </p>
+ </header>
+ <GettingStarted commands={install(SITE)} size={tiny()} quickstart={quickstart} />
+ <Api code={{ reactSignature, react, vanillaSignature, vanilla, cdn }} />
+ <Reference css={css} />
+ <p className="doc-note">
+ Anything missing? <a className="doc-more" href={`${LINKS.github}/issues`}>Open an issue on GitHub</a>.
+ </p>
+ </main>
+ </div>
<Footer />
</>
);
diff --git a/apps/site/app/docs/sections.tsx b/apps/site/app/docs/sections.tsx
new file mode 100644
index 0000000..a65e9ef
--- /dev/null
+++ b/apps/site/app/docs/sections.tsx
@@ -0,0 +1,150 @@
+import type { ReactNode } from "react";
+import { Exploded, Phosphor, Riffle, Slow, Terrain, Turntable } from "@lucasmarkes/hairline/react";
+import { CodeBlock } from "@/components/code-block";
+import { Install } from "@/components/install";
+import { Tabs, type Tab } from "@/components/tabs";
+import { SECTIONS } from "@/lib/docs";
+import { FIGURES, INTENSITY, OPTIONS, THEME, measure } from "@/lib/figures";
+import { PACKAGE } from "@/lib/snippets";
+
+const SMALL = { riffle: Riffle, terrain: Terrain, exploded: Exploded, phosphor: Phosphor, slow: Slow, turntable: Turntable };
+
+/** A section takes its title from SECTIONS, the list the sidebar reads, so the two always agree. */
+function Section({ id, children }: { id: string; children: ReactNode }) {
+ const { title } = SECTIONS.find((s) => s.id === id)!;
+ return (
+ <section id={id} aria-labelledby={`${id}-title`} className="doc-section">
+ <h2 id={`${id}-title`} className="doc-h2">{title}</h2>
+ {children}
+ </section>
+ );
+}
+
+const C = ({ children }: { children: ReactNode }) => <code className="doc-code">{children}</code>;
+
+export function GettingStarted({ commands, size, quickstart }: { commands: { label: string; code: string }[]; size: string; quickstart: Tab[] }) {
+ return (
+ <>
+ <Section id="install">
+ <p className="doc-p">Install the package, then import a figure wherever your interface runs.</p>
+ <div className="max-w-[460px]"><Install commands={commands} /></div>
+ <p className="doc-note">
+ Click the command to switch package manager. ESM only, no dependencies, <span data-size>{size}</span> gzipped for all six; a bundle that imports one carries one.
+ </p>
+ </Section>
+ <Section id="quick-start">
+ <p className="doc-p">Paste a figure. It fills its parent&rsquo;s width at a 5:4 aspect ratio.</p>
+ <Tabs tabs={quickstart} label="Quick start" />
+ </Section>
+ <Section id="options">
+ <p className="doc-p">Every figure takes the same four options, all optional.</p>
+ <div className="overflow-x-auto">
+ <table className="props" data-options>
+ <thead>
+ <tr><th>Option</th><th>Type</th><th>Default</th><th>What it does</th></tr>
+ </thead>
+ <tbody>
+ {OPTIONS.map((row) => (
+ <tr key={row.name}>
+ <td><code>{row.name}</code></td><td><code>{row.type}</code></td><td><code>{row.default}</code></td><td>{row.description}</td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+ </Section>
+ </>
+ );
+}
+
+export function Api({ code }: { code: { reactSignature: string; react: string; vanillaSignature: string; vanilla: string; cdn: string } }) {
+ return (
+ <>
+ <Section id="react">
+ <CodeBlock title="Signature" html={code.reactSignature} plain />
+ <p className="doc-p">
+ Import any of the six from <C>{PACKAGE}/react</C>. Each renders a <C>&lt;div&gt;</C>, takes any <C>&lt;div&gt;</C> attribute and forwards its ref. The entry is a client module, so a Server Component renders it without writing <C>&quot;use client&quot;</C>.
+ </p>
+ <CodeBlock title="app/page.tsx" html={code.react} />
+ </Section>
+ <Section id="vanilla">
+ <CodeBlock title="Signature" html={code.vanillaSignature} plain />
+ <p className="doc-p">
+ One function per figure, named in lower case: {FIGURES.map((f, i) => <span key={f.id}>{i ? ", " : ""}<C>{f.id}</C></span>)}. In <C>update</C>, a key set to <C>undefined</C> goes back to its default and a key left out stays as it is. <C>destroy</C> removes the drawing and its listeners.
+ </p>
+ <CodeBlock title="main.ts" html={code.vanilla} />
+ </Section>
+ <Section id="cdn">
+ <p className="doc-p">Without a bundler, import from esm.sh in a module script.</p>
+ <CodeBlock title="index.html" html={code.cdn} />
+ </Section>
+ </>
+ );
+}
+
+export function Reference({ css }: { css: string }) {
+ return (
+ <>
+ <Section id="figures">
+ <p className="doc-p">Every figure takes <C>intensity</C>, from 0 to 1. Here is what it turns up.</p>
+ <ul className="grid">
+ {FIGURES.map((doc) => {
+ const Small = SMALL[doc.id];
+ return (
+ <li key={doc.id} className="figure-row" data-row={doc.id}>
+ <div>
+ <h3 className="text-[20px] font-medium leading-none tracking-[-0.02em]">{doc.name}</h3>
+ <p className="mt-3 text-[15px] leading-[1.5]">Higher intensity: {doc.stronger}</p>
+ <p className="mt-1 max-w-[52ch] text-[14px] leading-[1.55] text-muted">{doc.summary}</p>
+ </div>
+ <div className="tile w-full max-w-[240px] justify-self-end"><Small /></div>
+ </li>
+ );
+ })}
+ </ul>
+ <div className="table-scroll" tabIndex={0} role="region" aria-label="What intensity sets in each figure">
+ <table className="intensity" data-intensity>
+ <thead>
+ <tr><th>Figure</th><th>Parameter</th><th>0</th><th>0.5</th><th>1</th></tr>
+ </thead>
+ <tbody>
+ {FIGURES.map((doc) => (
+ <tr key={doc.id}>
+ <td>{doc.name}</td>
+ <td><code>{doc.parameter.name}</code></td>
+ {INTENSITY[doc.id].map((n, i) => <td key={i}><code>{measure(n, doc.parameter.unit)}</code></td>)}
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+ <a className="doc-more" href="/#try">Try them on the home page →</a>
+ </Section>
+ <Section id="theme">
+ <p className="doc-p">
+ Six custom properties, set on the figure or on any ancestor. Without them a figure is light, or dark when an ancestor has class <C>dark</C> or <C>data-theme=&quot;dark&quot;</C>, or when the page&rsquo;s <C>color-scheme</C> is dark.
+ </p>
+ <div className="overflow-x-auto">
+ <table className="props" data-tokens>
+ <thead>
+ <tr><th>Property</th><th>Light</th><th>Role</th></tr>
+ </thead>
+ <tbody>
+ {THEME.map((t) => (
+ <tr key={t.property}><td><code>{t.property}</code></td><td><code>{t.light}</code></td><td>{t.role}</td></tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+ <CodeBlock title="globals.css" html={css} />
+ </Section>
+ <Section id="accessibility">
+ <ul className="doc-list">
+ <li>Each figure is an image with a description; <C>label</C> (or <C>aria-label</C> in React) replaces it.</li>
+ <li>Riffle is a focusable group: the arrow keys walk its cards and a live region reads out the card&rsquo;s number.</li>
+ <li>Under <C>prefers-reduced-motion</C>, Phosphor and Slow hold still, and every figure still answers the pointer.</li>
+ </ul>
+ </Section>
+ </>
+ );
+}
diff --git a/apps/site/app/globals.css b/apps/site/app/globals.css
index 2b5e9ca..11ea910 100644
--- a/apps/site/app/globals.css
+++ b/apps/site/app/globals.css
@@ -14,6 +14,7 @@
}
:root {
+ --topbar: 54px;
--lift: 0 1px 2px #00000008, 0 10px 28px -16px #00000024;
--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);
@@ -39,7 +40,7 @@ em { font-style: italic; }
.topbar {
position: sticky; top: 0; z-index: 10;
display: flex; align-items: center; justify-content: space-between;
- padding: 12px clamp(18px, 5vw, 28px);
+ height: var(--topbar); padding: 0 clamp(18px, 5vw, 28px);
background: color-mix(in srgb, var(--color-canvas) 82%, transparent);
backdrop-filter: blur(12px);
}
@@ -73,29 +74,47 @@ em { font-style: italic; }
.slider::-moz-range-track { height: 2px; border-radius: 1px; background: rgba(0, 0, 0, 0.16); }
.slider::-moz-range-thumb { width: 16px; height: 16px; border: 0; border-radius: 50%; background: #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.2); }
-.code { overflow: hidden; border-radius: 12px; background: #fff; box-shadow: inset 0 0 0 1px var(--color-stone); }
-.code-bar { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 4px 8px; border-bottom: 1px solid var(--color-line); }
-.code-tab { border-radius: 7px; padding: 6px 10px; font-size: 13px; font-weight: 500; line-height: 1; color: var(--color-muted); }
-.code-tab[aria-selected="true"] { background: var(--color-stone); color: var(--color-ink); }
+/* Code: a frame, a bar with the title and a copy icon, the code. Line numbers are a counter in ::before, so they are never selected or copied. */
+.code { overflow: hidden; border-radius: 12px; background: #fafafa; box-shadow: inset 0 0 0 1px var(--color-stone); }
+.code-bar { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 4px 4px 4px 14px; border-bottom: 1px solid var(--color-stone); }
+.code-tabs { display: flex; flex: none; gap: 2px; margin-left: -8px; }
+.code-tab { border-radius: 7px; padding: 6px 10px; font-size: 13px; font-weight: 500; line-height: 1; color: var(--color-muted); transition: background-color 150ms ease, color 150ms ease; }
+.code-tab[aria-selected="true"] { background: #fff; color: var(--color-ink); box-shadow: 0 0 0 1px var(--color-stone); }
.code-tab:hover { color: var(--color-ink); }
-.code-panel { margin: 0; overflow-x: auto; padding: 16px 18px; font: 400 13px/1.65 var(--font-mono); }
-.code-panel pre { margin: 0; background: transparent !important; }
-.code-panel code { font: inherit; }
+.code-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; color: var(--color-muted); }
+.code-tabs + .code-title { text-align: right; }
+.code-body { overflow-x: auto; padding: 16px 18px; font: 400 13px/1.65 var(--font-mono); }
+.code-body pre { margin: 0; }
+.code-body code { font: inherit; counter-reset: line; }
+.code-body .line::before { counter-increment: line; content: counter(line); display: inline-block; width: 2ch; margin-right: 16px; text-align: right; color: var(--color-faint); }
+.code-body[data-plain] .line::before { content: none; }
.tile { overflow: hidden; border-radius: 14px; background: #fff; box-shadow: inset 0 0 0 1px var(--color-stone), var(--lift); }
.figure-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(120px, 240px); align-items: center; gap: 24px; padding: 28px 0; border-top: 1px solid var(--color-line); }
.figure-row:last-child { border-bottom: 1px solid var(--color-line); }
-.card { border-radius: 14px; padding: 20px; background: #fff; box-shadow: inset 0 0 0 1px var(--color-stone); }
-
-.step { min-width: 0; }
-.step-title { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; font-size: 17px; font-weight: 500; }
-.step-n { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--color-ink); color: #fff; font-size: 12px; font-weight: 500; line-height: 1; font-variant-numeric: tabular-nums; }
-
-.props { width: 100%; border-collapse: collapse; font-size: 13px; }
-.props th { padding: 8px 14px 8px 0; text-align: left; font-size: 13px; font-weight: 500; color: var(--color-muted); }
-.props td { padding: 12px 14px 12px 0; vertical-align: top; border-top: 1px solid var(--color-line); line-height: 1.5; }
-.props td:nth-child(-n + 3) { font-family: var(--font-mono); white-space: nowrap; }
+.props, .intensity { width: 100%; border-collapse: collapse; font-size: 13px; }
+:is(.props, .intensity) th { padding: 8px 14px 8px 0; text-align: left; font-size: 13px; font-weight: 500; color: var(--color-muted); }
+:is(.props, .intensity) td { padding: 12px 14px 12px 0; vertical-align: top; border-top: 1px solid var(--color-line); line-height: 1.5; }
+.props td:nth-child(-n + 3) { white-space: nowrap; }
.props td:nth-child(2), .props td:nth-child(3) { color: var(--color-muted); }
+.props[data-tokens] td:nth-child(3) { white-space: normal; color: var(--color-ink); }
+.intensity td { white-space: nowrap; }
+.intensity td:nth-child(n + 3) { color: var(--color-muted); font-variant-numeric: tabular-nums; }
+.table-scroll { overflow-x: auto; border-radius: 4px; }
+
+/* The docs: the header and nine sections in one column. Prose stops at 64ch; code and tables take the column. */
+.docs { --gutter: clamp(18px, 5vw, 28px); --doc-offset: calc(var(--topbar) + 24px); max-width: 1080px; margin: 0 auto; padding: 0 var(--gutter); }
+.docs-main { display: grid; gap: 64px; min-width: 0; padding: 48px 0 96px; }
+.docs-main > * { min-width: 0; }
+.doc-section { display: grid; gap: 16px; scroll-margin-top: var(--doc-offset); }
+.doc-section > * { min-width: 0; }
+.doc-h2 { font-size: 24px; font-weight: 500; line-height: 1.2; letter-spacing: -0.02em; }
+.doc-p, .doc-list { max-width: 64ch; font-size: 15px; line-height: 1.6; text-wrap: pretty; }
+.doc-list { display: grid; gap: 8px; padding-left: 1.2em; list-style: disc; }
+.doc-note { max-width: 64ch; font-size: 14px; line-height: 1.55; color: var(--color-muted); text-wrap: pretty; }
+.doc-code { font-size: 0.9em; color: var(--color-ink); }
+.doc-more { justify-self: start; font-size: 14px; color: var(--color-ink); text-decoration: underline; text-decoration-color: var(--color-faint); text-underline-offset: 4px; }
+.doc-more:hover { text-decoration-color: currentColor; }
/* The hero: each block rises out of a blur, one after another. */
.hero-title { max-width: 16ch; font-size: 44px; font-weight: 500; line-height: 1.02; letter-spacing: -0.045em; text-wrap: balance; }
@@ -166,15 +185,18 @@ em { font-style: italic; }
.s-range { flex: 1; width: auto; }
}
-:is(button, a, input, .code-panel):focus-visible { outline: 1.5px solid var(--color-ink); outline-offset: 2px; }
+:is(button, a, input, .code-body, .table-scroll):focus-visible { outline: 1.5px solid var(--color-ink); outline-offset: 2px; }
@media (max-width: 640px) {
- .figure-row { grid-template-columns: minmax(0, 1fr) 120px; gap: 16px; }
+ /* a row stacks, text first, and the figure keeps its width */
+ .figure-row { grid-template-columns: minmax(0, 1fr); gap: 16px; }
+ .figure-row .tile { justify-self: start; }
/* one option per block: name, type and default on a line, the description under them */
.props thead { display: none; }
.props tr { display: flex; flex-wrap: wrap; column-gap: 12px; padding: 14px 0; border-top: 1px solid var(--color-line); }
.props td { padding: 0; border: 0; }
.props td:nth-child(4) { flex-basis: 100%; margin-top: 6px; }
+ .props[data-tokens] td:nth-child(3) { flex-basis: 100%; margin-top: 6px; }
}
@media (prefers-reduced-motion: reduce) {
html { scroll-behavior: auto; }
diff --git a/apps/site/app/page.tsx b/apps/site/app/page.tsx
index b96e4f2..38badf2 100644
--- a/apps/site/app/page.tsx
+++ b/apps/site/app/page.tsx
@@ -33,7 +33,7 @@ export default function Page() {
</div>
</section>
- <section aria-label="Try it">
+ <section id="try" aria-label="Try it" className="scroll-mt-[calc(var(--topbar)+24px)]">
<Inspector />
</section>
</main>
diff --git a/apps/site/components/code-block.tsx b/apps/site/components/code-block.tsx
new file mode 100644
index 0000000..4ba15cc
--- /dev/null
+++ b/apps/site/components/code-block.tsx
@@ -0,0 +1,23 @@
+"use client";
+
+import { useRef } from "react";
+import { CopyIcon } from "./copy";
+
+/**
+ * A code sample in its frame: a bar with its title and a copy icon, then the
+ * code, which is the block's one tab stop so the keyboard can scroll it.
+ * Line numbers are drawn in CSS, so they are never selected or copied;
+ * `plain` leaves them off.
+ */
+export function CodeBlock({ title, html, plain = false }: { title: string; html: string; plain?: boolean }) {
+ const body = useRef<HTMLDivElement>(null);
+ return (
+ <div className="code" data-code>
+ <div className="code-bar">
+ <span className="code-title">{title}</span>
+ <CopyIcon text={() => body.current?.textContent ?? ""} select={body} label="Copy code" />
+ </div>
+ <div ref={body} tabIndex={0} className="code-body" data-plain={plain || undefined} dangerouslySetInnerHTML={{ __html: html }} />
+ </div>
+ );
+}
diff --git a/apps/site/components/tabs.tsx b/apps/site/components/tabs.tsx
index 3c9f2e3..389c051 100644
--- a/apps/site/components/tabs.tsx
+++ b/apps/site/components/tabs.tsx
@@ -1,41 +1,62 @@
"use client";
-import { useId, useRef, useState, type KeyboardEvent } from "react";
+import { useEffect, useId, useRef, useState, type KeyboardEvent } from "react";
import { CopyIcon } from "./copy";
-export type Tab = { label: string; html: string };
+export type Tab = { label: string; file: string; html: string };
-/** Code behind tabs, with a copy icon. The HTML is highlighted at build. */
+/** Where the reader's tab is kept between visits. */
+const KEY = "hairline:docs-tab";
+
+/**
+ * Code under tabs, in the same frame as CodeBlock: the tabs on the left, the
+ * shown tab's file name and the copy icon on the right. The server renders
+ * the first tab; the stored one is read after mount, and storage that throws
+ * (private mode, blocked) leaves the first tab on.
+ */
export function Tabs({ tabs, label }: { tabs: Tab[]; label: string }) {
const [at, setAt] = useState(0);
const panel = useRef<HTMLDivElement>(null);
const list = useRef<HTMLDivElement>(null);
const id = useId();
+ useEffect(() => {
+ try {
+ const stored = tabs.findIndex((tab) => tab.label === localStorage.getItem(KEY));
+ if (stored > 0) setAt(stored);
+ } catch {}
+ }, [tabs]);
+
+ const pick = (i: number) => {
+ setAt(i);
+ try {
+ localStorage.setItem(KEY, tabs[i].label);
+ } catch {}
+ };
+
const onKeyDown = (event: KeyboardEvent) => {
const step = event.key === "ArrowRight" ? 1 : event.key === "ArrowLeft" ? -1 : 0;
if (!step) return;
event.preventDefault();
const next = (at + step + tabs.length) % tabs.length;
- setAt(next);
+ pick(next);
list.current?.querySelectorAll<HTMLButtonElement>("[role=tab]")[next]?.focus();
};
return (
- <div className="code">
+ <div className="code" data-code>
<div className="code-bar">
- <div ref={list} role="tablist" aria-label={label} onKeyDown={onKeyDown} className="flex gap-1">
+ <div ref={list} role="tablist" aria-label={label} onKeyDown={onKeyDown} className="code-tabs">
{tabs.map((tab, i) => (
- <button key={tab.label} type="button" role="tab" id={`${id}-tab-${i}`} aria-selected={i === at} aria-controls={`${id}-panel`} tabIndex={i === at ? 0 : -1} onClick={() => setAt(i)} className="code-tab">
+ <button key={tab.label} type="button" role="tab" id={`${id}-tab-${i}`} aria-selected={i === at} aria-controls={`${id}-panel`} tabIndex={i === at ? 0 : -1} onClick={() => pick(i)} className="code-tab">
{tab.label}
</button>
))}
</div>
- {/* read at the click, so it is always the tab on screen */}
+ <span className="code-title" title={tabs[at].file}>{tabs[at].file}</span>
<CopyIcon text={() => panel.current?.textContent ?? ""} select={panel} label="Copy code" />
</div>
- <div ref={panel} role="tabpanel" id={`${id}-panel`} aria-labelledby={`${id}-tab-${at}`} tabIndex={0} className="code-panel" dangerouslySetInnerHTML={{ __html: tabs[at].html }} />
+ <div ref={panel} role="tabpanel" id={`${id}-panel`} aria-labelledby={`${id}-tab-${at}`} tabIndex={0} className="code-body" dangerouslySetInnerHTML={{ __html: tabs[at].html }} />
</div>
);
}
-
diff --git a/apps/site/lib/figures.ts b/apps/site/lib/figures.ts
index 422eb00..dbf1dfe 100644
--- a/apps/site/lib/figures.ts
+++ b/apps/site/lib/figures.ts
@@ -99,14 +99,6 @@ export const THEME: { property: string; light: string; role: string }[] = [
{ property: "--hairline-stroke", light: "0.9", role: "The stroke width, in CSS pixels at any size." },
];
-/** The four cards under the figures. Tiny's body is the measured size; see lib/size.ts. */
-export const CARDS: { title: string; body: string }[] = [
- { title: "Tiny", body: "gzipped, all six figures. A bundle that imports one carries one." },
- { title: "No dependencies", body: "SVG and one shared animation frame. React is optional." },
- { title: "Accessible", body: "Keyboard on Riffle, a description for screen readers, and stillness under reduced motion." },
- { title: "Themeable", body: "Six CSS variables, on the figure or anything above it. Light and dark built in." },
-];
-
export const LINKS = {
github: "https://github.com/lucasmarkes/hairline",
npm: "https://www.npmjs.com/package/@lucasmarkes/hairline",
diff --git a/apps/site/lib/snippets.ts b/apps/site/lib/snippets.ts
index cc5a3ab..c19a0b7 100644
--- a/apps/site/lib/snippets.ts
+++ b/apps/site/lib/snippets.ts
@@ -85,11 +85,3 @@ export const VANILLA_SIGNATURE = `terrain(element: HTMLElement, options?: Hairli
destroy(): void
}
`;
-
-/** The quickstart's second step. */
-export const PASTE: { label: string; lang: string; code: string }[] = [
- { label: "React", lang: "tsx", code: REACT },
- { label: "Vanilla", lang: "ts", code: VANILLA },
- { label: "CDN", lang: "html", code: CDN },
- { label: "CSS", lang: "css", code: CSS },
-];
diff --git a/apps/site/test/site.spec.ts b/apps/site/test/site.spec.ts
index 75a1ea0..e545307 100644
--- a/apps/site/test/site.spec.ts
+++ b/apps/site/test/site.spec.ts
@@ -156,18 +156,20 @@ test("without a clipboard, copy selects the text instead and throws nothing", as
expect(noise).toEqual([]);
});
-test("Get started leads to the docs, whose quickstart pastes four ways and lists the four options", async ({ page, context }) => {
+test("Get started leads to the docs, whose quick start pastes three ways and copies the tab on show", async ({ page, context }) => {
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
await page.goto("/");
await page.getByRole("link", { name: "Get started" }).click();
await expect(page).toHaveURL(/\/docs$/);
- const quick = page.locator("section[aria-labelledby=quickstart]");
- await expect(quick.getByRole("tab")).toHaveText(["React", "Vanilla", "CDN", "CSS"]);
- await quick.getByRole("tab", { name: "CSS" }).click();
- await expect(quick.locator(".code-panel")).toContainText("--hairline-plate: #ffffff;");
- await quick.locator(".code").getByRole("button", { name: "Copy code" }).click();
- expect(await page.evaluate(() => navigator.clipboard.readText())).toContain("--hairline-stroke: 0.9;");
- await expect(quick.locator("[data-options] tbody tr td:first-child")).toHaveText(["intensity", "theme", "label", "onRead"]);
+ const quick = page.locator("#quick-start");
+ await expect(quick.getByRole("tab")).toHaveText(["React", "Vanilla", "CDN"]);
+ await expect(quick.locator(".code-title")).toHaveText("app/page.tsx");
+ await quick.getByRole("tab", { name: "CDN" }).click();
+ await expect(quick.locator(".code-title")).toHaveText("index.html");
+ await expect(quick.getByRole("tabpanel")).toContainText("https://esm.sh/@lucasmarkes/hairline");
+ await quick.getByRole("button", { name: "Copy code" }).click();
+ expect(await page.evaluate(() => navigator.clipboard.readText())).toMatch(/^<div id="figure"/);
+ await expect(page.locator("#options [data-options] tbody tr td:first-child")).toHaveText(["intensity", "theme", "label", "onRead"]);
});
test("llms.txt and the registry item are served", async ({ request }) => {
@@ -218,18 +220,26 @@ test("without a clipboard at all, the top bar's llms.txt button opens the file",
expect(noise.filter((line) => line.startsWith("pageerror"))).toEqual([]);
});
-test("one family outside the hero: the serif is the headline's one word, mono is code", async ({ page }) => {
- await page.goto("/");
- await expect(page.locator("h1 em")).toHaveCSS("font-family", /Instrument Serif/);
- const families = await page.evaluate(() =>
+/** Every element that holds text of its own, outside code, the hero's serif word and the figures, with its family. */
+const families = (page: Page) =>
+ page.evaluate(() =>
[...document.querySelectorAll("body *")]
// the install pill is code, prompt and all
.filter((el) => !el.closest("h1 em, code, pre, .pill, [data-hairline]") && [...el.childNodes].some((n) => n.nodeType === 3 && n.textContent!.trim()))
.map((el) => `${el.tagName} ${getComputedStyle(el).fontFamily}`),
);
- expect(families.length).toBeGreaterThan(5);
- expect(families.filter((f) => !/geist/i.test(f) || /mono/i.test(f))).toEqual([]);
+
+test("one family outside the hero: the serif is the headline's one word, mono is code", async ({ page }) => {
+ await page.goto("/");
+ await expect(page.locator("h1 em")).toHaveCSS("font-family", /Instrument Serif/);
+ const home = await families(page);
+ expect(home.length).toBeGreaterThan(5);
+ expect(home.filter((f) => !/geist/i.test(f) || /mono/i.test(f))).toEqual([]);
+
await page.goto("/docs");
+ const docs = await families(page);
+ expect(docs.length).toBeGreaterThan(40);
+ expect(docs.filter((f) => !/geist/i.test(f) || /mono/i.test(f))).toEqual([]);
for (const id of IDS) await expect(page.locator(`[data-row="${id}"] h3`)).toHaveCSS("font-style", "normal");
});
@@ -259,3 +269,60 @@ test("the footer links the author's site, then X, GitHub, npm and llms.txt", asy
await expect(footer.getByRole("link", { name: "X", exact: true })).toHaveAttribute("href", "https://x.com/lucasmarkes__");
await expect(footer.getByRole("link")).toHaveText(["Lucas Marques", "X", "GitHub", "npm", "llms.txt"]);
});
+
+test("a code block copies its code without the line numbers, and a signature has none", async ({ page, context }) => {
+ await context.grantPermissions(["clipboard-read", "clipboard-write"]);
+ await page.goto("/docs");
+ const blocks = page.locator("#react [data-code]");
+ await expect(blocks.locator(".code-title")).toHaveText(["Signature", "app/page.tsx"]);
+ const usage = blocks.last();
+ await usage.getByRole("button", { name: "Copy code" }).click();
+ const text = await page.evaluate(() => navigator.clipboard.readText());
+ expect(text.split("\n")[0]).toBe('import { Terrain } from "@lucasmarkes/hairline/react";');
+ expect(text).not.toMatch(/^\s*\d/m);
+ const number = (block: typeof usage) => block.locator(".line").first().evaluate((el) => getComputedStyle(el, "::before").content);
+ expect(await number(usage)).not.toBe("none");
+ expect(await number(blocks.first())).toBe("none");
+});
+
+test("the quick start remembers the tab a reader picked", async ({ page }) => {
+ await page.goto("/docs");
+ const quick = page.locator("#quick-start");
+ await quick.getByRole("tab", { name: "Vanilla" }).click();
+ await page.reload();
+ await expect(quick.getByRole("tab", { name: "Vanilla" })).toHaveAttribute("aria-selected", "true");
+ await expect(quick.locator(".code-title")).toHaveText("main.ts");
+});
+
+test("when storage throws, the quick start stays on React and still switches, with a clean console", async ({ page }) => {
+ await page.addInitScript(() => {
+ Object.defineProperty(window, "localStorage", { get() { throw new DOMException("blocked", "SecurityError"); } });
+ });
+ const noise = watch(page);
+ await page.goto("/docs");
+ const quick = page.locator("#quick-start");
+ await expect(quick.getByRole("tab", { name: "React" })).toHaveAttribute("aria-selected", "true");
+ await quick.getByRole("tab", { name: "CDN" }).click();
+ await expect(quick.getByRole("tab", { name: "CDN" })).toHaveAttribute("aria-selected", "true");
+ expect(noise).toEqual([]);
+});
+
+test("the docs fit a phone down to 320px: rows stack text first, and wide code and tables scroll in their own box", async ({ page }) => {
+ for (const width of [390, 320]) {
+ await page.setViewportSize({ width, height: 800 });
+ await page.goto("/docs");
+ expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(width);
+ const row = page.locator('[data-row="terrain"]');
+ // the whole text block, so a tile centred beside a short heading does not pass for stacked
+ const text = (await row.locator(":scope > div").first().boundingBox())!;
+ const tile = (await row.locator(".tile").boundingBox())!;
+ expect(tile.y).toBeGreaterThan(text.y + text.height);
+ }
+});
+
+test("the figures' link goes to the home's figure", async ({ page }) => {
+ await page.goto("/docs");
+ await page.getByRole("link", { name: "Try them on the home page →" }).click();
+ await expect(page).toHaveURL(/\/#try$/);
+ await expect(page.locator("#try [data-inspector]")).toBeInViewport();
+});