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

@gtechdoodler/bem-it

v3.0.2

Published

A BEM util to help with consistent element class naming in components.

Downloads

864

Readme

bem-it

A simple js object providing BEM class naming support and consistency for an individual or software team. Provides consistent BEM class names, and a clean api that doesn't bloat a component with untidy string concatenations.

The code has been thoroughly tested and test scripts are available, with source, in the npm-utils project here github. If you discover any bugs then please report. Thank you :).

Installation

Install from npm:

npm install @gtechdoodler/bem-it

How to Use

The examples provided are implemented in a React functional component...

Start by importing the BemIt object into your component, then create a new instance passing your component name to the constructor.

import React from 'react';
import BemIt from '@gtechdoodler/bem-it';

export default function() {
  const bem = new BemIt('Container');

  return (
    <div className={bem.out()}>
    </div>
  )
}

Calling bem.out() will output: Container

You always call .out() to output the class name value. Calling .out() also flushes the bem object, ensuring it's in a clean state, ready for your next statement.

Adding a Child Element

The examples from this point on, will include only the component, omitting the import statements.

export default function() {
  const bem = new BemIt('Container');

  return (
    <div className={bem.out()}>
      <div className={bem.el('content').out()}>
      </div>
    </div>
  )
}

Calling bem.el('content').out() will output: Container__content

Adding a Modifier to That Child Element

export default function() {
  const bem = new BemIt('Container');

  return (
    <div className={bem.out()}>
      <div className={bem.el('content').mod('show').out()}>
      </div>
    </div>
  )
}

Calling bem.el('content').mod('show').out() will output: Container__content Container__content--show

Notice we are following the official BEM standard here, outputting the Block__element and an additional Block__element--modifier to represent the modifier.

Multiple Modifiers as Array

For ternary operand false, pass an empty string, undefined, or null. This will ensure the exclusion of the modifier.

export default function({isFullScreen, isLoading, ...props}) {
  const bem = new BemIt('Container');
  const className = bem.mod([
    isFullScreen ? 'full-screen' : '',
    isLoading ? 'loading' : ''
  ]).out();

  return (
    <div className={className}>
    </div>
  )
}

Multiple Modifiers as Object

Anything falsy will be ignored.

export default function({isFullScreen, isLoading, ...props}) {
  const bem = new BemIt('Container');
  const className = bem.mod({
    'full-screen' isFullScreen,
    'loading': isLoading
  }).out();

  return (
    <div className={className}>
    </div>
  )
}

Include a Custom Class Name Passed as a Prop

To combine a class name passed as a prop, with bem output, you can import a function called addClass.

import BemIt, { addClass } from '@gtechdoodler/bem-it';

And implement as follows:

export default function({className, ...props}) {
  const bem = new BemIt('Container');

  return (
    <div className={addClass(className).before(bem)}>
    </div>
  )
}

If the className is falsy then it will be ignored, outputting only the bem class name. Also, you can flip the class names around, adding a custom class after a bem output, by calling addClass(className).after(bem).

Multiple Classes With a Single Statement

If you really must represent an element with mutiple class names, this is achieveable with chaining. Call and.

export default function() {
  const bem = new BemIt('Container');

  return (
    <div className={bem.out()}>
      <div className={bem.el('content').and.el('detail').out()}>
      </div>
    </div>
  )
}

Calling bem.el('content').and.el('detail').out() will output: Container__content Container__detail

TypeScript Declarations

These are exported, so if you're using TypeScript then have a play around... the api is light and chainable, so, if you want something wacky like:

bem.el('content').mod('show').and.el('content').el('summary').mod('highlight').out()

But please don't write code like this :).