({
children,
})
| 10 | } from '../keyboard-shortcuts-context/keyboard-shortcuts'; |
| 11 | |
| 12 | export const ThemeProvider: React.FC<React.PropsWithChildren> = ({ |
| 13 | children, |
| 14 | }) => { |
| 15 | const { theme, setTheme } = useLocalConfig(); |
| 16 | const isDarkSystemTheme = useMediaQuery({ |
| 17 | query: '(prefers-color-scheme: dark)', |
| 18 | }); |
| 19 | |
| 20 | const systemTheme = isDarkSystemTheme ? 'dark' : 'light'; |
| 21 | |
| 22 | const [effectiveTheme, setEffectiveTheme] = |
| 23 | useState<EffectiveTheme>(systemTheme); |
| 24 | |
| 25 | useEffect(() => { |
| 26 | setEffectiveTheme(theme === 'system' ? systemTheme : theme); |
| 27 | }, [theme, systemTheme]); |
| 28 | |
| 29 | useEffect(() => { |
| 30 | if (effectiveTheme === 'dark') { |
| 31 | document.documentElement.classList.add('dark'); |
| 32 | } else { |
| 33 | document.documentElement.classList.remove('dark'); |
| 34 | } |
| 35 | }, [effectiveTheme]); |
| 36 | |
| 37 | const handleThemeToggle = useCallback(() => { |
| 38 | if (theme === 'system') { |
| 39 | setTheme(effectiveTheme === 'dark' ? 'light' : 'dark'); |
| 40 | } else { |
| 41 | setTheme(theme === 'dark' ? 'light' : 'dark'); |
| 42 | } |
| 43 | }, [theme, effectiveTheme, setTheme]); |
| 44 | |
| 45 | useHotkeys( |
| 46 | keyboardShortcutsForOS[KeyboardShortcutAction.TOGGLE_THEME] |
| 47 | .keyCombination, |
| 48 | handleThemeToggle, |
| 49 | { |
| 50 | preventDefault: true, |
| 51 | enableOnFormTags: true, |
| 52 | }, |
| 53 | [handleThemeToggle] |
| 54 | ); |
| 55 | |
| 56 | return ( |
| 57 | <ThemeContext.Provider value={{ theme, setTheme, effectiveTheme }}> |
| 58 | {children} |
| 59 | </ThemeContext.Provider> |
| 60 | ); |
| 61 | }; |
nothing calls this directly
no test coverage detected