An open source design system that's fully customizable and agent ready
TypeScript
fix(docsite): expose sidebar search before hydration (#7178)
Xinzi Zhou13d926e28370
diff --git a/apps/docsite/e2e/mobile-navigation.spec.ts b/apps/docsite/e2e/mobile-navigation.spec.tsindex 7cd585c5ef..e189105521 100644--- a/apps/docsite/e2e/mobile-navigation.spec.ts+++ b/apps/docsite/e2e/mobile-navigation.spec.ts@@ -4,7 +4,7 @@* @file mobile-navigation.spec.ts* @input Production docsite HTML/CSS, with hydration held then released* @output Regression proof for initial navigation and its hydration handoff- * @position Docsite browser tests; jsdom cannot measure the rendered icon+ * @position Docsite browser tests; jsdom cannot measure icons or sticky occlusion*/import {expect, test} from '@playwright/test';@@ -67,19 +67,85 @@ test('the mobile hamburger survives hydration without changing icon size or draw}});-test('desktop navigation remains available at exactly 768px before hydration', async ({+test('desktop search stays exposed across hydration at exactly 768px', async ({page,}) => {await page.setViewportSize({width: 768, height: 844});- await page.route('**/_next/**/*.js*', route => route.abort());- await page.goto('/components', {waitUntil: 'load'});+ let resumeHydration = () => {};+ const hydration = new Promise<void>(resolve => {+ resumeHydration = resolve;+ });+ await page.route('**/_next/**/*.js*', async route => {+ await hydration;+ await route.continue();+ });- const nav = page.getByRole('navigation', {name: 'Astryx navigation'});- await expect(nav.getByRole('button', {name: 'Open navigation'})).toBeHidden();- await expect(- nav.getByRole('link', {name: 'Components', exact: true}),- ).toBeVisible();- await expect(- page.getByRole('textbox', {name: 'Search components'}),- ).toBeVisible();+ try {+ await page.goto('/components', {waitUntil: 'commit'});+ await page.waitForFunction(+ () => performance.getEntriesByName('first-contentful-paint').length > 0,+ );+ const nav = page.getByRole('navigation', {name: 'Astryx navigation'});+ await expect(+ nav.getByRole('button', {name: 'Open navigation'}),+ ).toBeHidden();+ await expect(+ nav.getByRole('link', {name: 'Components', exact: true}),+ ).toBeVisible();+ const search = page.getByRole('textbox', {name: 'Search components'});+ await expect(search).toBeVisible();+ // With no pending fonts, fonts.ready can wait for the held scripts' load+ // event even though geometry is settled (for example with offline fonts).+ await page.evaluate(async () => {+ if (document.fonts.status === 'loading') {+ await document.fonts.ready;+ }+ });++ // A reload can restore scroll before hydration. Visibility alone misses+ // the real input being painted underneath the sticky header.+ await page.evaluate(() => window.scrollTo(0, 700));+ const isSearchExposed = () =>+ search.evaluate(input => {+ const box = input.getBoundingClientRect();+ return (+ document.elementFromPoint(+ box.x + box.width / 2,+ box.y + box.height / 2,+ ) === input+ );+ });+ await expect.poll(isSearchExposed).toBe(true);+ const initialBox = await search.evaluate(input => {+ const {x, y, width, height} = input.getBoundingClientRect();+ return {x, y, width, height};+ });+ const headerBottom = await nav.evaluate(+ element => element.getBoundingClientRect().bottom,+ );+ expect(initialBox.width).toBeGreaterThan(0);+ expect(initialBox.height).toBeGreaterThan(0);+ expect(initialBox.y).toBeGreaterThanOrEqual(headerBottom);+ expect(initialBox.y + initialBox.height).toBeLessThan(844);++ resumeHydration();+ // Only the existing layout effect writes this inline value; the bootstrap+ // writes a stylesheet, so this proves the header has hydrated.+ await page.waitForFunction(+ () =>+ document.documentElement.style.getPropertyValue(+ '--appshell-header-height',+ ) !== '',+ );+ expect(await search.boundingBox()).toEqual(initialBox);++ const banner = page.getByRole('alert').filter({hasText: 'unreleased docs'});+ if (await banner.count()) {+ await banner.getByRole('button', {name: /^Dismiss/}).click();+ await expect(banner).toHaveCount(0);+ await expect.poll(isSearchExposed).toBe(true);+ }+ } finally {+ resumeHydration();+ }});diff --git a/apps/docsite/src/app/globals.css b/apps/docsite/src/app/globals.cssindex 1ee2340d02..17c91d2ecf 100644--- a/apps/docsite/src/app/globals.css+++ b/apps/docsite/src/app/globals.css@@ -92,6 +92,15 @@ body:has([data-home-page]) #astryx-app-shell-main {padding-top: 0;}+/* The docs sidebar must clear the whole header even before hydration, including+ on a reload with restored scroll. DocsShell seeds this docsite-owned variable+ before paint; the existing header observer keeps it current afterwards. Do not+ read or write AppShell's private measurement property, or affect preview shells. */+.astryx-docs-shell :has(> .astryx-app-shell-sidenav):where(:not(.astryx-app-shell .astryx-app-shell *)) {+ top: var(--appshell-header-height);+ height: calc(100dvh - var(--appshell-header-height));+}+/* The docs/components sidebar is a desktop affordance. AppShell renders itserver-side (desktop HTML), so below the mobile breakpoint we hide it withpure CSS — it never paints on a phone, no JS flip. The :where(:not(...))diff --git a/apps/docsite/src/components/DocsShell.tsx b/apps/docsite/src/components/DocsShell.tsxindex eb059acac4..e6a18f308a 100644--- a/apps/docsite/src/components/DocsShell.tsx+++ b/apps/docsite/src/components/DocsShell.tsx@@ -2,6 +2,13 @@'use client';+/**+ * @file DocsShell.tsx+ * @input Route metadata, component registry, and the rendered AppShell header+ * @output Docs navigation with a stable sidebar offset before hydration+ * @position Docsite-only shell and first-paint header measurement+ */+import {useState, useMemo} from 'react';import {usePathname} from 'next/navigation';import {Search, FlaskConical} from 'lucide-react';@@ -56,6 +63,24 @@ const foundationsSort = (a: DocTopic, b: DocTopic) => {return a.title.localeCompare(b.title);};+// Seed the full header height while parsing, before restored scroll can put+// the sidebar under it, and refresh once its fonts settle. CSSOM leaves React's+// DOM unchanged; the existing observer's inline value takes over after hydration.+const initialHeaderHeightScript = `(() => {+ const script = document.currentScript;+ const header = script?.closest('.astryx-app-shell-header');+ const style = script?.previousElementSibling;+ const sheet = style?.sheet;+ const publish = () => {+ if (!header?.isConnected || !style?.isConnected || !sheet) return;+ if (sheet.cssRules.length === 0) sheet.insertRule(':root {}');+ sheet.cssRules[0].style.setProperty('--appshell-header-height',+ header.getBoundingClientRect().height + 'px');+ };+ publish();+ document.fonts.ready.then(publish);+})();`;+// ── Shell ──────────────────────────────────────────────────────────────export function DocsShell({children, packages, docTopics}: DocsShellProps) {@@ -157,10 +182,21 @@ export function DocsShell({children, packages, docTopics}: DocsShellProps) {return (<AppShell+ className="astryx-docs-shell"variant="surface"height="auto"banner={CURRENT_TARGET === 'canary' ? <CanaryBanner /> : undefined}- topNav={<SharedTopNav />}+ topNav={+ <>+ <SharedTopNav />+ {/* Parser-blocking measurement precedes all sidebar markup, including+ when the response is streamed. Stylesheets above it are loaded. */}+ <style />+ <script+ dangerouslySetInnerHTML={{__html: initialHeaderHeightScript}}+ />+ </>+ }sideNav={<SideNav topContent={isOnComponentsRoute ? componentSearch : undefined}>{!isOnComponentsRoute && (