Skip to content

siddharthkp/css-constructor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

26 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;
        color: {this.props.color};
        font-family: monospace;
    `

    render () {
        return <div>Styled {this.props.color} text!</div>
    }
};

// <Hello color='papayawhip'/>

--

Features:

πŸŽ€ Supports real css

πŸ”₯ use props in css

πŸ”Ό Attaches inline styles to the highest element in your component

πŸ‘Ά Super tiny: only 1.4K gzipped

πŸ’„ Offical library emoji

--

Coming soon

πŸ™‹ classes instead of inline styles

πŸŒ€ pseudo selectors

πŸ“± media queries support

πŸ’» vendor prefixes

πŸ‘ͺ nested css

--

Usage

  1. npm install css-constructor --save

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

  3. 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.

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 πŸ™‡πŸ»