--
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};
/* 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 real css
π₯ use props in css
πΌ Attaches styles to the highest element in your component
π Uses classes instead of inline styles
πΆ Super tiny: only 1.2K gzipped
π pseudo selectors
π± media queries support
πͺ nested css
π Offical library emoji
--
Coming soon
π» vendor prefixes
--
-
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.
--
You can start using this right away. Even though π is in BETA
, the @css
block will always look the same.
The implementation is still fragile, report bugs if find any. Or, bookmark with a βοΈ and check back soon.
π uses ES7 class method decorators on the render function. I'll probably write a detailed post about it.
Heavily inspired from glamor, styled-components and radium
If you think π is useful for your project, βοΈ this repo for my motivation ππ»