Skip to content

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

Docs: a deep link lands on its section at once while sidebar clicks still glide, and the top of the page marks Install again

Lucas Marques8f1040a95f5b
diff --git a/apps/site/app/globals.css b/apps/site/app/globals.css
index 480a990..d8d2921 100644
--- a/apps/site/app/globals.css
+++ b/apps/site/app/globals.css
@@ -34,7 +34,8 @@
}
html { color-scheme: light; }
-@media (prefers-reduced-motion: no-preference) { html { scroll-behavior: smooth; } }
+/* smooth only once the docs' sidebar has mounted (it sets data-smooth), so a deep link lands at once instead of sweeping down from the top */
+@media (prefers-reduced-motion: no-preference) { html[data-smooth] { scroll-behavior: smooth; } }
body { min-height: 100vh; color: var(--color-ink); background: var(--color-canvas); }
em { font-style: italic; }
diff --git a/apps/site/components/sidebar.tsx b/apps/site/components/sidebar.tsx
index dbac5be..53e87f4 100644
--- a/apps/site/components/sidebar.tsx
+++ b/apps/site/components/sidebar.tsx
@@ -30,6 +30,8 @@ export function Sidebar() {
if (end) return setActive(LAST);
const first = SECTIONS.find((s) => inBand.has(s.id));
if (first) setActive(first.id);
+ // above the first section nothing is in the band, so Install is active again, as on load
+ else if (sections[0].getBoundingClientRect().top >= window.innerHeight * 0.3) setActive(SECTIONS[0].id);
};
const observer = new IntersectionObserver(
(entries) => {
@@ -43,9 +45,12 @@ export function Sidebar() {
);
for (const el of sections) observer.observe(el);
window.addEventListener("scroll", pick, { passive: true });
+ // the browser has already scrolled to a deep link's section, at once; the reader's own clicks from here on glide
+ document.documentElement.dataset.smooth = "";
return () => {
observer.disconnect();
window.removeEventListener("scroll", pick);
+ delete document.documentElement.dataset.smooth;
};
}, []);
diff --git a/apps/site/test/site.spec.ts b/apps/site/test/site.spec.ts
index f07fabd..71963ea 100644
--- a/apps/site/test/site.spec.ts
+++ b/apps/site/test/site.spec.ts
@@ -405,3 +405,30 @@ test.describe("without JavaScript", () => {
await expect(quick.locator(".code-title")).toHaveText("app/page.tsx");
});
});
+
+test("a deep link lands on its section at once, without sweeping down the page", async ({ page }) => {
+ await page.goto("/docs#theme");
+ // read at once, not polled: a smooth scroll from the top would still be under way
+ const top = await page.locator("#theme-title").evaluate((el) => el.getBoundingClientRect().top);
+ expect(top).toBeGreaterThanOrEqual(54);
+ expect(top).toBeLessThan(200);
+});
+
+test("back at the top of the page, Install is marked again", async ({ page }) => {
+ await page.goto("/docs");
+ const nav = page.getByRole("navigation", { name: "Docs" });
+ await page.evaluate(() => document.getElementById("theme")!.scrollIntoView({ behavior: "instant" }));
+ await expect(nav.getByRole("link", { name: "Theme" })).toHaveAttribute("aria-current", "location");
+ // an instant jump, as Back makes under reduced motion: no section passes through the band on the way
+ await page.evaluate(() => window.scrollTo({ top: 0, behavior: "instant" }));
+ await expect(nav.getByRole("link", { name: "Install" })).toHaveAttribute("aria-current", "location");
+});
+
+test("a click in the sidebar still scrolls smoothly to its section", async ({ page }) => {
+ await page.goto("/docs");
+ await page.getByRole("navigation", { name: "Docs" }).getByRole("link", { name: "Theme" }).click();
+ const final = await page.locator("#theme").evaluate((el) => el.getBoundingClientRect().top + scrollY - parseFloat(getComputedStyle(el).scrollMarginTop));
+ // just after the click the page is still on its way
+ expect(await page.evaluate(() => scrollY)).toBeLessThan(final - 100);
+ await expect.poll(() => page.evaluate(() => scrollY)).toBeGreaterThan(final - 2);
+});