import {ImageStyle, StyleSheet, TextStyle, ViewStyle} from 'react-native'; import {ThemeProvider} from './ThemeProvider'; type NamedStyles = {[P in keyof T]: ViewStyle | TextStyle | ImageStyle}; export function DynamicStyleSheet | NamedStyles>( callback: () => T | NamedStyles, ): (theme?: 'light' | 'dark') => T { const cache: {light?: T; dark?: T} = { light: undefined, dark: undefined, }; return (theme?: 'light' | 'dark'): T => { const currentTheme = theme || ThemeProvider.theme; let style = cache[currentTheme]; if (!style) { style = StyleSheet.create(callback()); cache[currentTheme] = style; } return style; }; }