Cascading styles for ReactNative. A components for easy styling you React Native application.
npm install --save react-native-stylable
// app.js
import React from 'react'
import { StyleProvider } from 'react-native-stylable'
import AppView from './AppView'
import styleSheet from './styles'
const App = () => <StyleProvider styleSheet={styleSheet}>
<AppView/>
</StyleProvider>
export default App
// styles.js
import { Stylesheet } from 'react-native-stylable'
const s = new Stylesheet()
// Global styles, theme code, etc...
// Will overwrite defaults...
s.addRules({
'baseText': {
style: { fontSize: 8 }
}
})
export default s
// some component, Item.js for ex
import PropTypes from 'prop-types'
import React from 'react'
import { Image, Text, View } from 'react-native'
import { stylable } from 'react-native-stylable'
import styleSheet from './styles'
// default styles for our component
styleSheet.addDefaultRules({
Item: {
style: {
borderWidth: 1,
borderColor: 'rgb(240,240,240)',
margin: 4,
flex: 1
}
},
'Item Description': {
style: {
marginHorizontal: 12
}
},
'Item PriceText': {
mixins: ['baseText'],
style: {
fontWeight: 'bold'
}
},
'Item DescriptionText': {
mixins: ['baseText'],
style: {
marginVertical: 6
}
},
'Item Image': {
style: {
left: 0,
right: 0,
height: 160
}
}
})
const Description = stylable('Description')(View)
const DescriptionText = stylable('DescriptionText')(Text)
const Image = stylable('Image')(Image)
const PriceText = stylable('PriceText')(Text)
class Item extends React.Component {
static propTypes = {
modelItem: PropTypes.object.isRequired
}
render () {
const { modelItem, ...other } = this.props
const price = modelItem.price + modelItem.currency
const uri = modelItem.imageUri
return <View {...other}>
<Image source={{uri}} />
<Description>
<DescriptionText>{modelItem.name}</DescriptionText>
<PriceText>{price}</PriceText>
</Description>
</View>
}
}
export default stylable('Item')(Item)
import { StyleSheet, StyleProvider, stylable } from 'react-native-stylable'
Holds style rules.
Creates new empty style sheet.
const styleSheet = new StyleSheet()
Add default (low priority) rules to style sheet. This method intended to be called from custom components code to provide default styles.
Add normal rules. This method intended to be called from application startup to provide customizations.
Each rule has selector and properties.
Selector is an element name or sequence of names separated by space
(same as descendant selectors in CSS). Also any element name can be suffixed with variant with dot symbol
(ex: Button.active ButtonTitle
), variants works like classes in CSS.
Properties is an object, that can contains mixins
, props
and style
keys.
mixins
- Array of others element names.props
- Properties to be added to React component.style
- Styles to be added tostyle
of React component.
React component that provides StyleSheet to other stylable components.
<StyleProvider styleSheet={styleSheet}>{other app components}</StyleSheet>
Return function (fn(component:Component):Component
) to produce Higher Order Component with name
that
pass props and style from StyleSheet to wrapped component. Also generated component can accept one additional
property - variant
to dynamically change styles.
Source code is licensed under the MIT License.