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

@jedmao/react-bem

v0.4.3

Published

BEM helper functions and HOCs for React.

Downloads

16

Readme

react-bem

NPM version npm license Travis Build Status Dependency Status

npm

BEM helper functions and HOCs for React.

Introduction

This library allows you to write BEM block and element components in such a way that the block saves its name in context, later to be accessed by any number of BEM element components. A block component might look something like this:

// Foo.tsx
import { createBEMBlock } from '@jedmao/react-bem'
import React from 'react'
import Bar from './Bar'

class Foo extends React.Component {
  static defaultProps = {
    bemBlock: 'foo'
  }
  render() {
    return (
      <div bemModifiers="mod1 mod2">
        <Bar />
        <div bemElement="baz" bemModifiers={['mod3', { mod4: true }]}>
          qux
        </div>
      </div>
    )
  }
}

export default createBEMBlock(Foo)

The Bar component here is an BEM element component and looks like this:

// Bar.tsx
import { createBEMElement } from '@jedmao/react-bem'
import React from './React'

class Bar extends React.Component {
  static defaultProps = {
    bemElement: 'bar'
  }
  render() {
    return (
      <div>
        <div bemElement="corge" className="save-me">
          garpley
        </div>
      </div>
    )
  }
}

export default createBEMElement(Bar)

Rendering the Foo block above would produce the following HTML:

<div className="foo foo--mod1 foo--mod2">
  <div className="foo__bar">
    <div className="foo__corge save-me">
      garpley
    </div>
  </div>
  <div className="foo__baz foo__baz--mod3 foo__baz--mod4">
    qux
  </div>
</div>

Some things to note

  • The block name of foo has been preserved in context and provided to the Bar element, so that it's always available.
  • All block, element and modifiers props have been consumed. They are not rendered in the final HTML.
  • The BEM props have been resolved and inserted into the className prop. The existing className is preserved.
  • The Bar component could be used inside a different block, in which case the foo__corge classname would be ${block}__corge.

Tips for writing scalable and maintainable BEM components

  • BEM blocks should be extremely portable, so the smaller the better. Try your best to keep it decoupled from other components.
  • Try to keep most of your modifiers at the root node of the block. Instead of .foo__bar--dark, you can usually move the --dark modifier up so that you can target the __bar element like .foo--dark > .foo__bar. In this way, you can use the same --dark modifier for any number of other elements w/o scattering the same modifier everywhere.
  • If you find yourself creating elements like foo__bar-baz and foo__bar-qux, this could be a code smell. Consider if there are any benefits of extracting the concept of __bar into a new Bar block.

API

createBEMBlock( ComponentClass )

Wraps a class with BEM block functionality, providing the BEM block name via context and converting block and modifiers attributes into className attributes.

createBEMBlockSFC( SFC )

Wraps an SFC with BEM block functionality, providing the BEM block name via context and converting block and modifiers attributes into className attributes.

createBEMElement( ComponentClass )

Wraps a class with BEM element functionality, receiving the BEM block name via context and converting element and modifiers attributes into className attributes.

createBEMElementSFC( SFC )

Wraps a Stateless Function Component (SFC) with BEM element functionality, receiving the BEM block name via context and converting element and modifiers attributes into className attributes.

resolveBEMNode( node, { block, element?, modifiers? } )

Walks a BEM node tree, consumes BEM props and resolves class names.


bemClassNameProp( block [, element] [, modifiers] [, options] )

Same as bemClassNames (below), but returns an object with a className prop:

{
  className: 'foo'
}

bemClassNames( block [, element] [, modifiers] [, options] )

Joins a BEM block or element with any number of potentionally deeply nested modifiers.

getDisplayName( class )

Attempts to get the displayName of a class. Falls back to name prop or "Component".

isFunction( value )

Checks if value is classified as a Function object.

isString( value )

Checks if value is classified as a String primitive or object.

omit( object, [paths] )

Creates an object composed of the own and inherited enumerable property paths of object that are not omitted.

omitBEMProps( object )

Omits block, element and modifiers props from an object.

TypeScript

This project is written in TypeScript, so the TypeScript definitions come for free. Here are some interfaces you might be able to use in your project:

  • BEMBlockClass
  • BEMBlockProps
  • BEMClassNamePropOptions
  • BEMClassNamesOptions
  • BEMElementClass
  • BEMElementProps
  • BEMNode
  • ReactBEMElement
  • ReactBEMElementProps
  • ReactElementProps
  • ReactRenderResult

Testing

Run the following command:

$ npm test

This will build scripts, run tests and generate a code coverage report. Anything less than 100% coverage will throw an error.

Watching

For much faster development cycles, run the following commands in 2 separate processes:

$ npm run build:watch

Compiles TypeScript source into the ./dist folder and watches for changes.

$ npm run watch

Runs the tests in the ./dist folder and watches for changes.