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
Ultra-light utility to help writing CSS inside your JS React components declarations.
Statically extracts CSS styles declared at the component class level and outputs a nicely formatted CSS string. It is then easing to pipe it to your CSS postprocessor of choice (eg. postcss).
Declare styles as a static property of your component class (or using statics if you use vanilla React.createClass):
classMyComponentextendsReact.Component{staticstyles={'.MyComponent .MyComponent-item': {// you can put build-time calculations herefontSize: 0.8*readFontSizeFromConfig()+'px',}};render: function(){return<divclassName='MyComponent'><divclassName='MyComponent-item'>This is smaller that usual.</div></div>;},};// alternate syntax using the decoratorimport{styles}from'react-statics-styles';
@styles({'.MyComponent .MyComponent-item': {fontSize: 0.8*readFontSizeFromConfig()+'px',}})classMyComponentextendsReact.Component{ ... }
Then pass one or more class definition(s) to extractStyles(class) or extractAllStyles(array of class):
import{extractStyles,extractAllStyles}from'react-statics-styles';extractStyles(MyComponent);// returns a CSS stringextractAllStyles([MyComponent1,MyComponent2, ...]);
Assuming that readFontSizeFromConfig() returns 10, then the first line returns the string: