Last active
December 1, 2021 01:57
-
-
Save nmquebb/1d5312476282e7a6c543e8d745a9b4ab to your computer and use it in GitHub Desktop.
React Native light/dark theme styles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useMemo } from 'react'; | |
| import { StyleProp, StyleSheet, useColorScheme } from 'react-native'; | |
| type ThemeKeys = 'base' | 'light' | 'dark'; | |
| type ComposedStyles = StyleProp<object>; | |
| type StylesRecord = Record<ThemeKeys, ComposedStyles>; | |
| export function useThemeStyles<T extends StylesRecord>(styles: T): T['base'] { | |
| const theme = useColorScheme() ?? 'light'; | |
| return useMemo(() => { | |
| return StyleSheet.compose(styles.base, styles[theme]); | |
| }, [theme]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment