npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hyperstyles

v3.3.1

Published

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

Downloads

9

Readme

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