Skip to content

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

Docs: a click in the sidebar holds its mark for the whole glide, not only its first second

Lucas Marques43fa1e51d874
diff --git a/apps/site/components/rail.tsx b/apps/site/components/rail.tsx
index 42b9da8..8d750b8 100644
--- a/apps/site/components/rail.tsx
+++ b/apps/site/components/rail.tsx
@@ -138,6 +138,16 @@ export function Rail({ label, groups }: { label: string; groups: RailGroup[] })
held.current = 0;
pick();
};
+ // a long glide outlasts the first second: while the page still moves, the mark stays held
+ const scrolled = () => {
+ if (held.current) {
+ held.current = Math.max(held.current, performance.now() + 200);
+ clearTimeout(timer);
+ timer = window.setTimeout(release, held.current - performance.now());
+ return;
+ }
+ pick();
+ };
const hold = (event: MouseEvent) => {
const a = (event.target as Element | null)?.closest<HTMLAnchorElement>("a[href^='#']");
const id = a?.hash.slice(1);
@@ -148,7 +158,7 @@ export function Rail({ label, groups }: { label: string; groups: RailGroup[] })
timer = window.setTimeout(release, 1000);
};
pick();
- window.addEventListener("scroll", pick, { passive: true });
+ window.addEventListener("scroll", scrolled, { passive: true });
window.addEventListener("scrollend", release);
window.addEventListener("resize", pick);
document.addEventListener("click", hold);
@@ -156,7 +166,7 @@ export function Rail({ label, groups }: { label: string; groups: RailGroup[] })
document.documentElement.dataset.smooth = "";
return () => {
clearTimeout(timer);
- window.removeEventListener("scroll", pick);
+ window.removeEventListener("scroll", scrolled);
window.removeEventListener("scrollend", release);
window.removeEventListener("resize", pick);
document.removeEventListener("click", hold);
diff --git a/apps/site/test/site.spec.ts b/apps/site/test/site.spec.ts
index 8215d1b..985a8a7 100644
--- a/apps/site/test/site.spec.ts
+++ b/apps/site/test/site.spec.ts
@@ -742,6 +742,32 @@ test("a click in the sidebar still scrolls smoothly to its section", async ({ pa
await expect.poll(() => page.evaluate(() => scrollY)).toBeGreaterThan(final - 2);
});
+test("a glide longer than a second keeps the mark on the link picked, every frame of the way", async ({ page }) => {
+ await page.goto("/docs");
+ const nav = page.locator(".doc-sidebar");
+ await nav.getByRole("link", { name: "Install" }).waitFor();
+ // every frame from the click's mark on, the marked link, until the page has come to rest
+ const watching = page.evaluate(() => new Promise<{ marks: string[]; ms: number }>((done) => {
+ const marks = new Set<string>();
+ let t0 = 0, last = -1, still = 0;
+ const tick = () => {
+ const on = document.querySelector(".doc-sidebar a[aria-current]")?.textContent;
+ if (on === "Theme" && !t0) t0 = performance.now();
+ if (t0 && on) marks.add(on);
+ still = scrollY === last ? still + 1 : 0;
+ last = scrollY;
+ if (still > 30 && scrollY > 0) done({ marks: [...marks], ms: performance.now() - t0 });
+ else requestAnimationFrame(tick);
+ };
+ tick();
+ }));
+ await nav.getByRole("link", { name: "Theme" }).click();
+ const { marks, ms } = await watching;
+ // Install to Theme is a long way: the glide outlasts the second a click used to hold the mark for
+ expect(ms).toBeGreaterThan(1000);
+ expect(marks).toEqual(["Theme"]);
+});
+
test("the top bar's Inspo link opens the story of how Hairline was made, with a clean console", async ({ page }) => {
const noise = watch(page);
await page.goto("/");