|
| 1 | + |
| 2 | + |
| 3 | +# react-native-themed-stylesheet |
| 4 | + |
| 5 | +A package that allows you to create React Native StyleSheets with support for Dark/Light/Auto Themes. |
| 6 | + |
| 7 | +- Depends on react-native-appearance to choose the theme based on OS preference (Android 10/iOS 13) |
| 8 | +- Simple API |
| 9 | +- Fully typed |
| 10 | +- Builds on top of StyleSheets and Hooks |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +**Using Expo** |
| 15 | + |
| 16 | +``` |
| 17 | +expo install react-native-appearance react-native-themed-stylesheet |
| 18 | +``` |
| 19 | + |
| 20 | +**Using Yarn** |
| 21 | + |
| 22 | +``` |
| 23 | +yarn add react-native-appearance react-native-themed-stylesheet |
| 24 | +``` |
| 25 | + |
| 26 | +**Using NPM** |
| 27 | + |
| 28 | +``` |
| 29 | +npm install --save react-native-appearance react-native-themed-stylesheet |
| 30 | +``` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +Defining Themes: |
| 35 | + |
| 36 | +```ts |
| 37 | +// themes.ts |
| 38 | + |
| 39 | +import { createTheme } from 'react-native-themed-stylesheet' |
| 40 | + |
| 41 | +const themes = { |
| 42 | + light: { |
| 43 | + textColor: '#ff0000' |
| 44 | + }, |
| 45 | + dark: { |
| 46 | + textColor: '#fff' |
| 47 | + }, |
| 48 | + common: { |
| 49 | + //Optional |
| 50 | + fontSize: 12 |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +const { ThemeProvider, useStyle, useTheme } = createTheme(themes, 'auto') //Initial Mode is optional(Default: 'auto') |
| 55 | + |
| 56 | +export { ThemeProvider, useStyle, useTheme } |
| 57 | +``` |
| 58 | + |
| 59 | +Using themes: |
| 60 | + |
| 61 | +```tsx |
| 62 | +// Components.tsx |
| 63 | +import React from 'react' |
| 64 | +import { View, Text, Button } from 'react-native' |
| 65 | +import { ThemeProvider, useStyle, useTheme } from './themes' |
| 66 | +import { styleSheetFactory } from './themes' |
| 67 | + |
| 68 | +const ComponentWithUseStyle = () => { |
| 69 | + const styles = useStyle(theme => { |
| 70 | + text: { |
| 71 | + color: theme.textColor, |
| 72 | + fontSize: theme.fontSize |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + return ( |
| 77 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 78 | + <Text style={styles.text}>Hello World</Text> |
| 79 | + </View> |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +const ComponentWithUseTheme = () => { |
| 84 | + const { theme, mode, setThemes, setMode } = useTheme() |
| 85 | + console.log('Current Mode:', mode) |
| 86 | + |
| 87 | + return ( |
| 88 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 89 | + <Text style={{ color: theme.textColor, fontSize: theme.fontSize }}>Hello World</Text> |
| 90 | + <Button title='Dark Mode' onPress={() => setMode('dark')}/> |
| 91 | + <Button title='Light Mode' onPress={() => setMode('light')}/> |
| 92 | + <Button title='Auto Mode' onPress={() => setMode('auto')}/> |
| 93 | + <Button title='Change Themes' onPress={ |
| 94 | + () => setThemes({ |
| 95 | + light: { |
| 96 | + textColor: '#ffff00' |
| 97 | + }, |
| 98 | + dark: { |
| 99 | + textColor: '#C9C9C9' |
| 100 | + }, |
| 101 | + common: { |
| 102 | + fontSize: 14 |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | + /> |
| 107 | + </View> |
| 108 | + ) |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## API |
| 113 | + |
| 114 | +### Function: `createTheme(themes, initialMode)` |
| 115 | + |
| 116 | +Use this function to create the theme. |
| 117 | + |
| 118 | +**Parameters** |
| 119 | + |
| 120 | +- `themes`: An object containing light, dark and an optional common theme(Will be merge with boths themes). |
| 121 | +- `initialMode`: A string('light', 'dark' or 'auto') specifying the initial mode(Default: 'auto'). |
| 122 | + |
| 123 | +**Returns** |
| 124 | + |
| 125 | +``` |
| 126 | +
|
| 127 | +ThemeObject |
| 128 | +
|
| 129 | +``` |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +### Object: `ThemeObject` |
| 134 | + |
| 135 | +An object containing the following properties: |
| 136 | + |
| 137 | +- ThemeProvider: Theme Provider |
| 138 | +- useStyle: Hook to create Named StyleSheets |
| 139 | +- useTheme: Hook to get access to ThemeContext |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +### React Component: `ThemeProvider` |
| 144 | + |
| 145 | +A react component to provide ThemeContext. |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +### Function: `useStyle(createStyle)` |
| 150 | + |
| 151 | +Use this function to create themed stylesheets. |
| 152 | + |
| 153 | +**Parameters** |
| 154 | + |
| 155 | +- `createStyle`: A function that receive the current theme and return an object of type `T`. |
| 156 | + |
| 157 | +**Returns** |
| 158 | + |
| 159 | +``` |
| 160 | +
|
| 161 | +StyleSheet.NamedStyles<T> |
| 162 | +
|
| 163 | +``` |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +### Function: `useTheme()` |
| 168 | + |
| 169 | +Use this function to get theme context. |
| 170 | + |
| 171 | +**Returns** |
| 172 | + |
| 173 | +``` |
| 174 | +
|
| 175 | +{ theme, mode, setThemes, setMode } |
| 176 | +
|
| 177 | +``` |
| 178 | + |
| 179 | +An object containing the following properties: |
| 180 | + |
| 181 | +- `theme`: The current theme |
| 182 | +- `mode`: The current mode. |
| 183 | +- `setThemes`: Function to set the themes(The same type of createTheme themes param). |
| 184 | +- `setMode`: Function to set the mode('light', 'dark' or 'auto'). |
0 commit comments