Skip to content

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

TypeScript

fix(storybook): sync color-scheme to document root for dark mode toggle (#499)

Cindy Zhanga6ff3aa71361
diff --git a/apps/storybook/.storybook/preview.tsx b/apps/storybook/.storybook/preview.tsx
index ed3a03120f..985ef6f65c 100644
--- a/apps/storybook/.storybook/preview.tsx
+++ b/apps/storybook/.storybook/preview.tsx
@@ -21,13 +21,26 @@ const themes = {
};
/**
- * Decorator that wraps all stories in the XDS Theme provider
+ * Decorator that wraps all stories in the XDS Theme provider.
+ *
+ * Sets `color-scheme` on the document root so that CSS `light-dark()`
+ * resolves correctly at every level of the page — not just inside the
+ * XDSTheme wrapper div. Without this, the iframe's `<html>` and
+ * `<body>` keep the browser-default `color-scheme` (typically "light")
+ * and toggling the toolbar has no visible effect.
*/
const withXDSTheme: Decorator = (Story, context) => {
// Get theme selection from toolbar
const themeKey = (context.globals?.xdsTheme || 'default') as string;
const mode = context.globals?.colorMode === 'dark' ? 'dark' : 'light';
+ // Sync color-scheme to the document root so light-dark() works
+ // everywhere, including on <html>/<body> backgrounds and any
+ // elements rendered outside the XDSTheme wrapper.
+ React.useEffect(() => {
+ document.documentElement.style.setProperty('color-scheme', mode);
+ }, [mode]);
+
// No theme — render with just base defineVars defaults
if (themeKey === 'none') {
return (