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