Skip to content

Commit 075e156

Browse files
committed
fix: apply theme synchronously in Storybook decorator for Chromatic
- Use initialGlobals instead of deprecated defaultValue - Apply data-theme attribute synchronously before React renders - Default to dark theme when globals not set (Chromatic headless defaults to light) This ensures Chromatic captures the correct theme in snapshots.
1 parent 3332f68 commit 075e156

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

.storybook/preview.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ const preview: Preview = {
6868
theme: {
6969
name: "Theme",
7070
description: "Choose between light and dark UI themes",
71-
defaultValue: "dark",
7271
toolbar: {
7372
icon: "mirror",
7473
items: [
@@ -79,13 +78,24 @@ const preview: Preview = {
7978
},
8079
},
8180
},
81+
initialGlobals: {
82+
theme: "dark",
83+
},
8284
decorators: [
8385
(Story, context) => {
84-
const mode = context.globals.theme as ThemeMode | undefined;
86+
// Default to dark if mode not set (e.g., Chromatic headless browser defaults to light)
87+
const mode = (context.globals.theme as ThemeMode | undefined) ?? "dark";
88+
89+
// Apply theme synchronously before React renders - critical for Chromatic snapshots
90+
if (typeof document !== "undefined") {
91+
document.documentElement.dataset.theme = mode;
92+
document.documentElement.style.colorScheme = mode;
93+
}
94+
8595
return (
8696
<ThemeProvider forcedTheme={mode}>
8797
<Story />
88-
{!mode && !isE2ETestEnv() && <StorybookThemeToggle />}
98+
{!context.globals.theme && !isE2ETestEnv() && <StorybookThemeToggle />}
8999
</ThemeProvider>
90100
);
91101
},

0 commit comments

Comments
 (0)