Transparently apply CSS Modules to hyperscript-compatible DOM builders, such as virtual-hyperscript and React.
npm install hyperstylesTransparently 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:
`js
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:
`html`
To use CSS Modules, you'll need to set up your module bundler to load Interoperable CSS.
* webpack setup example
* browserify setup example
Once your build process is configured you can use hyperstyles!
`jsx
/* @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 (
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.$3
`js
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'})
]);
}
`$3
`js
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;
`$3
`js
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
;
};
`
Tips
Here's a couple of ways to get the most out of hyperstyles
$3
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:`js
import hyperstyles from 'hyperstyles';
import h from 'virtual-dom/h';export default hyperstyles(h);
`In
car.js:`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:`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')
]);
}
`$3
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:
* React
* virtual-hyperscript
* hyperx (wrapping virtual-hyperscript or React)
* hyperscript
It has also been succesfully used with crel, which cannot be tested outside a browser environment.
ISC