You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ES7 decorator for dynamic stylesheet usage w/ webpack
install
$ npm install bloody-react-styled --save-dev
require
importstyledfrom"bloody-react-styled"
API
@styled(styles) class
styled is a ES7 decorator that applies useable style only if at least one
instance of the component it is attached to is in mounted,
and removes it when there are no more instances.
how to
first, install style-loader, css-loader and possibility the loader
of your choice for a pre/post-processor.
$ npm install --save-dev style-loader css-loader
to configure webpack for that, use in your webpack.config.js :
constconfig={// ...module : {loaders : [// ...{test : /\.css$/,loaders: [// use the useable to only use the stylesheet when necessary"style/useable","css",// example of css processor you might want to use"cssnext",],},],},// ...}
then you can do
importReact,{Component}from"react"importstyledfrom"bloody-react-styled"importstylesfrom"./styles.css"
@styled(styles)classMyComponentextendsComponent{render(){return(<divclassName="MyComponent">
will be styled!
</div>)}}exportdefaultMyComponent
local stylesheets with css-loader
css
:local .default {
padding:1rem;
}
js
importReact,{Component}from"react"importstyledfrom"bloody-react-styled"importstylesfrom"./styles.css"
@styled(styles)classMyComponentextendsComponent{render(){const{locals}=stylesreturn(<divclassName={locals.default}>
will be styled!
</div>)}}exportdefaultMyComponent