Skip to content

siddharthkp/css-constructor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

50 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CSS constructor πŸ’„ for React components

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!

πŸ’„ Offical library emoji

--

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.

--

How does it work?

πŸ’„ uses ES7 class method decorators on the render function. Detailed post coming soon.

Inspiration

Heavily inspired from glamor, styled-components and radium

Special thanks to thysultan. stylis is the bomb!

Support

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