Skip to content

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

TypeScript

fix(docsite): eliminate flash of light theme on dark-mode load (#2713) (#2907)

Cindy Zhange6e06e393d48
diff --git a/apps/docsite/src/app/(site)/_landing/hero/HeroThemeReel.tsx b/apps/docsite/src/app/(site)/_landing/hero/HeroThemeReel.tsx
index 0b5b632dbb..78e4806d60 100644
--- a/apps/docsite/src/app/(site)/_landing/hero/HeroThemeReel.tsx
+++ b/apps/docsite/src/app/(site)/_landing/hero/HeroThemeReel.tsx
@@ -46,8 +46,12 @@ interface HeroReelState {
/** Whether entrance/cycle animation should run (false under reduced-motion). */
animate: boolean;
setPaused: (paused: boolean) => void;
- /** The docsite's active color mode (from the global theme toggle / OS). */
- userMode: 'light' | 'dark';
+ /**
+ * The docsite's color mode for theme rendering. 'system' on the first paint
+ * (before the OS preference resolves) so slides keep `color-scheme: light
+ * dark` and their light-dark() tokens follow the OS — no flash (#2713).
+ */
+ userMode: 'system' | 'light' | 'dark';
}
const HeroReelContext = createContext<HeroReelState | null>(null);
@@ -59,12 +63,13 @@ function useHeroReel(): HeroReelState | null {
/**
* The color mode a slide should render in: dark-first themes (e.g. Gothic) are
* always dark; every other theme follows the docsite's color mode so the hero
- * respects the user's light/dark toggle.
+ * respects the user's light/dark toggle (and the OS preference via 'system'
+ * before that resolves).
*/
function effectiveMode(
slide: HeroThemeSlide,
- userMode: 'light' | 'dark',
-): 'light' | 'dark' {
+ userMode: 'system' | 'light' | 'dark',
+): 'system' | 'light' | 'dark' {
return slide.isDark ? 'dark' : userMode;
}
@@ -72,6 +77,9 @@ function effectiveMode(
* Whether the active hero slide should render with light text/nav. True when
* the slide is a dark-first theme (e.g. Gothic) OR the docsite is in dark mode —
* in both cases the hero sits on a dark body and its text/nav must go light.
+ * 'system' is treated as not-dark here: the light-text overrides only apply once
+ * the resolved dark mode is known, which avoids forcing light ink on a slide
+ * that may still paint its light scheme.
*/
export function useHeroReelIsDark(): boolean {
const reel = useContext(HeroReelContext);
@@ -204,7 +212,7 @@ export function HeroReelProvider({children}: {children: ReactNode}) {
const [index, setIndex] = useState(0);
const [paused, setPaused] = useState(false);
const reduceMotion = useMediaQuery('(prefers-reduced-motion: reduce)');
- const {mode: userMode} = useThemeMode();
+ const {themeMode: userMode} = useThemeMode();
const goTo = useCallback(
(next: number) => {
diff --git a/apps/docsite/src/app/(site)/_landing/hero/heroThemeContent.ts b/apps/docsite/src/app/(site)/_landing/hero/heroThemeContent.ts
index 9ccdbb10ce..ee72d1cc50 100644
--- a/apps/docsite/src/app/(site)/_landing/hero/heroThemeContent.ts
+++ b/apps/docsite/src/app/(site)/_landing/hero/heroThemeContent.ts
@@ -11,9 +11,12 @@
*/
import type {XDSDefinedTheme} from '@xds/core/theme';
-import {packages} from '../../../../generated/packageRegistry';
-import {themeObjects} from '../../../../generated/themeRegistry';
-import {astryxTheme, BRAND_BLUE} from '../../../../themes/astryxTheme';
+import {packages} from '@/generated/packageRegistry';
+import {themeObjects} from '@/generated/themeRegistry';
+// Built theme (__built:true) so the hero reel's <XDSTheme> slide uses pre-built
+// CSS and skips runtime injection. BRAND_BLUE (logo-only) lives in @/constants.
+import {astryxTheme} from '@/themes/astryx';
+import {BRAND_BLUE} from '@/constants';
// Sentinel for the docsite's local brand theme (not an @xds/theme-* package).
const ASTRYX = 'astryx';
diff --git a/apps/docsite/src/app/(site)/page.tsx b/apps/docsite/src/app/(site)/page.tsx
index 13e9cae3fb..3fe11f87d2 100644
--- a/apps/docsite/src/app/(site)/page.tsx
+++ b/apps/docsite/src/app/(site)/page.tsx
@@ -11,7 +11,9 @@ import {XDSGrid} from '@xds/core/Grid';
import {XDSButton} from '@xds/core/Button';
import {XDSTheme} from '@xds/core/theme';
import {spacingVars} from '@xds/core/theme/tokens.stylex';
-import {astryxTheme} from '../../themes/astryxTheme';
+// Built theme (__built:true) so <XDSTheme> uses the pre-built CSS and skips
+// runtime style injection. Importing the source astryxTheme.ts re-triggers it.
+import {astryxTheme} from '@/themes/astryx';
import {
HeroReelProvider,
HeroReelCards,
diff --git a/apps/docsite/src/app/globals.css b/apps/docsite/src/app/globals.css
index d481bee81d..736af7a181 100644
--- a/apps/docsite/src/app/globals.css
+++ b/apps/docsite/src/app/globals.css
@@ -19,6 +19,15 @@
override real XDS semantic tokens). Each is a light-dark() pair so dark
mode is handled by the value itself, no consumer logic required. */
:root {
+ /* AppShell measures its header post-hydration and sets
+ --appshell-header-height on the shell element. Until that runs, every
+ consumer (the hero's fixed `top`, the nav backdrop, the docs content
+ padding) falls back to mismatched literals, so the top spacing visibly
+ pops in on load (#2713). Seed the auto-mode header height (--spacing-12 =
+ 48px) here so the very first paint already reserves the right offset; the
+ AppShell then overwrites it with the measured value (identical, no jump). */
+ --appshell-header-height: var(--spacing-12, 48px);
+
/* Feature card backdrop. Soft pastel blue in light mode, deep navy in
dark mode. Consumed by FeaturesShowcase's bento cards via
`backgroundColor: var(--xds-marketing-feature-card-bg)`. Picked
@@ -44,10 +53,9 @@
scrolling clears the nav, and add an actual padding-top to the main
element so the first paint already has the offset in place.
- The --appshell-header-height variable is populated by the AppShell once
- it measures the rendered header (post-hydration). The 64px fallback
- approximates the default XDS top-nav height so the layout reserves
- space immediately on the first paint. */
+ The --appshell-header-height variable is seeded at :root (above) and then
+ re-set by the AppShell once it measures the rendered header. The literal
+ fallbacks below only apply if the :root seed is ever missing. */
body:not(:has([data-playground-page])) #xds-app-shell-main {
scroll-padding-top: var(--appshell-header-height, 64px);
padding-top: var(--appshell-header-height, 64px);
diff --git a/apps/docsite/src/app/layout.tsx b/apps/docsite/src/app/layout.tsx
index 52b8006073..81e81e8d8e 100644
--- a/apps/docsite/src/app/layout.tsx
+++ b/apps/docsite/src/app/layout.tsx
@@ -23,7 +23,11 @@ export const metadata: Metadata = {
export default function RootLayout({children}: {children: React.ReactNode}) {
return (
- <html lang="en">
+ // Server-render data-xds-theme so the Astryx theme's @scope'd tokens (body
+ // color, top spacing) apply on the first paint. Deliberately no data-theme:
+ // reset.css then keeps `color-scheme: light dark`, so the light-dark()
+ // tokens follow the OS preference with no script and no flash (#2713).
+ <html lang="en" data-xds-theme="astryx">
<head>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link
diff --git a/apps/docsite/src/app/providers.tsx b/apps/docsite/src/app/providers.tsx
index 5945b39b71..d10457d9f8 100644
--- a/apps/docsite/src/app/providers.tsx
+++ b/apps/docsite/src/app/providers.tsx
@@ -11,10 +11,19 @@ import {astryxTheme} from '../themes/astryx';
type ThemeMode = 'light' | 'dark';
const ThemeModeContext = createContext<{
+ /** Resolved color mode for UI consumers (toggle icon, ColorSwatch). */
mode: ThemeMode;
+ /**
+ * Raw, system-aware mode for theme rendering. Stays 'system' on the first
+ * paint so nested <XDSTheme> scopes keep `color-scheme: light dark` and their
+ * light-dark() tokens follow the OS preference — no flash before the OS mode
+ * resolves (#2713).
+ */
+ themeMode: 'system' | ThemeMode;
toggleMode: () => void;
}>({
mode: 'light',
+ themeMode: 'system',
toggleMode: () => {},
});
@@ -23,11 +32,13 @@ export function useThemeMode() {
}
export function Providers({children}: {children: React.ReactNode}) {
- // Default to the OS color scheme. SSR/first paint use a deterministic 'light'
- // default (so hydration matches); the effect syncs to the real system
- // preference on mount and tracks live changes — until the user manually
- // toggles, after which their choice sticks for the rest of the session.
- const [mode, setMode] = useState<ThemeMode>('light');
+ // Start in 'system' so SSR and the first paint defer to the OS scheme via
+ // reset.css's `color-scheme: light dark` + light-dark() tokens — no flash,
+ // no script (#2713). The effect resolves it to a concrete 'light'/'dark'
+ // (visually identical) and tracks OS changes until the user toggles.
+ // A fully SSR-correct manual toggle would need a server-read cookie; left
+ // out of scope here.
+ const [mode, setMode] = useState<'system' | ThemeMode>('system');
const [isManual, setIsManual] = useState(false);
useEffect(() => {
@@ -43,11 +54,15 @@ export function Providers({children}: {children: React.ReactNode}) {
const toggleMode = () => {
setIsManual(true);
- setMode(m => (m === 'light' ? 'dark' : 'light'));
+ setMode(m => (m === 'dark' ? 'light' : 'dark'));
};
+ // Expose a concrete light/dark to consumers (toggle icons, ColorSwatch).
+ // 'system' only survives the first render before the effect resolves it.
+ const resolvedMode: ThemeMode = mode === 'dark' ? 'dark' : 'light';
+
return (
- <ThemeModeContext value={{mode, toggleMode}}>
+ <ThemeModeContext value={{mode: resolvedMode, themeMode: mode, toggleMode}}>
<XDSTheme theme={astryxTheme} mode={mode}>
<XDSLinkProvider component={Link}>{children}</XDSLinkProvider>
</XDSTheme>
diff --git a/apps/docsite/src/constants.ts b/apps/docsite/src/constants.ts
index 4b8f369ad7..e640359414 100644
--- a/apps/docsite/src/constants.ts
+++ b/apps/docsite/src/constants.ts
@@ -1,3 +1,10 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
export const GITHUB_REPO = 'https://github.com/facebookexperimental/xds';
+
+/**
+ * Astryx brand blue — logo/wordmark only (not wired to any semantic token).
+ * Lives here, not in astryxTheme.ts, so it can be imported without pulling in
+ * the unbuilt source theme object (which triggers runtime style injection).
+ */
+export const BRAND_BLUE = 'light-dark(#225BFF, #3D87FF)';
diff --git a/apps/docsite/src/themes/astryxTheme.ts b/apps/docsite/src/themes/astryxTheme.ts
index bb4d8e15ac..fbe86bab03 100644
--- a/apps/docsite/src/themes/astryxTheme.ts
+++ b/apps/docsite/src/themes/astryxTheme.ts
@@ -7,15 +7,15 @@
* in the UI — buttons, links, focus rings, the Switch "on" track, accent
* icons, and tints — is driven by the high-emphasis PRIMARY ink (warm
* near-black). So the theme makes the accent tokens resolve to PRIMARY, and
- * the brand blue lives in its own BRAND_BLUE constant reserved for the logo
- * (applied by the hero, not by any semantic token here).
+ * the brand blue lives in the `BRAND_BLUE` constant in `@/constants`, reserved
+ * for the logo (applied by the hero, not by any semantic token here).
*
* 1. PRIMARY — the high-emphasis foreground ink (warm near-black). Drives
* primary text/icons AND the accent tokens, so all interactive UI reads
* as near-black rather than blue. See `PRIMARY` below.
* 2. BRAND_BLUE — the Astryx brand blue, reserved for the logo only. Not
* wired to any semantic accent token; the hero paints the wordmark with
- * it directly. See `BRAND_BLUE` below.
+ * it directly. Defined in `@/constants`.
*
* The only other overrides are the cream body color, Figtree typography, a
* +4px radius bump, semibold display headings, and pill buttons. The neutral
@@ -38,13 +38,9 @@ const PRIMARY = 'light-dark(#15110C, #DFE2E5)';
const PRIMARY_MUTED =
'light-dark(rgba(21, 17, 12, 0.08), rgba(223, 226, 229, 0.14))';
-// === BRAND_BLUE (logo only) ==================================================
-// Brand blue (#225BFF) — the same blue baked into the Astryx logo/favicon.
-// Reserved for the wordmark; the hero applies it directly (this theme does not
-// point any accent token at it). Dark mode uses a lighter blue (#3D87FF) so the
-// logo stays legible on the dark body. Exported so the hero can reference the
-// exact brand value instead of hardcoding it.
-export const BRAND_BLUE = 'light-dark(#225BFF, #3D87FF)';
+// BRAND_BLUE (logo/wordmark only) lives in `@/constants` — kept out of this
+// source theme so consumers can read it without importing the unbuilt theme
+// object (which would re-trigger runtime style injection).
export const astryxTheme = defineTheme({
name: 'astryx',