Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

Fix/docsite topnav flicker (#3627)

Joseph87f7bd68dc52
diff --git a/apps/docsite/src/app/(site)/layout.tsx b/apps/docsite/src/app/(site)/layout.tsx
index 06e0f0d39c..24b2b06e7e 100644
--- a/apps/docsite/src/app/(site)/layout.tsx
+++ b/apps/docsite/src/app/(site)/layout.tsx
@@ -14,7 +14,11 @@ export default async function MarketingLayout({
const year = await getCopyrightYear();
return (
- <AppShell variant="surface" height="auto" topNav={<SharedTopNav />}>
+ <AppShell
+ variant="surface"
+ height="auto"
+ mobileNav={false}
+ topNav={<SharedTopNav />}>
<div className={styles.shell}>
<div className={styles.main}>{children}</div>
<div className={styles.footer}>
diff --git a/apps/docsite/src/app/blog/layout.tsx b/apps/docsite/src/app/blog/layout.tsx
index d8cb221986..42f8df939c 100644
--- a/apps/docsite/src/app/blog/layout.tsx
+++ b/apps/docsite/src/app/blog/layout.tsx
@@ -19,7 +19,11 @@ export default async function BlogLayout({
const year = await getCopyrightYear();
return (
- <AppShell variant="surface" height="auto" topNav={<SharedTopNav />}>
+ <AppShell
+ variant="surface"
+ height="auto"
+ mobileNav={false}
+ topNav={<SharedTopNav />}>
{children}
<SiteFooter year={year} />
</AppShell>
diff --git a/apps/docsite/src/app/not-found.tsx b/apps/docsite/src/app/not-found.tsx
index 04b18bd6cd..76a7598238 100644
--- a/apps/docsite/src/app/not-found.tsx
+++ b/apps/docsite/src/app/not-found.tsx
@@ -13,7 +13,11 @@ export default async function NotFound() {
const year = await getCopyrightYear();
return (
- <AppShell variant="surface" height="fill" topNav={<SharedTopNav />}>
+ <AppShell
+ variant="surface"
+ height="fill"
+ mobileNav={false}
+ topNav={<SharedTopNav />}>
<div className={styles.shell}>
<div className={styles.content}>
<Center axis="both" height="100%">
diff --git a/apps/docsite/src/components/SharedTopNav.tsx b/apps/docsite/src/components/SharedTopNav.tsx
index cbf0539eae..84009b23a6 100644
--- a/apps/docsite/src/components/SharedTopNav.tsx
+++ b/apps/docsite/src/components/SharedTopNav.tsx
@@ -4,10 +4,18 @@
import {useEffect, useState} from 'react';
import {usePathname} from 'next/navigation';
-import {TopNav, TopNavHeading, TopNavItem} from '@astryxdesign/core/TopNav';
+import * as stylex from '@stylexjs/stylex';
+import {
+ TopNav,
+ TopNavHeading,
+ TopNavItem,
+ TopNavRenderContext,
+} from '@astryxdesign/core/TopNav';
+import {MobileNav} from '@astryxdesign/core/MobileNav';
import {Button} from '@astryxdesign/core/Button';
import {HStack} from '@astryxdesign/core/Layout';
-import {Search, HeartHandshake, Sun, Moon} from 'lucide-react';
+import {spacingVars} from '@astryxdesign/core/theme/tokens.stylex';
+import {Search, HeartHandshake, Sun, Moon, Menu} from 'lucide-react';
import {GITHUB_REPO} from '../constants';
import {AstryxIcon} from './logos';
import {SearchPalette} from './SearchPalette';
@@ -35,10 +43,70 @@ const GitHubIcon = ({
</svg>
);
+// Responsive helpers. The desktop links and the mobile hamburger both live in
+// the DOM at all times; a pure CSS @media query decides which is visible so the
+// server-rendered HTML is correct on first paint (no post-hydration flip).
+const MOBILE_BREAKPOINT = '@media (max-width: 768px)';
+
+const styles = stylex.create({
+ desktopNav: {
+ display: {
+ default: 'flex',
+ [MOBILE_BREAKPOINT]: 'none',
+ },
+ alignItems: 'center',
+ gap: spacingVars['--spacing-1'],
+ },
+ mobileToggle: {
+ display: {
+ default: 'none',
+ [MOBILE_BREAKPOINT]: 'flex',
+ },
+ alignItems: 'center',
+ },
+ drawerItems: {
+ display: 'flex',
+ flexDirection: 'column',
+ gap: spacingVars['--spacing-0-5'],
+ },
+ // Theme-toggle icons. Both Moon and Sun are always in the DOM; while the mode
+ // is still unresolved ('system'), a pure CSS prefers-color-scheme query decides
+ // which one shows so the first paint matches the OS — otherwise the icon starts
+ // as Moon (resolvedMode's 'light' default) and visibly swaps to Sun on a
+ // dark-OS machine after hydration. Once the mode resolves to a concrete value
+ // (OS-detected or a manual toggle) React forces the icon explicitly; for the
+ // OS-following case that matches what the media query already showed, so
+ // nothing visibly changes.
+ moonWhenSystem: {
+ display: {
+ default: 'inline-flex',
+ '@media (prefers-color-scheme: dark)': 'none',
+ },
+ },
+ sunWhenSystem: {
+ display: {
+ default: 'none',
+ '@media (prefers-color-scheme: dark)': 'inline-flex',
+ },
+ },
+ iconShown: {display: 'inline-flex'},
+ iconHidden: {display: 'none'},
+});
+
+// Primary navigation links, shared by the desktop bar and the mobile drawer.
+const NAV_ITEMS = [
+ {key: 'docs', label: 'Docs', href: '/docs/getting-started'},
+ {key: 'components', label: 'Components', href: '/components'},
+ {key: 'templates', label: 'Templates', href: '/templates'},
+ {key: 'themes', label: 'Themes', href: '/themes'},
+ {key: 'playground', label: 'Playground', href: '/playground'},
+] as const;
+
export function SharedTopNav() {
const [isSearchOpen, setIsSearchOpen] = useState(false);
+ const [isMenuOpen, setIsMenuOpen] = useState(false);
const pathname = usePathname();
- const {mode, toggleMode} = useThemeMode();
+ const {mode, themeMode, toggleMode} = useThemeMode();
useEffect(() => {
const onKeyDown = (event: KeyboardEvent) => {
@@ -104,33 +172,16 @@ export function SharedTopNav() {
/>
}
centerContent={
- <>
- <TopNavItem
- label="Docs"
- href="/docs/getting-started"
- isSelected={getActiveItem() === 'docs'}
- />
- <TopNavItem
- label="Components"
- href="/components"
- isSelected={getActiveItem() === 'components'}
- />
- <TopNavItem
- label="Templates"
- href="/templates"
- isSelected={getActiveItem() === 'templates'}
- />
- <TopNavItem
- label="Themes"
- href="/themes"
- isSelected={getActiveItem() === 'themes'}
- />
- <TopNavItem
- label="Playground"
- href="/playground"
- isSelected={getActiveItem() === 'playground'}
- />
- </>
+ <div {...stylex.props(styles.desktopNav)}>
+ {NAV_ITEMS.map(item => (
+ <TopNavItem
+ key={item.key}
+ label={item.label}
+ href={item.href}
+ isSelected={getActiveItem() === item.key}
+ />
+ ))}
+ </div>
}
endContent={
<HStack gap={2}>
@@ -159,7 +210,30 @@ export function SharedTopNav() {
}
variant="ghost"
isIconOnly
- icon={mode === 'light' ? <Moon size={20} /> : <Sun size={20} />}
+ icon={
+ <>
+ <Moon
+ size={20}
+ {...stylex.props(
+ themeMode === 'system'
+ ? styles.moonWhenSystem
+ : mode === 'light'
+ ? styles.iconShown
+ : styles.iconHidden,
+ )}
+ />
+ <Sun
+ size={20}
+ {...stylex.props(
+ themeMode === 'system'
+ ? styles.sunWhenSystem
+ : mode === 'dark'
+ ? styles.iconShown
+ : styles.iconHidden,
+ )}
+ />
+ </>
+ }
onClick={toggleMode}
/>
<Button
@@ -188,6 +262,16 @@ export function SharedTopNav() {
trackClickCta({page: 'landing', target: 'get_started'})
}
/>
+ <div {...stylex.props(styles.mobileToggle)}>
+ <Button
+ label="Open menu"
+ tooltip="Menu"
+ variant="ghost"
+ isIconOnly
+ icon={<Menu size={20} />}
+ onClick={() => setIsMenuOpen(true)}
+ />
+ </div>
</HStack>
}
/>
@@ -199,6 +283,34 @@ export function SharedTopNav() {
docTopics={docTopics}
templates={templates}
/>
+ <MobileNav
+ isOpen={isMenuOpen}
+ onOpenChange={setIsMenuOpen}
+ side="end"
+ label="Astryx navigation"
+ header={
+ <AstryxIcon
+ width={24}
+ height={24}
+ role="img"
+ aria-label="Astryx"
+ style={{display: 'block', color: 'var(--color-brand)'}}
+ />
+ }>
+ <TopNavRenderContext value="drawer">
+ <div {...stylex.props(styles.drawerItems)}>
+ {NAV_ITEMS.map(item => (
+ <TopNavItem
+ key={item.key}
+ label={item.label}
+ href={item.href}
+ isSelected={getActiveItem() === item.key}
+ onClick={() => setIsMenuOpen(false)}
+ />
+ ))}
+ </div>
+ </TopNavRenderContext>
+ </MobileNav>
</>
);
}