Skip to content

Commit dac3891

Browse files
committed
🤖 fix(storybook): use forcedTheme as initial value to prevent flash
When forcedTheme is provided (e.g., from Chromatic mode variants), use it as the initial value for usePersistedState to ensure the correct theme is applied on first render without flashing through the default/persisted theme. This ensures light mode stories render in light theme and dark mode stories render in dark theme, rather than both appearing as dark.
1 parent e3fbdbf commit dac3891

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/browser/contexts/ThemeContext.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ function applyThemeToDocument(theme: ThemeMode) {
5252
}
5353

5454
export function ThemeProvider(props: { children: ReactNode; forcedTheme?: ThemeMode }) {
55-
const [theme, setTheme] = usePersistedState<ThemeMode>(UI_THEME_KEY, resolveSystemTheme(), {
56-
listener: !props.forcedTheme, // Disable listener when theme is forced (avoids conflicts)
57-
});
55+
// When forcedTheme is provided, use it as the default to avoid flash of wrong theme
56+
const [theme, setTheme] = usePersistedState<ThemeMode>(
57+
UI_THEME_KEY,
58+
props.forcedTheme ?? resolveSystemTheme(),
59+
{
60+
listener: !props.forcedTheme, // Disable listener when theme is forced (avoids conflicts)
61+
}
62+
);
5863

5964
// Use forcedTheme if provided (e.g., from Storybook), otherwise use persisted theme
6065
const activeTheme = props.forcedTheme ?? theme;

0 commit comments

Comments
 (0)