@@ -39,79 +39,104 @@ npm install --save react-native-appearance react-native-themed-stylesheet
3939
4040## Usage
4141
42- Creating the theme :
42+ Create a type declaration file to merge BaseTheme declaration :
4343
4444``` ts
45- // theme.ts
46- import { createTheme } from ' react-native-themed-stylesheet'
47-
48- const themes = {
49- light: {
50- textColor: ' #ff0000'
51- },
52- dark: {
53- textColor: ' #fff'
54- },
55- common: { // Optional
56- fontSize: 12
45+ // react-native-themed-stylesheet.d.ts
46+ import ' react-native-themed-stylesheet'
47+
48+ declare module ' react-native-themed-stylesheet' {
49+ export interface BaseTheme {
50+ theme: {
51+ colors: {
52+ primary: string
53+ }
54+ },
55+ constants: { // This is optional. If declared/defined, it will be merge with the current theme.
56+ colors: {
57+ accent: string
58+ }
59+ }
5760 }
5861}
59-
60- const { ThemeProvider, useStyle, useTheme } = createTheme (themes , ' auto' ) // Initial Mode is optional(Default: 'auto')
61-
62- export { ThemeProvider , useStyle , useTheme }
6362```
6463
6564Using the theme:
6665
6766``` tsx
68- // Components .tsx
67+ // App .tsx
6968import React from ' react'
7069import { View , Text , Button } from ' react-native'
71- import { ThemeProvider , useStyle , useTheme } from ' ./theme'
72-
73- const ComponentWithUseStyle: React .FC = () => {
74- const styles = useStyle (theme => {
75- text : {
76- color : theme .textColor ,
77- fontSize : theme .fontSize
78- }
79- }
80-
81- return (
82- <View style = { { flex: 1 , justifyContent: ' center' , alignItems: ' center' }} >
83- <Text style = { styles .text } >Hello World</Text >
84- </View >
85- )
86- }
70+ import { ThemeProvider , useCreateStyles , useMode , useTheme , useThemes } from ' react-native-themed-stylesheet'
8771
88- const ComponentWithUseTheme : React .FC = () => {
89- const { theme, mode, setThemes, setMode } = useTheme ()
72+ const DefaultComponent: React .FC = () => {
73+ const { colors } = useTheme ()
74+ const [mode, setMode] = useMode ()
75+ const [themes, setThemes] = useThemes ()
9076 console .log (' Current Mode:' , mode )
91-
77+ console . log ( ' Themes: ' , themes )
9278 return (
9379 <View style = { { flex: 1 , justifyContent: ' center' , alignItems: ' center' }} >
94- <Text style = { { color: theme . textColor , fontSize: theme . fontSize }} >Hello World</Text >
80+ <Text style = { { color: colors . primary , backgroundColor: colors . accent }} >Hello World</Text >
9581 <Button title = ' Dark Mode' onPress = { () => setMode (' dark' )} />
9682 <Button title = ' Light Mode' onPress = { () => setMode (' light' )} />
9783 <Button title = ' Auto Mode' onPress = { () => setMode (' auto' )} />
9884 <Button title = ' Change Themes' onPress = {
9985 () => setThemes ({
10086 light: {
101- textColor: ' #ffff00 '
87+ colors: { primary: ' #c1c1c1 ' }
10288 },
10389 dark: {
104- textColor: ' #C9C9C9 '
90+ colors: { primary: ' #c2c2c2 ' }
10591 },
106- common : {
107- fontSize: 14
92+ constants : {
93+ colors: { accent: ' #c3c3c3 ' }
10894 }
10995 })
11096 }
11197 />
11298 </View >
11399 )
114100}
101+
102+ const ComponentWithUseCreateStyles: React .FC = () => {
103+ const styles = useCreateStyles (({ colors }) => {
104+ text : {
105+ color : colors .primary ,
106+ backgroundColor : colors .accent
107+ }
108+ }
109+ return (
110+ <View style = { { flex: 1 , justifyContent: ' center' , alignItems: ' center' }} >
111+ <Text style = { styles .text } >Hello World</Text >
112+ </View >
113+ )
114+ }
115+
116+ const themes = {
117+ dark: {
118+ colors: {
119+ primary: ' #000'
120+ }
121+ },
122+ light: {
123+ colors: {
124+ primary: ' #fff'
125+ }
126+ },
127+ constants: {
128+ colors: {
129+ accent: ' #c0c0c0'
130+ }
131+ }
132+ }
133+
134+ const App : React .FC = () => (
135+ <ThemeProvider themes = { themes } >
136+ <DefaultComponent />
137+ <ComponentWithUseCreateStyles />
138+ </ThemeProvider >
139+ )
115140` ` `
116141## Storybook Addon
117142
@@ -121,18 +146,9 @@ const ComponentWithUseTheme: React.FC = () => {
121146// storybook.js
122147import {
123148 getStorybookUI ,
124- configure ,
125- addDecorator ,
126- addParameters
149+ configure
127150} from ' @storybook/react-native'
128- import { withThemeHook } from ' react-native-themed-stylesheet/storybook'
129151import ' react-native-themed-stylesheet/storybook/register'
130- import { useTheme } from ' ./theme'
131-
132- addDecorator (withThemeHook )
133- addParameters ({
134- useTheme
135- })
136152
137153configure (() => {
138154 require (' path/to/some/story' )
@@ -145,74 +161,83 @@ export default StorybookUIRoot // Make sure to use this component within ThemePr
145161
146162## API
147163
148- ### Function : ` createTheme ( themes , [ initialMode ]) `
164+ ### React Component : ` ThemeProvider `
149165
150- Use this function to create the theme .
166+ Component to provide ThemeContext .
151167
152- **Parameters **
168+ **Props **
153169
154- - ` themes ` : An object containing light, dark and an optional common theme(Will be merge with boths themes).
155- - ` initialMode ` : A string('light', 'dark' or 'auto') specifying the initial mode(Default: 'auto').
156-
157- **Returns**
170+ - ` themes ` : An object of type ` Themes ` :
158171
172+ ` ` ` ts
173+ type Themes = {
174+ dark: BaseTheme [' theme' ]
175+ light : BaseTheme [' theme' ]
176+ constants ?: BaseTheme [' constants' ]
177+ }
159178` ` `
160179
161- ThemeObject
180+ - ` mode ` : An optional string of type ` ThemeMode ` : (Default: 'auto')
162181
182+ ` ` ` ts
183+ type ThemeMode = ' auto' | ' dark' | ' light'
163184` ` `
164185
165186---
166187
167- ### Object: ` ThemeObject `
168-
169- An object containing the following properties:
188+ ### Function: ` useCreateStyle (createStyles )`
170189
171- - ` ThemeProvider ` : Theme Provider.
172- - ` useStyle ` : Hook to create Named StyleSheets.
173- - ` useTheme ` : Hook to get access to ThemeContext.
190+ Hook to create themed stylesheets.
174191
175- ---
192+ **Parameters**
176193
177- ### React Component: ` ThemeProvider `
194+ - ` createStyles ` : A function that receives the current theme and returns an object of type ` Styles ` .
178195
179- A react component to provide ThemeContext.
196+ ` ` ` ts
197+ type Styles = {
198+ [prop : string ]: ViewStyle | TextStyle | ImageStyle
199+ }
200+ type Theme = (BaseTheme [' constants' ] & BaseTheme [' theme' ]) | BaseTheme [' theme' ]
201+ ` ` `
180202
181- ---
203+ **Returns**
182204
183- ### Function: ` useStyle (createStyles )`
205+ ` ` `
206+ [styles : StyleSheet .NamedStyles < Styles > , theme : Theme ]
207+ ` ` `
184208
185- Hook to create themed stylesheets.
209+ ---
186210
187- **Parameters**
211+ ### Function: ` useMode () `
188212
189- - ` createStyles ` : A function that receives the current theme and returns an object of type ` T ` .
213+ Hook to get access to current mode .
190214
191215**Returns**
192216
193- ` ` `
194-
195- StyleSheet .NamedStyles < T >
196-
217+ ` ` ` ts
218+ [mode , setMode ]: [ThemeMode , (newMode : ThemeMode ) => void ]
197219` ` `
198220
199221---
200222
201223### Function: ` useTheme ()`
202224
203- Hook to get access to theme context .
225+ Hook to get access to current theme .
204226
205227**Returns**
206228
229+ ` ` ` ts
230+ theme : Theme
207231` ` `
208232
209- { theme , mode , setThemes , setMode }
233+ ---
210234
211- ` ` `
235+ ### Function: ` useThemes () `
212236
213- An object containing the following properties:
237+ Hook to get access to themes.
214238
215- - ` theme ` : The current theme.
216- - ` mode ` : The current mode.
217- - ` setThemes ` : Function to set the themes(The same type of ` createTheme ` ` themes ` param).
218- - ` setMode ` : Function to set the mode('light', 'dark' or 'auto').
239+ **Returns**
240+
241+ ` ` ` ts
242+ [themes , setThemes ]: [Themes , (newThemes : Themes ) => void ]
243+ ` ` `
0 commit comments