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

@seaneiros/react-bem

v1.2.1

Published

A set of decorators to ease bem classes management

Downloads

68

Readme

React Bem

A set of handy decorators to make BEM class management more declarative.

Installation

To use HOC decorator you should use React 15.0 or later

npm install --save @seaneiros/react-bem

This assumes that you’re using npm package manager with a module bundler like Webpack or Browserify to consume CommonJS modules.

If you don't use npm or a modern module bundler, and would rather prefer a UMD files, you can grab pre-built versions from @seaneiros/react-bem/lib/umd.

Usage

Static field

It's the default type of exported decorator (due to lower memory consumption).

Basic example

// MyComponent.js

import React from 'react';
import bem from '@seaneiros/react-bem';

const MyComponent = props => {
  const { bem } = MyComponent;

  return (
    <div className={bem.block()}>
      <div className={bem.element('element')}>
        My decorated component
      </div>
    </div>
  );
};

export default bem({ block: 'block' })(MyComponent);
// somewhere in the app

...
render() {
  return (
    <MyComponent />
  );
}

will result

<div class="block">
  <div class="block__element">
    My decorated component
  </div>
</div>

Passing props

Let's make our previous example less dumb.

// MyComponent.js

import React from 'react';
import bem from '@seaneiros/react-bem';

const MyComponent = props => {
  const { bem } = MyComponent;

  return (
    <div className={bem.block(props)}>
      <div className={bem.element('element')}>
        My decorated component
      </div>
    </div>
  );
};

export default bem({
  block: 'block',
  modifiers: [ 'inline' ],
})(MyComponent);

Notice that we pass props as the first parameter to the bem.block method.

<MyComponent className="externalClass" inline />

will turn into

<div class="externalClass block block--inline">
  <div class="block__element">
    My decorated component
  </div>
</div>

More modifiers

That was pretty cool, but what if we need to set some styles within the component?

// MyComponent.js

import React from 'react';
import bem from '@seaneiros/react-bem';

const MyComponent = props => {
  const { type } = props;
  const { bem } = MyComponent;

  const color = type === 'success' ? 'green' : 'red';

  return (
    <div className={bem.block(props, { type })}>
      <div className={bem.element('element', { color })}>
        My decorated component
      </div>
    </div>
  );
};

export default bem({
  block: 'block',
  modifiers: [ 'inline' ],
})(MyComponent);

Now we need to pass type property to compute our styles

<MyComponent
  className="externalClass"
  type="success"
  inline
/>
<div class="externalClass block block--inline block--type-success">
  <div class="block__element block__element--color-green">
    My decorated component
  </div>
</div>

Sometimes (for some reason) you may need to set more than one value to modifier. Let's improve our previous example

// MyComponent.js

import React from 'react';
import bem from '@seaneiros/react-bem';

const MyComponent = props => {
  const { type } = props;
  const { bem } = MyComponent;

  const color = type === 'success' ? 'green' : 'red';

  return (
    <div className={bem.block(props, { type, no: [ 'margin', 'border' ] })}>
      <div className={bem.element('element', { color })}>
        My decorated component
      </div>
    </div>
  );
};

export default bem({
  block: 'block',
  modifiers: [ 'inline' ],
})(MyComponent);

Now we'll get

<div class="externalClass block block--inline block--type-success block--no-margin block--no-border">
  <div class="block__element block__element--color-green">
    My decorated component
  </div>
</div>

HOC

You can also use HOC to manage styles; syntax is pretty much the same

// MyComponent.js

import React from 'react';
import { bemHoc as bem } from '@seaneiros/react-bem';

const MyComponent = props => {
  const { type } = props;
  const { bem } = MyComponent;

  const color = type === 'success' ? 'green' : 'red';

  return (
    <div className={bem.block({ type, no: [ 'margin', 'border' ] })}>
      <div className={bem.element('element', { color })}>
        My decorated component
      </div>
    </div>
  );
};

export default bem({
  block: 'block',
  modifiers: [ 'inline' ],
})(MyComponent);

Notice, that we don't need to pass props as the first parameter to bem.block method anymore.

HOC will create helper instance for every decorated component, so it's not recommended to use it for lists.

Hook

Since React v 16.8 we can use hooks in our functional components

// MyComponent.js

import React from 'react';
import { useBem } from '@seaneiros/react-bem';

const MyComponent = props => {
  const bem = useBem({
    block: 'block',
    modifiers: [
      'someExternalModifier',
    ],
  }, props);

  return (
    <div className={bem.block({ modifier: true })}>
      <div className={bem.element('element')}>
        My component
      </div>
    </div>
  );
};

Helper

Although you are expected to use decorators, it is still possible to use helper itself.

import { BemHelper } from '@seaneiros/react-bem';

const bem = new BemHelper({
  block: 'someBlock',
  modifiers: [ ... ],
});

// use as a static field
...

Configuration

It is possible to change delimiters as you want

bem({
  block: 'someBlock',
  modifiers: [ ... ],
  config: {
    element: '~~',
    modifier: '::',
    modifierValue: '_',
  },
})(MyComponent);

License

ISC