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-classes

v2.0.1

Published

Easy BEM classes

Downloads

7

Readme

bem-classes

Fast and powerful classnames utility for in your javascript application. Easy to use and fast to type interface for creating BEM classnames in your client-side applications.

const block = bem('block', { $big: truthy })
// 'block block--big'
const element = block('element', {
  $modifier: truthy,
  extraClass: true
})
// 'block__element block__element--modifier extraClass'

Install

npm install bem-classes

Usage

Require

You can use any variable name you like.

import bem from 'bem-classes'
var bem = require('bem-classes')

You can also grab the bem-classes.js file from the repository.

Standard class strings

Pass the arguments to the imported function and it returns a classes facotry. This instance implements the toString method which will return the class string when a string is expected. You can also call .toString() explicitly to return the class string.

bem('block')
  // => { [Function: factory] toString: [Function] }
bem('block').toString()
  // => 'block'
`${bem('block')}`
  // => 'block'

You can pass strings or key-value pairs as arguments. The key of a key-value pair only gets added as a class if the value is truthy. You can add as many arguments as you like.

`${bem('block', 'column')}`
  // => 'block column'
`${bem('block', { column: true })}`
  // => 'block column'
`${bem('block', { column: false })}`
  // => 'block'

`${bem('block', { column: false }, 'class1 class2 class3')}`
  // => 'block class1 class2 class3'
`${bem('block', { column: false }, 'class1', 'class2', 'class3')}`
  // => 'block class1 class2 class3'

BEM class strings

The first valid class becomes the block class. Use the prefix - or $ for a modifier.

`${bem('block')}`
  // => 'block'
`${bem('block', '-modifier')}`
  // => 'block block--modifier'
`${bem('block', '$modifier')}`
  // => 'block block--modifier'
`${bem('block', { '-modifier': true })}`
  // => 'block block--modifier'
`${bem('block', { $modifier: true })}`
  // => 'block block--modifier'
`${bem('block__element', '$modifier')}`
  // => 'block__element block__element--modifier'

You can extend the returned object by calling the block. This can be useful for building a React class as a block with the elements inside.

const block = bem('block', { $modifier: true })
`${block}`
// => 'block block--modifier'
`${block('element', { $modifier: true })}`
// => 'block__element block__element--modifier'

const element = bem('block')('element')
// => 'block__element'

React example

An example in react showing the power and flexibility of bem-classes

import bem from 'bem-classes'

const BemExample = (props) => {
  const block = bem('block')
  const element = block('element', {
    $modifier: props.thisPropIsTruthy,
    extraClass: true
  })
  return (
    <div className={block}>
      <div className={element}>Hello World</div>
    </div>
  )
}
<div class="block">
  <div class="block__element block__element--modifier extraClass">Hello World</div>
</div>

Test

npm test
npm run ci

Build

npm run build

Develop (watch and rebuild)

npm run watch
npm test