mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
at session-alignment 65 lines 1.9 kB view raw
1import React from 'react' 2import {ColorSchemeName, useColorScheme} from 'react-native' 3 4import {useThemePrefs} from 'state/shell' 5import {isWeb} from 'platform/detection' 6import {ThemeName, light, dark, dim} from '#/alf/themes' 7import * as SystemUI from 'expo-system-ui' 8 9export function useColorModeTheme(): ThemeName { 10 const colorScheme = useColorScheme() 11 const {colorMode, darkTheme} = useThemePrefs() 12 13 React.useLayoutEffect(() => { 14 const theme = getThemeName(colorScheme, colorMode, darkTheme) 15 updateDocument(theme) 16 SystemUI.setBackgroundColorAsync(getBackgroundColor(theme)) 17 }, [colorMode, colorScheme, darkTheme]) 18 19 return React.useMemo( 20 () => getThemeName(colorScheme, colorMode, darkTheme), 21 [colorScheme, colorMode, darkTheme], 22 ) 23} 24 25function getThemeName( 26 colorScheme: ColorSchemeName, 27 colorMode: 'system' | 'light' | 'dark', 28 darkTheme?: ThemeName, 29) { 30 if ( 31 (colorMode === 'system' && colorScheme === 'light') || 32 colorMode === 'light' 33 ) { 34 return 'light' 35 } else { 36 return darkTheme ?? 'dim' 37 } 38} 39 40function updateDocument(theme: ThemeName) { 41 // @ts-ignore web only 42 if (isWeb && typeof window !== 'undefined') { 43 // @ts-ignore web only 44 const html = window.document.documentElement 45 // @ts-ignore web only 46 const meta = window.document.querySelector('meta[name="theme-color"]') 47 48 // remove any other color mode classes 49 html.className = html.className.replace(/(theme)--\w+/g, '') 50 html.classList.add(`theme--${theme}`) 51 // set color to 'theme-color' meta tag 52 meta?.setAttribute('content', getBackgroundColor(theme)) 53 } 54} 55 56function getBackgroundColor(theme: ThemeName): string { 57 switch (theme) { 58 case 'light': 59 return light.atoms.bg.backgroundColor 60 case 'dark': 61 return dark.atoms.bg.backgroundColor 62 case 'dim': 63 return dim.atoms.bg.backgroundColor 64 } 65}