Take a look at Shoutem Themes for React Native.
Here is what you get with Shoutem Theme:
Global style where certain style is automatically applied to component by its style name:
const theme = {
'my.app.ComponentStyleName': {
backgroundColor: 'navy',
},
};
Activating certain component specific style with styleName
(like CSS class):
const theme = {
'my.app.ComponentStyleName': {
'.rounded': {
borderRadius: 20,
},
backgroundColor: 'navy',
},
};
// Implementation - will apply borderRadius to Component
<Component styleName="rounded"/>
Automatic style inheritance:
const theme = {
'my.app.ComponentStyleName': {
'my.app.ChildComponentStyleName': {
backgroundColor: 'red',
},
'.rounded': {
borderRadius: 20,
},
backgroundColor: 'navy',
},
};
// Implementation - will apply red background color to ChildComponent
<Component styleName="rounded">
<ChildComponent/>
</Component>
Nested style for composed components:
const theme = {
'my.app.ComponentStyleName': {
containerView: {
paddingTop: 10,
},
innerView: {
backgroundColor: 'yellow',
},
},
};
// Of course do not forget to connect Component
function Component({ style }) {
return (
<View style={style.containerView}>
<View style={style.innerView}>
<Text>Warning with yellow background.</Text>
</View>
</View>
);
}
To get it work you need to use StyleProvider
, the wrapper component which provides style to all other component through context. Similar to Redux StoreProvider
.
Also you need to connect your component to style with connectStyle
so you always use connected component. Similar to Redux connect
.
export const styledComponent = connectStyle('my.app.ComponentStyleName',
{ ...defaultStyleIfAny })(Component);
You can see example within documentation.
One last thing, we have also provided set of components in our UI ToolKit which are already connected to style, so you can just import them and style in your global style/theme.