Every React component gets an inbuilt javascript constructor for functional logic.
Introducing the css constructor for styling!
import React from 'react';
import css from 'css-constructor';
export default class Hello extends React.Component {
/* javascript constructor */
constructor (props) {
super(props);
}
/* css constructor */
@css`
font-size: 16px;
text-align: center;
/* Use props in your CSS */
color: {this.props.color};
/* Adds vendor prefixes */
display: flex;
/* Pseudo selectors */
&:hover {
color: #FFF;
}
/* Nested elements */
img {
border-radius: 50%;
}
#handle {
margin-top: 20px;
}
/* Media queries */
@media (max-width: 600px) {
& {font-size: 18px;}
}
`
render () {
return (<div>
<img src="https://github.com/siddharthkp.png"/>
<div id="handle">@siddharthkp</div>
</div>)
}
};
// <Hello color='papayawhip'/>
--
Features:
π Supports the entirety of CSS
π₯ Use props in css
πΌ Attaches styles to the highest element in your component
π Uses classes instead of inline styles
π Isolated and co-located
π pseudo selectors
π± media queries support
πͺ nested css
π» built in vendor prefixes
π§ Editable in developer tools
πΆ Super tiny: only 1.2K gzipped!
π Official library emoji
--
-
npm install css-constructor --save
-
import css from 'css-constructor'
-
Add a
@css
block just before therender
function (important) -
Add
transform-decorators-legacy
as the firstplugin
in your.babelrc
(already downloaded with π).
If you are not familiar with babel plugins
you can follow the detailed instructions here.
Or, if you would prefer using π without adding the babel transform for decorators, up-vote this issue.
--
π uses ES7 class method decorators on the render function. Detailed post coming soon.
Heavily inspired from glamor, styled-components and radium
Special thanks to thysultan. stylis is the bomb!
If you think π is useful for your project, βοΈ this repo for my motivation ππ»