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

deku-css-modules

v2.0.1

Published

mapping of class names to CSS modules inside Deku components

Downloads

8

Readme

deku-css-modules

npm version

Mapping of class names to CSS modules in Deku components

CSS Modules

CSS Modules uses a module bundler such as webpack to load CSS scoped to a particular document.

CSS module loader will generate a unique name for a each CSS class at the time of loading the CSS document

CSS Modules with Deku looks like this:

/** @jsx element */
import { element } from 'deku';
import styles from './table.css';

let Table = {
    render () {
        return <div class={styles.table}>
            <div class={styles.row}>
                <div class={styles.cell}>A0</div>
            </div>
        </div>;
    }
}

Rendering the component will produce a markup similar to:

<div class="table__table___32osj">
    <div class="table__row___2w27N">
        <div class="table__cell___2w27N">A0</div>
    </div>
</div>

and a corresponding CSS file that matches those CSS classes... Awesome!

deku-css-modules

Similar to React CSS Modules, Deku CSS Modules automates loading of CSS Modules using the styleName property.

Check out the deku-webpack-example

/** @jsx element */

import { element } from 'deku';
import CSSModules from 'deku-css-modules.js';
import styles from './table.css';

const Table = function () {
    return (
        <div styleName='table'>
            <div styleName='row'>
                <div styleName='cell'>A0</div>
            </div>
        </div>
    )
}

export default CSSModules(Table, styles);

Benefits of using deku-css-modules:

  • You are not forced to use camelCase naming convention.
  • You do not need to refer to the styles object every time you use a CSS Module.
  • There is clear distinction between global CSS class and CSS Modules styleName

Implementation

deku-css-modules extends the render method of the target component. It uses the value of styleName to look for CSS Modules in the associated styles object and appends the matching unique CSS class names to each Elements className property value.

Usage

Bundlers

webpack

Check out the example deku-webpack-example

Extending Component Styles

Use styles property to overwrite the default component styles.

Explanation using Table component:

/** @jsx element */
import { element } from 'deku';
import CSSModules from 'deku-css-modules.js';
import styles from './table.css';

const Table = function () {
    return (
        <div styleName='table'>
            <div styleName='row'>
                <div styleName='cell'>A0</div>
            </div>
        </div>
    )
}

export default CSSModules(Table, styles);

In this example, CSSModules is used to decorate Table component using ./table.css CSS Modules. When Table component is rendered, it will use the properties of the styles object to construct className values.

Using styles property you can overwrite the default component styles object, e.g.

import customStyles from './table-custom-styles.css';

<Table styles={customStyles} />;

Interoperable CSS can extend other ICSS. Use this feature to extend default styles, e.g.

/* table-custom-styles.css */
.table {
    composes: table from './table.css';
}

.row {
    composes: row from './table.css';
}

/* .cell {
    composes: cell from './table.css';
} */

.table {
    width: 400px;
}

.cell {
    float: left; width: 154px; background: #eee; padding: 10px; margin: 10px 0 10px 10px;
}

In this example, table-custom-styles.css selectively extends table.css (the default styles of Table component).

Refer to the UsingStylesProperty example for an example of a working implementation.

styles Property

Decorated components inherit styles property that describes the mapping between CSS modules and CSS classes.

const render = ({props}) => {
    <div>
        <p styleName='foo'></p>
        <p class={props.styles.foo}></p>
    </div>;
}

In the above example, styleName='foo' and class={this.props.styles.foo} are equivalent.

Decorator

You need to decorate your component using deku-css-modules, e.g.

/** @jsx element **/
import { element } from 'deku';
import CSSModules from 'deku-css-modules.js';
import styles from './table.css';

const Table = function () {
    return (
        <div styleName='table'>
            <div styleName='row'>
                <div styleName='cell'>A0</div>
            </div>
        </div>
    )
}

export default CSSModules(Table, styles);

Thats it!

As the name implies, deku-css-modules is compatible with the ES7 decorators syntax:

/** @jsx element **/
import { element } from 'deku';
import CSSModules from 'deku-css-modules.js';
import styles from './table.css';

export default {
  @CSSModules(styles)
  render: () => {
    return (
        <div styleName='table'>
            <div styleName='row'>
                <div styleName='cell'>A0</div>
            </div>
        </div>
    )
  },
  onCreate: () => {
    console.log('A MyComponent entity was created!')
  }
}

Browserify

Refer to css-modulesify.

Development

npm install
npm run build
npm test
npm start

License

MIT