Coder Social home page Coder Social logo

knowncitizen / hyperstyles Goto Github PK

View Code? Open in Web Editor NEW

This project forked from colingourlay/hyperstyles

0.0 1.0 0.0 14 KB

Transparently apply CSS Modules to hyperscript-compatible DOM builders, such as virtual-hyperscript and React

Home Page: https://www.npmjs.com/package/hyperstyles

JavaScript 100.00%

hyperstyles's Introduction

hyperstyles

Transparently apply CSS Modules to hyperscript-compatible DOM builders, such as virtual-hyperscript and React.

$ npm install hyperstyles
var h = require('hyperstyles')(require('virtual-dom/h'), require('./car.css'));

module.exports = function render() {
    return h('div.root', [
        h('div.front-door'),
        h('div.back-door')
    ]);
}

Usage

To use CSS Modules, you'll need to set up your module bundler to load Interoperable CSS.

Once your build process is configured you can use hyperstyles!

In ES2015, using virtual-hyperscript:

import vh from 'virtual-dom/h';
import styles from './car.css';
import hyperstyles from 'hyperstyles';

const h = hyperstyles(vh, styles);

function render() {
    return h('div', {styleName: 'root'}, [
        h('div', {styleName: 'front-door'}),
        h('div', {styleName: 'back-door'})
    ]);
}

export default render;

In JSX, using React.createElement:

/** @jsx h */

var React = require('react');
var styles = require('./car.css');
var hyperstyles = require('hyperstyles');
var h = hyperstyles(React.createElement, styles);

var Car = React.createClass({
    render: function () {
        return (
            <div styleName="root">
                <div styleName="front-door"></div>
                <div styleName="back-door"></div>
            </div>
        );
    }
});

module.exports = Car;

In ES5, using React.createElement:

var React = require('react');
var styles = require('./car.css');
var hyperstyles = require('hyperstyles');
var h = hyperstyles(React.createElement, styles);

var Car = React.createClass({
    render: function () {
        return h('div.root', [
            h('div.front-door'),
            h('div.back-door')
        ]);
    }
});

module.exports = Car;

Note that we use the styleName property instead of className to denote classes we want to replace from the CSS module. You can use them together and classNamess will remain as they are, with styleNames appended.

Partial application

If you just supply a single argument (a hyperscript-compatible function) to hyperstyles, it'll return a function which you can then call multiple times with different CSS modules to create multiple wrapped functions.

var vh = require('virtual-dom/virtual-hyperscript');
var hyper = require('hyperstyles')(vh);
var hCar = hyper(require('./car.css'));
var hBike = hyper(require('./bike.css'));

function renderCar() {
    return hCar('div.root', [
        hCar('div.front-door'),
        hCar('div.back-door')
    ]);
}

function renderBike() {
    return hBike('div.root', [
        hBike('div.front-wheel'),
        hBike('div.back-wheel')
    ]);
}

This is useful if you expose your partially applied function as a module, which each of your components can include and call with their own styles.

tagName shorthand

You can use the tagName shorthand (as demonstrated in the first example) to quickly set the id and className properties on the node you are creating. Any CSS classes you put the shorthand format will be treated as if you passed them in as the styleName property rather than the className property, and as such, will be transformed by hyperstyles. The shorthand will even work with React, as long as you're not using JSX.

License

ISC

hyperstyles's People

Contributors

colingourlay avatar

Watchers

 avatar

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.