react-native-themed-stylesheet A package that allows you to create React Native StyleSheets with support for Dark/Light/Auto Themes.
A package that allows you to create React Native StyleSheets with support for Dark/Light/Auto Themes.
Using Expo
expo install react-native-appearance react-native-themed-stylesheet
Using Yarn
yarn add react-native-appearance react-native-themed-stylesheet
Using NPM
npm install --save react-native-appearance react-native-themed-stylesheet
Creating the theme:
// theme.ts
import { createTheme } from 'react-native-themed-stylesheet'
const themes = {
light: {
textColor: '#ff0000'
},
dark: {
textColor: '#fff'
},
common: { // Optional
fontSize: 12
}
}
const { ThemeProvider, useStyle, useTheme } = createTheme(themes, 'auto') // Initial Mode is optional(Default: 'auto')
export { ThemeProvider, useStyle, useTheme }
Using the theme:
// Components.tsx
import React from 'react'
import { View, Text, Button } from 'react-native'
import { ThemeProvider, useStyle, useTheme } from './theme'
const ComponentWithUseStyle: React.FC = () => {
const styles = useStyle(theme => {
text: {
color: options.disabled ? '#C9C9C9' : theme.textColor,
fontSize: theme.fontSize
}
})
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={styles.text}>Hello World</Text>
</View>
)
}
const ComponentWithUseTheme: React.FC = () => {
const { theme, mode, setThemes, setMode } = useTheme()
console.log('Current Mode:', mode)
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ color: theme.textColor, fontSize: theme.fontSize }}>Hello World</Text>
<Button title='Dark Mode' onPress={() => setMode('dark')}/>
<Button title='Light Mode' onPress={() => setMode('light')}/>
<Button title='Auto Mode' onPress={() => setMode('auto')}/>
<Button title='Change Themes' onPress={
() => setThemes({
light: {
textColor: '#ffff00'
},
dark: {
textColor: '#C9C9C9'
},
common: {
fontSize: 14
}
})
}
/>
</View>
)
}
// storybook.js
import {
getStorybookUI,
configure,
addDecorator,
addParameters
} from '@storybook/react-native'
import { withThemeHook } from 'react-native-themed-stylesheet/storybook'
import 'react-native-themed-stylesheet/storybook/register'
import { useTheme } from './theme'
addDecorator(withThemeHook)
addParameters({
useTheme
})
configure(() => {
require('path/to/some/story')
}, module)
const StorybookUIRoot = getStorybookUI()
export default StorybookUIRoot // Make sure to use this component within ThemeProvider.
createTheme(themes, [initialMode])
Use this function to create the theme.
Parameters
themes
: An object containing light, dark and an optional common theme(Will be merge with boths themes).initialMode
: A string('light', 'dark' or 'auto') specifying the initial mode(Default: 'auto').Returns
ThemeObject
ThemeObject
An object containing the following properties:
ThemeProvider
: Theme Provider.useStyle
: Hook to create Named StyleSheets.useTheme
: Hook to get access to ThemeContext.ThemeProvider
A react component to provide ThemeContext.
useStyle(createStyles)
Hook to create themed stylesheets.
Parameters
createStyles
: A function that receives the current theme and returns an object of type T
.Returns
StyleSheet.NamedStyles<T>
useTheme()
Hook to get access to theme context.
Returns
{ theme, mode, setThemes, setMode }
An object containing the following properties:
theme
: The current theme.mode
: The current mode.setThemes
: Function to set the themes(The same type of createTheme
themes
param).setMode
: Function to set the mode('light', 'dark' or 'auto').Author: andreppedroza
Source Code: https://github.com/andreppedroza/react-native-themed-stylesheet
Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.
Skenix is providing React Native Development Services with qualified React Native App Developers. Get the best React Native App Development Services.
Are you a mobile app developer looking for more efficient tools for your projects? Mobile app development is getting tougher and tougher as the market continues to grow. As a developer, you need to develop Apps which meet the demands of your users. To achieve this, adopt the right tools, like the React Native development framework.
Looking for top React Native mobile app development company in USA for Startups & Enterprise? Find out the top list of React Native mobile app development company in USA.
Hire top react native app development company in New York to build and develop custom react native mobile apps for Android & iOS with the latest features.