Memoize usePalette (#2201)

zio/stable
dan 2023-12-13 07:09:07 +00:00 committed by GitHub
parent b1f9454f1d
commit 0e3218db7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 34 deletions

View File

@ -1,3 +1,4 @@
import {useMemo} from 'react'
import {TextStyle, ViewStyle} from 'react-native' import {TextStyle, ViewStyle} from 'react-native'
import {useTheme, PaletteColorName, PaletteColor} from '../ThemeContext' import {useTheme, PaletteColorName, PaletteColor} from '../ThemeContext'
@ -15,38 +16,41 @@ export interface UsePaletteValue {
icon: TextStyle icon: TextStyle
} }
export function usePalette(color: PaletteColorName): UsePaletteValue { export function usePalette(color: PaletteColorName): UsePaletteValue {
const palette = useTheme().palette[color] const theme = useTheme()
return { return useMemo(() => {
colors: palette, const palette = theme.palette[color]
view: { return {
backgroundColor: palette.background, colors: palette,
}, view: {
viewLight: { backgroundColor: palette.background,
backgroundColor: palette.backgroundLight, },
}, viewLight: {
btn: { backgroundColor: palette.backgroundLight,
backgroundColor: palette.backgroundLight, },
}, btn: {
border: { backgroundColor: palette.backgroundLight,
borderColor: palette.border, },
}, border: {
borderDark: { borderColor: palette.border,
borderColor: palette.borderDark, },
}, borderDark: {
text: { borderColor: palette.borderDark,
color: palette.text, },
}, text: {
textLight: { color: palette.text,
color: palette.textLight, },
}, textLight: {
textInverted: { color: palette.textLight,
color: palette.textInverted, },
}, textInverted: {
link: { color: palette.textInverted,
color: palette.link, },
}, link: {
icon: { color: palette.link,
color: palette.icon, },
}, icon: {
} color: palette.icon,
},
}
}, [theme, color])
} }