diff options
author | Hailey <me@haileyok.com> | 2024-02-06 21:16:50 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-06 21:16:50 -0800 |
commit | 573cf31d865044b0cc442a7390ecbda15c9ba1d0 (patch) | |
tree | e08b33af016a585da2ee6b0fefaac010497a6236 /src/alf/util/useColorModeTheme.ts | |
parent | dc6603a1b978122238108792c9de2d4f3b040396 (diff) | |
download | voidsky-573cf31d865044b0cc442a7390ecbda15c9ba1d0.tar.zst |
fix theme cross-tab write loop (#2774)
* don't write on onUpdate, memoize * refac useColorModeTheme
Diffstat (limited to 'src/alf/util/useColorModeTheme.ts')
-rw-r--r-- | src/alf/util/useColorModeTheme.ts | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/src/alf/util/useColorModeTheme.ts b/src/alf/util/useColorModeTheme.ts index 49e2ec8f5..48cf904fe 100644 --- a/src/alf/util/useColorModeTheme.ts +++ b/src/alf/util/useColorModeTheme.ts @@ -1,5 +1,5 @@ import React from 'react' -import {useColorScheme} from 'react-native' +import {ColorSchemeName, useColorScheme} from 'react-native' import {useThemePrefs} from 'state/shell' import {isWeb} from 'platform/detection' @@ -10,21 +10,31 @@ export function useColorModeTheme(): ThemeName { const colorScheme = useColorScheme() const {colorMode, darkTheme} = useThemePrefs() - return React.useMemo(() => { - if ( - (colorMode === 'system' && colorScheme === 'light') || - colorMode === 'light' - ) { - updateDocument('light') - updateSystemBackground('light') - return 'light' - } else { - const themeName = darkTheme ?? 'dim' - updateDocument(themeName) - updateSystemBackground(themeName) - return themeName - } - }, [colorMode, darkTheme, colorScheme]) + React.useLayoutEffect(() => { + const theme = getThemeName(colorScheme, colorMode, darkTheme) + updateDocument(theme) + updateSystemBackground(theme) + }, [colorMode, colorScheme, darkTheme]) + + return React.useMemo( + () => getThemeName(colorScheme, colorMode, darkTheme), + [colorScheme, colorMode, darkTheme], + ) +} + +function getThemeName( + colorScheme: ColorSchemeName, + colorMode: 'system' | 'light' | 'dark', + darkTheme?: ThemeName, +) { + if ( + (colorMode === 'system' && colorScheme === 'light') || + colorMode === 'light' + ) { + return 'light' + } else { + return darkTheme ?? 'dim' + } } function updateDocument(theme: ThemeName) { |