Skip to content

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

Docs: a sidebar of anchors marks the section in view, and on a phone it is one strip under the top bar that follows the reader

Lucas Marquese8dda78777cd
diff --git a/apps/site/app/docs/page.tsx b/apps/site/app/docs/page.tsx
index ffaa9ad..cbcb2f5 100644
--- a/apps/site/app/docs/page.tsx
+++ b/apps/site/app/docs/page.tsx
@@ -1,5 +1,6 @@
import type { Metadata } from "next";
import { Footer, Topbar } from "@/components/chrome";
+import { Sidebar } from "@/components/sidebar";
import { LINKS } from "@/lib/figures";
import { highlight } from "@/lib/highlight";
import { tiny } from "@/lib/size";
@@ -30,6 +31,7 @@ export default async function Docs() {
<>
<Topbar />
<div className="docs">
+ <Sidebar />
<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>
diff --git a/apps/site/app/globals.css b/apps/site/app/globals.css
index 11ea910..480a990 100644
--- a/apps/site/app/globals.css
+++ b/apps/site/app/globals.css
@@ -33,7 +33,8 @@
--code-token-link: #6b6b6b;
}
-html { color-scheme: light; scroll-behavior: smooth; }
+html { color-scheme: light; }
+@media (prefers-reduced-motion: no-preference) { html { scroll-behavior: smooth; } }
body { min-height: 100vh; color: var(--color-ink); background: var(--color-canvas); }
em { font-style: italic; }
@@ -103,7 +104,7 @@ em { font-style: italic; }
.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 { --gutter: clamp(18px, 5vw, 28px); --doc-offset: calc(var(--topbar) + 60px); 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); }
@@ -116,6 +117,26 @@ em { font-style: italic; }
.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 sidebar: the same links twice, grouped in a column from 1024px, one sideways strip under the top bar below it. */
+.doc-link { display: flex; align-items: center; height: 32px; padding: 0 10px; border-radius: 8px; font-size: 14px; color: var(--color-muted); white-space: nowrap; transition: background-color 150ms ease, color 150ms ease; }
+.doc-link:hover { color: var(--color-ink); }
+.doc-link[aria-current] { color: var(--color-ink); background: var(--color-stone); }
+.doc-sidebar { display: none; }
+.doc-group-label { margin-bottom: 4px; padding: 0 10px; font-size: 12px; font-weight: 500; color: var(--color-muted); }
+.doc-strip { position: sticky; top: var(--topbar); z-index: 9; margin: 0 calc(-1 * var(--gutter)); background: color-mix(in srgb, var(--color-canvas) 82%, transparent); backdrop-filter: blur(12px); }
+.doc-strip-scroll { overflow-x: auto; padding: 6px var(--gutter); scrollbar-width: none; }
+.doc-strip-scroll::-webkit-scrollbar { display: none; }
+.doc-strip-scroll ul { display: flex; gap: 2px; width: max-content; }
+.doc-strip-scroll[data-start] { mask-image: linear-gradient(to right, transparent, #000 24px); }
+.doc-strip-scroll[data-end] { mask-image: linear-gradient(to right, #000 calc(100% - 24px), transparent); }
+.doc-strip-scroll[data-start][data-end] { mask-image: linear-gradient(to right, transparent, #000 24px, #000 calc(100% - 24px), transparent); }
+@media (min-width: 1024px) {
+ .docs { --doc-offset: calc(var(--topbar) + 24px); display: grid; grid-template-columns: 220px minmax(0, 1fr); column-gap: 56px; padding-top: 64px; }
+ .docs-main { padding-top: 0; }
+ .doc-strip { display: none; }
+ .doc-sidebar { display: grid; gap: 24px; align-self: start; position: sticky; top: calc(var(--topbar) + 24px); }
+}
+
/* 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; }
.hero-title em { font-family: var(--font-serif); font-size: 1.08em; font-weight: 400; letter-spacing: -0.02em; }
@@ -199,7 +220,6 @@ em { font-style: italic; }
.props[data-tokens] td:nth-child(3) { flex-basis: 100%; margin-top: 6px; }
}
@media (prefers-reduced-motion: reduce) {
- html { scroll-behavior: auto; }
.hero-rise > *, .enter, .enter-hint { animation-name: fade; }
.slide[data-ready] .slide-hl { transition: none; }
.icopy-b, .icopy[data-copied] .icopy-a { transform: none; filter: none; }
diff --git a/apps/site/components/sidebar.tsx b/apps/site/components/sidebar.tsx
new file mode 100644
index 0000000..dbac5be
--- /dev/null
+++ b/apps/site/components/sidebar.tsx
@@ -0,0 +1,100 @@
+"use client";
+
+import { useEffect, useRef, useState } from "react";
+import { SECTIONS, type Group } from "@/lib/docs";
+
+const GROUPS = [...new Set(SECTIONS.map((s) => s.group))] as Group[];
+const LAST = SECTIONS[SECTIONS.length - 1].id;
+
+/**
+ * The docs' anchors: grouped in a column beside the page from 1024px, one
+ * sideways strip under the top bar below it. Both are plain links, so they
+ * work without JavaScript; the script only marks the section in view, which
+ * is the first one, in page order, inside a band from under the sticky
+ * chrome to 30% down the viewport.
+ */
+export function Sidebar() {
+ const [active, setActive] = useState(SECTIONS[0].id);
+ const [edges, setEdges] = useState({ start: false, end: false });
+ const strip = useRef<HTMLDivElement>(null);
+
+ useEffect(() => {
+ const sections = SECTIONS.map((s) => document.getElementById(s.id)).filter((el): el is HTMLElement => el !== null);
+ if (!sections.length) return;
+ // the band starts where a section lands after a click: its scroll-margin-top, under the top bar and on a phone the strip
+ const offset = parseFloat(getComputedStyle(sections[0]).scrollMarginTop) || 0;
+ const inBand = new Set<string>();
+ const pick = () => {
+ const end = window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 2;
+ // the last section is too short to reach the band, so the end of the page marks it
+ if (end) return setActive(LAST);
+ const first = SECTIONS.find((s) => inBand.has(s.id));
+ if (first) setActive(first.id);
+ };
+ const observer = new IntersectionObserver(
+ (entries) => {
+ for (const entry of entries) {
+ if (entry.isIntersecting) inBand.add(entry.target.id);
+ else inBand.delete(entry.target.id);
+ }
+ pick();
+ },
+ { rootMargin: `-${offset}px 0px -70% 0px` },
+ );
+ for (const el of sections) observer.observe(el);
+ window.addEventListener("scroll", pick, { passive: true });
+ return () => {
+ observer.disconnect();
+ window.removeEventListener("scroll", pick);
+ };
+ }, []);
+
+ useEffect(() => {
+ const el = strip.current;
+ if (!el) return;
+ const measure = () => setEdges({ start: el.scrollLeft > 1, end: el.scrollLeft + el.clientWidth < el.scrollWidth - 1 });
+ measure();
+ el.addEventListener("scroll", measure, { passive: true });
+ const resize = new ResizeObserver(measure);
+ resize.observe(el);
+ return () => {
+ el.removeEventListener("scroll", measure);
+ resize.disconnect();
+ };
+ }, []);
+
+ // Brings the marked item into the strip by scrolling the strip alone. scrollIntoView would also scroll the
+ // page, and would cut short the smooth scroll a click on the strip has just started.
+ useEffect(() => {
+ const el = strip.current;
+ const item = el?.querySelector<HTMLElement>(`a[href="#${active}"]`);
+ if (!el || !item || !el.clientWidth) return;
+ const pad = 24;
+ if (item.offsetLeft - pad < el.scrollLeft) el.scrollTo({ left: item.offsetLeft - pad });
+ else if (item.offsetLeft + item.offsetWidth + pad > el.scrollLeft + el.clientWidth) el.scrollTo({ left: item.offsetLeft + item.offsetWidth + pad - el.clientWidth });
+ }, [active]);
+
+ const link = (id: string, title: string) => (
+ <li key={id}>
+ <a href={`#${id}`} className="doc-link" aria-current={id === active ? "location" : undefined}>{title}</a>
+ </li>
+ );
+
+ return (
+ <>
+ <nav aria-label="Docs" className="doc-sidebar">
+ {GROUPS.map((group) => (
+ <div key={group} className="doc-group">
+ <p className="doc-group-label">{group}</p>
+ <ul>{SECTIONS.filter((s) => s.group === group).map((s) => link(s.id, s.title))}</ul>
+ </div>
+ ))}
+ </nav>
+ <nav aria-label="Docs" className="doc-strip">
+ <div ref={strip} className="doc-strip-scroll" data-start={edges.start || undefined} data-end={edges.end || undefined}>
+ <ul>{SECTIONS.map((s) => link(s.id, s.title))}</ul>
+ </div>
+ </nav>
+ </>
+ );
+}
diff --git a/apps/site/test/site.spec.ts b/apps/site/test/site.spec.ts
index e545307..f07fabd 100644
--- a/apps/site/test/site.spec.ts
+++ b/apps/site/test/site.spec.ts
@@ -326,3 +326,82 @@ test("the figures' link goes to the home's figure", async ({ page }) => {
await expect(page).toHaveURL(/\/#try$/);
await expect(page.locator("#try [data-inspector]")).toBeInViewport();
});
+
+test("the sidebar's links land on their section under the top bar and mark it, and scrolling moves the mark", async ({ page }) => {
+ await page.goto("/docs");
+ const nav = page.getByRole("navigation", { name: "Docs" });
+ await expect(nav.getByRole("link")).toHaveText(["Install", "Quick start", "Options", "React", "Vanilla", "CDN", "Figures", "Theme", "Accessibility"]);
+ await expect(nav.getByRole("link", { name: "Install" })).toHaveAttribute("aria-current", "location");
+
+ await nav.getByRole("link", { name: "Theme" }).click();
+ await expect(page).toHaveURL(/\/docs#theme$/);
+ await expect(nav.getByRole("link", { name: "Theme" })).toHaveAttribute("aria-current", "location");
+ await expect(nav.locator("[aria-current]")).toHaveCount(1);
+ await expect.poll(() => page.locator("#theme-title").evaluate((el) => el.getBoundingClientRect().top)).toBeGreaterThanOrEqual(54);
+
+ await page.evaluate(() => document.getElementById("figures")!.scrollIntoView());
+ await expect(nav.getByRole("link", { name: "Figures" })).toHaveAttribute("aria-current", "location");
+
+ // Accessibility is too short to reach the band; the end of the page marks it
+ await page.evaluate(() => window.scrollTo(0, document.documentElement.scrollHeight));
+ await expect(nav.getByRole("link", { name: "Accessibility" })).toHaveAttribute("aria-current", "location");
+});
+
+test("opening /docs#theme lands on Theme, clear of the top bar, with Theme marked", async ({ page }) => {
+ await page.goto("/docs#theme");
+ const nav = page.getByRole("navigation", { name: "Docs" });
+ await expect(nav.getByRole("link", { name: "Theme" })).toHaveAttribute("aria-current", "location");
+ const top = await page.locator("#theme-title").evaluate((el) => el.getBoundingClientRect().top);
+ expect(top).toBeGreaterThanOrEqual(54);
+ expect(top).toBeLessThan(200);
+});
+
+test("on a phone the sidebar is one strip under the top bar, and it follows the reader", async ({ page }) => {
+ await page.setViewportSize({ width: 390, height: 800 });
+ await page.goto("/docs");
+ const strip = page.locator(".doc-strip");
+ const scroller = strip.locator(".doc-strip-scroll");
+ await expect(strip).toBeVisible();
+ await expect(page.locator(".doc-sidebar")).toBeHidden();
+ expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(390);
+ await expect(scroller).toHaveAttribute("data-end");
+ await expect(scroller).not.toHaveAttribute("data-start");
+
+ const inside = async (name: string) => {
+ const s = (await scroller.boundingBox())!;
+ const b = (await strip.getByRole("link", { name }).boundingBox())!;
+ return b.x >= s.x - 1 && b.x + b.width <= s.x + s.width + 1;
+ };
+ // the page moves, not the strip: the strip has to bring Accessibility in by itself
+ await page.evaluate(() => window.scrollTo(0, document.documentElement.scrollHeight));
+ await expect(strip.getByRole("link", { name: "Accessibility" })).toHaveAttribute("aria-current", "location");
+ await expect.poll(() => inside("Accessibility")).toBe(true);
+ await expect(scroller).toHaveAttribute("data-start");
+
+ await strip.getByRole("link", { name: "Install" }).click();
+ await expect(page).toHaveURL(/#install$/);
+ await expect(strip.getByRole("link", { name: "Install" })).toHaveAttribute("aria-current", "location");
+ await expect.poll(() => inside("Install")).toBe(true);
+ // the heading clears the top bar and the strip
+ await expect.poll(async () => {
+ const s = (await strip.boundingBox())!;
+ const t = (await page.locator("#install-title").boundingBox())!;
+ return t.y >= s.y + s.height;
+ }).toBe(true);
+ expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(390);
+});
+
+test.describe("without JavaScript", () => {
+ test.use({ javaScriptEnabled: false });
+
+ test("the sidebar is plain links that still move the page, and the quick start shows React", async ({ page }) => {
+ await page.goto("/docs");
+ const nav = page.getByRole("navigation", { name: "Docs" });
+ await expect(nav.getByRole("link", { name: "Theme" })).toHaveAttribute("href", "#theme");
+ await nav.getByRole("link", { name: "Theme" }).click();
+ await expect(page).toHaveURL(/\/docs#theme$/);
+ const quick = page.locator("#quick-start");
+ await expect(quick.getByRole("tab", { name: "React" })).toHaveAttribute("aria-selected", "true");
+ await expect(quick.locator(".code-title")).toHaveText("app/page.tsx");
+ });
+});