From 7e3e7d83ff99cb4c3831375b16caade33953a0ad Mon Sep 17 00:00:00 2001 From: Yousef Date: Mon, 11 Nov 2024 23:50:19 +0200 Subject: [PATCH 1/3] Support unistyles provider with no breaking changes --- examples/expo/src/App.tsx | 1 + examples/expo/src/common/navigation.ts | 4 +- examples/expo/src/examples/HomeScreen.tsx | 17 ++++ .../examples/TwoThemesWithProviderScreen.tsx | 60 +++++++++++++ examples/expo/src/examples/index.ts | 1 + src/context.tsx | 90 +++++++++++++++++++ src/hooks/useUnistyles.ts | 16 +++- src/index.ts | 5 +- 8 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 examples/expo/src/examples/TwoThemesWithProviderScreen.tsx create mode 100644 src/context.tsx diff --git a/examples/expo/src/App.tsx b/examples/expo/src/App.tsx index 365574ba..36fc24a9 100644 --- a/examples/expo/src/App.tsx +++ b/examples/expo/src/App.tsx @@ -28,6 +28,7 @@ export const App: React.FunctionComponent = () => ( + diff --git a/examples/expo/src/common/navigation.ts b/examples/expo/src/common/navigation.ts index 4d599533..25a04f5d 100644 --- a/examples/expo/src/common/navigation.ts +++ b/examples/expo/src/common/navigation.ts @@ -31,7 +31,8 @@ export enum DemoNames { UpdateTheme = 'UpdateThemeScreen', AndroidStatusBarNavigationBar = 'AndroidStatusBarNavigationBarScreen', Layout = 'LayoutScreen', - Keyboard = 'KeyboardScreen' + Keyboard = 'KeyboardScreen', + TwoThemesWithProvider = 'TwoThemesWithProvider', } export type DemoStackParams = { @@ -66,6 +67,7 @@ export type DemoStackParams = { [DemoNames.AndroidStatusBarNavigationBar]: undefined, [DemoNames.Layout]: undefined, [DemoNames.Keyboard]: undefined + [DemoNames.TwoThemesWithProvider]: undefined, } export type NavigationProps = NavigationProp diff --git a/examples/expo/src/examples/HomeScreen.tsx b/examples/expo/src/examples/HomeScreen.tsx index c9079e1d..33d844ec 100644 --- a/examples/expo/src/examples/HomeScreen.tsx +++ b/examples/expo/src/examples/HomeScreen.tsx @@ -71,6 +71,23 @@ export const HomeScreen = () => { navigation.navigate(DemoNames.TwoThemes) }} /> + { + UnistylesRegistry + .addThemes({ + light: lightTheme, + premium: premiumTheme + // we need to cast it to UnistylesThemes as we already registered 3 themes with TypeScript under styles/index.ts, + // but we want to demonstrate how to register two themes + } as UnistylesThemes) + .addConfig({ + initialTheme: 'light' + }) + + navigation.navigate(DemoNames.TwoThemesWithProvider) + }} + /> { diff --git a/examples/expo/src/examples/TwoThemesWithProviderScreen.tsx b/examples/expo/src/examples/TwoThemesWithProviderScreen.tsx new file mode 100644 index 00000000..8110106e --- /dev/null +++ b/examples/expo/src/examples/TwoThemesWithProviderScreen.tsx @@ -0,0 +1,60 @@ +import React from 'react' +import { Text, View } from 'react-native' +import { createStyleSheet, useStyles, UnistylesRuntime, useInitialTheme, UnistylesProvider } from 'react-native-unistyles' +import { Button, DemoScreen } from '../components' + +export const TwoThemesWithProviderScreen: React.FunctionComponent = () => { + return ( + + + + ) +} +const TwoThemesWithProviderScreenContent: React.FunctionComponent = () => { + // if you have 2 or more themes, you need to select one of them + // keep in mind that everything is synchronous and so fast, that you can set it in the same screen 🔥 + // two more notes: + // call it before useStyles + // if you won't do that you will get UNISTYLES_THEME_NOT_FOUND error + useInitialTheme('premium') + + // you can also skip useInitialTheme and set the theme during UnistylesRegistry.addConfig({}) call + + const { styles, theme } = useStyles(stylesheet) + + return ( + + + + This screen has two themes registered with `UnistylesRegistry.addThemes` function. + + + It also shows a way to switch the theme from anywhere of your app. You can do that importing `UnistylesRuntime` and calling `setTheme` function. + + + This screen uses {UnistylesRuntime.themeName} theme. + +