react-native-theming
An efficient and StyleSheet.create
compatible theming library for React Native.
Installation
$ yarn add react-native-theming
or
$ npm install --save react-native-theming
Usage
Create themes
const themes = ;
Create Styles
Create styles as you would with StyleSheet.create
. Except you can
now use theme variables on your styles with an @
prefix followed by
the name of the theme variable as declared in the theme. You can also
construct your style including the theme variable, like 'rgba(@backgroundColor, 0.2)'.
; const styles = ;
Create custom components
The theming library provides Theme.View
, Theme.Image
, Theme.Text
,
Theme.AnimatedView
, Theme.AnimatedImage
, Theme.Animated.Text
components,
which needs to be used in place of respective View, Image and Text for the theme
to take affect. Custom components could be easily made themable as well.
;; const Button = ;const Bar = ;
Create your themed view
It is not just the styles, but the themes could even be applied to the props.
Not all properties will however support theming. For example, with the builtin
components, only Theme.Image
and Theme.AnimatedImage
supports theming with
source
property. You can however create custom components with an array of
props that needs theming support. In the above example, the StatusBar
component
has been themed with barStyle
and backgroundColor
props.
;;; ... Create your themes... Create the styles... Create custom components { return <ThemeView style=stylescontainer> <Bar barStyle="@statusBar" backgroundColor="@backgroundColor" /> <ThemeImage source="@icon" /> <ThemeText style=styleswelcome> React Native Theming Demo! </ThemeText> <ThemeText style=stylesinstructions> To experiment check appjs file </ThemeText> <Text style=stylesinstructions> You can now create your themes using JSON The styles declaration is directly compatible with StyleSheetcreate You just need to replace `StyleSheet.create` with `createStyle` and add your theme variables in the styles </Text> <View style= flexDirection: 'row' > themes </View> </ThemeView> ; }
Applying Theme
Applying themes is just a matter of invoking apply
method on the theme
instance
returned by the createTheme
method. Check out the Button.onPress event in the
above example. The first created theme becomes the default theme.
Try the demo
or