react-native-stylable
Cascading styles for ReactNative. A components for easy styling you React Native application.
⚠️ The package is currently in alpha stage of development. If you find a bug or missing functionality, please feel free to report, but better fix or implement what you want and send a pull request to GitHub repository.
Installation
npm install --save react-native-stylable
Usage
// app.js const App = <StyleProvider styleSheet=styleSheet> <AppView/></StyleProvider>
// styles.js const s = // Global styles, theme code, etc...// Will overwrite defaults...s
// some component, Item.js for ex // default styles for our componentstyleSheet const Description = Viewconst DescriptionText = Textconst Image = Imageconst PriceText = Text Component static propTypes = modelItem: PropTypesobjectisRequired { const modelItem ...other = thisprops const price = modelItemprice + modelItemcurrency const uri = modelItemimageUri return <View ...other> <Image source=uri /> <Description> <DescriptionText>modelItemname</DescriptionText> <PriceText>price</PriceText> </Description> </View> } 'Item'Item
API
Stylesheet
Holds style rules.
Constructor
Creates new empty style sheet.
const styleSheet =
addDefaultRules(rules:Object)
Add default (low priority) rules to style sheet. This method intended to be called from custom components code to provide default styles.
addRules(rules:Object)
Add normal rules. This method intended to be called from application startup to provide customizations.
Rules object
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.
StyleProvider
React component that provides StyleSheet to other stylable components.
<StyleProvider styleSheet={styleSheet}>{other app components}</StyleSheet>
stylable(name:String):Function
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.
Author
License
Source code is licensed under the MIT License.