GithubHelp home page GithubHelp logo

siddharthkp / css-constructor Goto Github PK

View Code? Open in Web Editor NEW
99.0 5.0 1.0 970 KB

๐Ÿ’„ CSS constructor for React components

Home Page: https://codesandbox.io/s/jl5jp0y5nw

License: MIT License

JavaScript 99.98% HTML 0.02%
css-in-js react decorators

css-constructor's Introduction

CSS constructor for React

ย 

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';                  // ๐Ÿ‘ถ Super tiny: only 1.2K gzipped!

export default class Hello extends React.Component {

    /* javascript constructor */
    constructor (props) {
        super(props);
    }

    /* css constructor */                           
    @css`                                           // ๐Ÿ”’ Isolated and co-located
        font-size: 16px;                            
        text-align: center;                         // ๐ŸŽ€ Supports the entirety of CSS

        color: {this.props.color};                  // ๐Ÿ”ฅ Use props in css

        display: flex;                              // ๐Ÿ’ป Built in vendor prefixing

        &:hover {                                   // ๐ŸŒ€ Pseudo selectors
            color: #FFF;
        }

        img {                                       // ๐Ÿ‘ช Nested css
            border-radius: 50%;
        }
        #handle {
            margin-top: 20px;
        }

        @media (max-width: 600px) {                 // ๐Ÿ“ฑ Media queries support
            & {font-size: 18px;}
        }
    `

    render () {
        return (<div>                               // ๐Ÿ”ผ Attaches class to the highest element
            <img src="https://github.com/siddharthkp.png"/>
            <div id="handle">@siddharthkp</div>
        </div>)
    }
};

// <Hello color='papayawhip'/>

--

Other features

๐Ÿ™‹ Uses classes instead of inline styles

๐Ÿ”ง Editable in developer tools

๐Ÿ‘ถ Super tiny: only 1.2K gzipped!

๐Ÿ’„ Official library emoji

Coming soon

๐ŸŒ server side rendering

--

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

โญ๏ธ this repo!

css-constructor's People

Contributors

siddharthkp avatar vikramsunilpatil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

zenobht

css-constructor's Issues

Alternative syntax (without decorators/babel transform)

Alternate syntax

โœ… No decorators = no extra babel transforms
โœ… CSS constructor looks like other class methods
โœ… Position independent (don't have to place it right above render)
โš ๏ธ Code is more verbose
โš ๏ธ Adds a wrapper

import React from 'react';
import css from 'css-constructor';

class Hello extends React.Component {

    /* javascript constructor */
    constructor (props) {
        super(props);
    }

    /* css constructor */
    css (props) {
        return `
            font-size: 16px;
            text-align: center;
            color: {this.props.color};
            font-family: monospace;
        `;
    }

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

export default css(Hello);

Chrome 66

Last night, I successfully built the demonstration application, feeding it through gulp to get a nice bundle.js that I could load into a Web browser from my local Apache Web server. I had it working exactly as expected in both Edge and Chrome. This morning, I modified it to implement the suggestion discussed in Alternative syntax (without decorators/babel transform) #1, at https://github.com/siddharthkp/css-constructor/issues/1, and switched over to Chrome to have a look. To my dismay, I discovered that the page rendered as shown below.

css_constructor_chrome_66

On further investigation, I discovered that Chrome was now at version 66, having updated itself at the start of my new work session. As a further test, I reverted to the original decorated implementation, with the same result. Finally, I displayed the reverted page in Edge, where I found that it rendered as it had last night, as shown below.

css_constructor_edge

Next, I turned my attention to my global browserslist, which happens to be the default list. As I explained and showed at https://github.com/browserslist/browserslist/issues/255, I discovered that the newest Chrome browser listed is 65, leading me to wonder whether that was the source of the rendering behavior change. If so, this suggests a worrisome weakness in the implementation of either browseerlist or something else in the build pipeline.

I don't know enough to answer that question. For now, I suppose that I am stuck with using Edge for testing, until the default list is updated to cover Chrome 66. I am keen to learn if there is a better way to resolve this issue. Hailing frequencies are open.

How does it work?

Hi, @siddharthkp!

This is terrific, man! I really like your approach ๐Ÿ‘
Could you, please, tell me.. How is it supposed to work? Does it compile to css in the end? Or is it possible to accumulate all these properties to one separate css file?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.