bd17f04810
* Add storybook shortcut in dev * Migrate to alg pkg * Migrate font_bold to new font_semi_bold * Migrate font_heavy to font_bold * Fix old theme refs * Remove leading util * Bump * Revert "Add storybook shortcut in dev" This reverts commit 7a86195c77fb5d449c7782e64ebabb6addfab919. * Remove alf script * Adjust font scale * Bump * Bump pkg * Migrate values from conflicts * Create default themes outside react * Use built-in alf utils * Migrate old font styles * Remove unused tokens * Move theme creation to more appropriate file * Update button colors * Adjust TextField colors, line heights
67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import React from 'react'
|
|
import {type ColorSchemeName, useColorScheme} from 'react-native'
|
|
import {type ThemeName} from '@bsky.app/alf'
|
|
|
|
import {isWeb} from '#/platform/detection'
|
|
import {useThemePrefs} from '#/state/shell'
|
|
import {dark, dim, light} from '#/alf/themes'
|
|
|
|
export function useColorModeTheme(): ThemeName {
|
|
const theme = useThemeName()
|
|
|
|
React.useLayoutEffect(() => {
|
|
updateDocument(theme)
|
|
}, [theme])
|
|
|
|
return theme
|
|
}
|
|
|
|
export function useThemeName(): ThemeName {
|
|
const colorScheme = useColorScheme()
|
|
const {colorMode, darkTheme} = useThemePrefs()
|
|
|
|
return getThemeName(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) {
|
|
// @ts-ignore web only
|
|
if (isWeb && typeof window !== 'undefined') {
|
|
// @ts-ignore web only
|
|
const html = window.document.documentElement
|
|
// @ts-ignore web only
|
|
const meta = window.document.querySelector('meta[name="theme-color"]')
|
|
|
|
// remove any other color mode classes
|
|
html.className = html.className.replace(/(theme)--\w+/g, '')
|
|
html.classList.add(`theme--${theme}`)
|
|
// set color to 'theme-color' meta tag
|
|
meta?.setAttribute('content', getBackgroundColor(theme))
|
|
}
|
|
}
|
|
|
|
export function getBackgroundColor(theme: ThemeName): string {
|
|
switch (theme) {
|
|
case 'light':
|
|
return light.atoms.bg.backgroundColor
|
|
case 'dark':
|
|
return dark.atoms.bg.backgroundColor
|
|
case 'dim':
|
|
return dim.atoms.bg.backgroundColor
|
|
}
|
|
}
|