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

bem-classnames-loader

v2.0.1

Published

Use BEM class names defined in your CSS files

Downloads

31

Readme

#bem-classnames-loader for webpack

npm

This loader extracts modifiers and states defined in your css files and then provide an interface for generating class names. So you get a something similar with css-modules but with BEM.

##Installation

npm install bem-classnames-loader --save-dev

##Examples

button.scss

.button {
  color: white;
  cursor: pointer;

  &__inner {
    font-size: 16px;
  }

  &--borderless {
    border: none;
  }

  &--type-default {
    background-color: gray;
  }

  &--type-success {
    background-color: green;
  }

  &.is-disabled {
    opacity: .5;
    cursor: default;
  }
}

button.js

import style from './button.scss';

style('button') // button
style('button', { disabled: true }) // button is-disabled
style('button', { borderless: true }) // button button--borderless
style('button', { disabled: true, type: 'success' }) // button is-disabled button--type-success
style('button', { disabled: true }, 'form__button') // button is-disabled form__button
style('button', { disabled: true }, { type: 'default' }, 'form__button') // button is-disabled button--type-default form__button

style('&') // button
style('&inner') // button__inner
style('button__inner') // button__inner

webpack.config.js

...
// Optional parameters (you can pass them with loader query too)
bemClassnames: {
  prefixes: {
    state: 'is-'
  }
},
module: {
  loaders: [
    {
      test: /\.scss$/,
      loader: 'bem-classnames!style!css!sass'

      // If you using extract-text-plugin
      // loaders: ['bem-classnames', ExtractTextPlugin.extract('css!sass')]
    }
  ]
}
...

##React component example This example shows how easy you can use props to generate class names.

import React, { Component, PropTypes } from 'react';
import style from './button.scss';

export default class Button extends Component {
    
  static propTypes = {
    disabled: PropTypes.bool,
    borderless: PropTypes.bool,
    type: PropTypes.oneOf([ 'success', 'default' ]);
  };

  static defaultProps = {
    type: 'default'
  };

  render() {
    return (
      <button className={style('&', this.props)}>
        <div className={style('&inner')}>
          Click me
        </div>
      </button>
    );
  }
  
};

Now render Button with different props:

<Button /> //button button--type-default
<Button borderless /> //button button--type-default button--borderless
<Button type="success" /> //button button--type-success
<Button type="success" disabled /> //button button--type-success is-disabled

##Loader options

{
  prefixes: {
    element: '__',
    modifier: '--',
    state: 'is-',
    modifierValue: '-',
    stateValue: '-'
  },
  applyClassPrefix: ''
}
  • prefixes - define bem entity prefixes
  • applyClassPrefix - prefix will be added to class names. For example, you use postcss-loader and it's postcss-class-prefix plugin to add prefixes in your css. So you should use applyClassPrefix to add prefixes on Javascript side.

##API

import style from './button.scss';

##style Itself is a function, which generates class names in cool way.

##style.ns Get/set namespace. Sometimes class name is very large, namespaces help you to write lesser code.

Example:

style('&') // button
style('&inner') // button__inner

// Set new namespace if you need
style.ns('super-good-component');

style('&') // super-good-component
style('&placeholder') // super-good-component__placeholder

##style.modifier Adds new modifier.

Example:

// Add boolean modifier
style.modifier('button', 'fade');
style('button', { fade: true }) // button button--fade

// Add string modifier
style.modifier('button', 'size', ['sm', 'lg']);
style('button', { size: 'sm' }) // button button--size-sm

##style.state Adds new state.

Example:

// Add boolean state
style.state('button', 'active');
style('button', { active: true }) // button is-active

// Add string state
style.state('button', 'foo', ['bar']);
style('button', { foo: 'bar' }) // button is-foo-bar

##style.getClasses Returns defined classes.