react-stylish
Make your React component style-able by all.
Guide
React users have many different ways for styling their components. A mix between using good ole classNames and a million different ways of specifying inline styles.
If you're trying to make a general purpose React Component and want to allow your users to style it however they please, you can use this tiny little module and forget about it.
Basically you accept a styles
prop, which is an object of either classNames or
inline styles. Then you just pass the result over to your elements and move on.
If you are using the ES7 object spread operator it's as simple as this:
; Component { const myElement = ; return <div ...myElement/>; }
Enable this in Babel by using either
babel-preset-stage-2
orbabel-plugin-transform-object-rest-spread
.
If you are not using the spread operator you can use:
; Component { const myElement = ; return <div className=myElementclassName style=myElementstyles/>; }
Don't forget about validating PropTypes
! You can accept an object with any
keys or you can specify the exact keys you want to allow.
; Component static propTypes = // this will accept any keys: styles: PropType // or if you want to validate the exact keys to accept: styles: PropType // and if you want to make it required: styles: PropTypeisRequired ;
Install
$ npm install --save react-stylish
Usage
Setup your component like this:
;; Component static propTypes = styles: PropTypeisRequired ; { const styles = ; return <div ...stylesfoo> <div ...stylesbar> thispropschildren </div> </div> ; }
and users will consume it like this:
; Component { return <MyComponent styles= foo: 'a-nice-class-name' bar: background: 'red' color: 'blue' /> ; }
License
MIT © James Kyle