Code Monkey home page Code Monkey logo

hyperstyles's Introduction

hyperstyles

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

$ npm install hyperstyles

Here's a quick example using virtual-hyperscript in ES2015:

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

const h = hyperstyles(vh, styles);

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

When rendered, the following markup will be produced:

<div class="Car__root___hHwf0">
    <div class="Car__front-door___i3N9f"></div>
    <div class="Car__back-door___27Guk"></div>
</div>

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!

ES2015 (JSX), using React.createElement:

/** @jsx h */

import React from 'react';
import styles from './car.css';
import hyperstyles from 'hyperstyles';

const h = hyperstyles(React.createElement, styles);

export default class Car extends React.Component {
    render () {
        return (
            <div styleName="root">
                <div styleName="front-door"></div>
                <div styleName="back-door"></div>
            </div>
        );
    }
});

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

ES5, using virtual-hyperscript:

var hyperstyles = require('hyperstyles');
var styles = require('./car.css');

var h = hyperstyles(require('virtual-dom/h'), styles);

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

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;

ES5+2015, using hyperx + virtual-hyperscript:

var h = require('virtual-dom/h')
var hyperx = require('hyperx');
var hyperstyles = require('hyperstyles');
var styles = require('./car.css');

var hx = hyperx(hyperstyles(h, styles));

module.exports = function render() {
  return hx`<div styleName="root">
        <div styleName="front-door"></div>
        <div styleName="back-door"></div>
    </div>`;
};

Tips

Here's a couple of ways to get the most out of hyperstyles

Tip A: Use 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.

In hyper.js:

import hyperstyles from 'hyperstyles';
import h from 'virtual-dom/h';

export default hyperstyles(h);

In car.js:

import hyper from './hyper';
import styles from './car.css';

const h = hyper(styles);

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

In bike.js:

import hyper from './hyper';
import styles from './car.css';

const h = hyper(styles);

export default function render() {
    return h('div.root', [
        h('div.front-wheel'),
        h('div.back-wheel')
    ]);
}

Tip B: Use the tagName shorthand

You can use the tagName className shorthand (as demonstrated in the first example) as an alternative to setting the styleName property. The shorthand will even work with React, as long as you're not using JSX.

Any CSS classes you write in the shorthand format will be transformed into the className property by hyperstyles, even if you didn't pass any properties in. If your underlying DOM creation function also recognises this kind of shorthand, you can safely include your desired element id, which will be passed through once hyperstyles has transformed the classNames.

Here's what would happen during the transform:

In Out
['div'] ['div']
['div.things'] ['div', {className: 'things__r489jf'}]
['div.things', {className: 'stuff'}] ['div', {className: 'stuff things__r489jf'}]
['div.things#stuff'] ['div#stuff', {className: 'things__r489jf'}]

Tests

$ npm test

There are currently tests to ensure hyperstyles is compatible with the following utilities:

It has also been succesfully used with crel, which cannot be tested outside a browser environment.

License

ISC

hyperstyles's People

Contributors

colingourlay avatar rtsao 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.