Skip to content

siddharthkp/css-constructor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CSS constructor πŸ’„ for React components

beta

--

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

--

Usage

  1. npm install css-constructor --save

  2. import css from 'css-constructor'

  3. Add a @css block just before the render function (important)

  4. Add transform-decorators-legacy as the first plugin 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.

Beta

The implementation is still fragile, report bugs if find any. Or, bookmark with a ⭐️ and check back soon.

How does it work?

πŸ’„ uses ES7 class method decorators on the render function. I'll probably write a detailed post about it.

Inspiration

Heavily inspired from glamor, styled-components and radium

Support

If you think πŸ’„ is useful for your project, ⭐️ this repo for my motivation πŸ™‡πŸ»