Stylis
- ~1Kb minified+gzipped
- ~3kb minified
Stylis is a small css compiler that turns this
;
Where styles
is the following css
// flat css font-size: 2em;font-family: sans-serif; // polyfill for shadow dom selectors :host :host(.fancy) :host-context(body) // removes line comment .name // inject to global scope @global // inject to global scope inline span, h1, :global(h2) // prefixing & // namespaced animations &:before // namespaced keyframes // flat namespaced css in @media // nested h1
into this (minus the whitespace)
@-}@}{ }
Supports
- Edge
- IE 9+
- Chrome
- Firefox
- Safari
- Node.js
Installation
direct download
CDN
npm
npm install stylis --save
API
; // namespaceAnimations and namespaceKeyframes allow // you to prevent keyframes and animations// from being namespaced
Intergration
You can use stylis to build an abstraction ontop of, for example imagine we want to build an abstract that makes the following React Component possible
Component { return ` &{ color: blue } `; } { return React ; }
We could simply extend the Component class as follows
ReactComponentprototype { var namespace = thisdisplayName; return { ; mounted = true; this; }}
Then use it in the following way
Component { return ` &{ color: blue } `; } { return React ; }
When the first instance of the component is mounted the function assigned to the ref will get executed adding a style element with the compiled output of stylesheet()
where as only the namespace attribute is added to any subsequent instances.
You can of course do this another way
Component { superprops; // or you can even inline this thisstyle = React; } { return ` &{ color: blue } `; } { return React ; }
One will add it to the head another will render it in place with the component.
If you want a better picture into what can be done, there is an abstraction i created for dio.js that does away with the above boilerplate entirely http://jsbin.com/mozefe/1/edit?js,output