Skip to content

Commit fb22131

Browse files
committed
🤖 fix: propagate parent theme in ThemeProvider to support nesting
1 parent e5d1ec7 commit fb22131

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/browser/contexts/ThemeContext.tsx

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

5454
export function ThemeProvider(props: { children: ReactNode; forcedTheme?: ThemeMode }) {
55+
const parentContext = useContext(ThemeContext);
5556
const [persistedTheme, setPersistedTheme] = usePersistedState<ThemeMode>(
5657
UI_THEME_KEY,
5758
resolveSystemTheme(),
@@ -60,7 +61,7 @@ export function ThemeProvider(props: { children: ReactNode; forcedTheme?: ThemeM
6061
}
6162
);
6263

63-
const theme = props.forcedTheme ?? persistedTheme;
64+
const theme = props.forcedTheme ?? parentContext?.theme ?? persistedTheme;
6465

6566
useLayoutEffect(() => {
6667
applyThemeToDocument(theme);
@@ -69,15 +70,23 @@ export function ThemeProvider(props: { children: ReactNode; forcedTheme?: ThemeM
6970
const setTheme = useCallback(
7071
(value: React.SetStateAction<ThemeMode>) => {
7172
if (props.forcedTheme) return;
73+
if (parentContext) {
74+
parentContext.setTheme(value);
75+
return;
76+
}
7277
setPersistedTheme(value);
7378
},
74-
[props.forcedTheme, setPersistedTheme]
79+
[props.forcedTheme, parentContext, setPersistedTheme]
7580
);
7681

7782
const toggleTheme = useCallback(() => {
7883
if (props.forcedTheme) return;
84+
if (parentContext) {
85+
parentContext.toggleTheme();
86+
return;
87+
}
7988
setPersistedTheme((current) => (current === "dark" ? "light" : "dark"));
80-
}, [props.forcedTheme, setPersistedTheme]);
89+
}, [props.forcedTheme, parentContext, setPersistedTheme]);
8190

8291
const value = useMemo<ThemeContextValue>(
8392
() => ({

0 commit comments

Comments
 (0)