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

@infctr/bem-cn

v2.1.3

Published

Friendly BEM class names generator, greate for React

Downloads

2

Readme

BEM class names generator

Build Status Coverage Status

Friendly BEM class names generator. Great for React.

Bem-cn (aka BEM Class Name) is extra small (minified+gzipped less than 1.5Kb) and extremely simple client-side library and Node.js module.

Inspired by b_.

Justification

I spent a lot of time finding BEM class name generator, that meets my needs:

  • Simple usage with React
  • Support modifiers without value
  • Mix multiple blocks
  • Friendly chainable API

When my efforts had led to naught I've created this micro library.

Install

With Node.js:

npm i --save bem-cn

Or use Bower for install:

bower install --save bem-cn

Works with webpack and browserify:

// CommonJS
var block = require('bem-cn');

// or ES6 modules
import block from 'bem-cn';

Cheat sheet

var b = block('button');

// Block
b; // 'button'
b(); // 'button'

// Element
b('icon'); // 'button__icon'

// Modifier
b({type: 'text'});  // 'button button_type_text'
b({onlykey: true});  // 'button button_onlykey'
b({without: false});  // 'button'

b('icon', {name: 'check'}); // 'button__icon button__icon_name_check'
b('icon')({name: 'check'}); // 'button__icon button__icon_name_check'

// Mix another classes
b('icon').mix('another'); // 'button__icon another'
b('icon').mix(['one', 'two']); // 'button__icon one two'

// States like in SMACSS: https://smacss.com/book/type-state
b.state({hidden: true}); // 'button is-hidden'
b.state({hidden: false}); // 'button'
b.state({hidden: true, error: true}); // 'button is-hidden is-error'

// More states!
b.is({loading: true}); // 'button is-loading'
b.has({content: true}); // 'button has-content'
// Setup custom delimiters
block.setup({
    el: '~~',
    mod: '--',
    modValue: '-'
});

var b = block('block');

b('element'); // 'block~~element'
b({mod: 'value'}); // 'block block--mod-value'
// Setup namespace
block.setup({ns: 'ns-'});

var b = block('block');

b(); // 'ns-block'
b('element'); // 'ns-block__element'
b({mod: 'value'}); // 'ns-block ns-block_mod_value'

Try it with React

import block from 'bem-cn';
import React from 'react';
import ReactDOM from 'react-dom';

let b = block('popup');

let Popup = React.createClass({
    render() {
        let {skin, children} = this.props;

        return (
            <div className={b}>
            	<span className={b('icon')} />
            	<div className={b('content', {skin})}>
            		{children}
            	</div>
            </div>
        );
    }
});

ReactDOM.render(<Popup skin="bright">Hello!</Popup>, target);

/*
<div class="popup">
	<span class="popup__icon"></span>
	<div class="popup__content popup__content_skin_bright">
        Hello!
	</div>
</div>
 */

Troubleshooting

PropTypes warnings

bem-cn has specific chainable API. As a result, each call returns function for a further call. But most components are expecting property className as a string and using propTypes object for check this. In this case, you will see a warning. There are the couple of ways to avoid these warnings below.

#1

Use final call without arguments to get a string

<CustomComponent className={b('icon')()} />

#2

Use explicit call of method toString():

<CustomComponent className={b('icon').toString()} />

#3

Use less specific propTypes rules:

let CustomComponent = React.createClass({
    propTypes: {
        className: React.PropTypes.oneOf([
            React.PropTypes.string,
            React.PropTypes.func
        ])
    },
    // ...
});

ES3 browsers

bem-cn is fully compatible with ES5 browsers. If you are going to support ES3 browsers than just use es5 shim.