1.2.0-next.e5fc24b0 • Published 7 years ago

@fesk/react-bem v1.2.0-next.e5fc24b0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

React BEM

A small library for creating React components using the http://getbem.com/ methodology.

The React components created using this library are stateless and pure, they should be used in other stateful components. This allows the BEM components to be reused and also built in isolation.

Creating BEM Blocks

There are 2 ways to create a block. The first is to define the individual elements, and use them individually in other components.

Simple blocks

const Header = bem('header', header => ({
  Header: header,
  Menu: header.element('menu'),
  MenuItem: header.element('menu-item').modifier('isSelected'),
  Logo: header.element('logo'),
}));

This definition will create 4 React components that can be used, and will have the appropriate BEM classes added.

ReactDOM.render(
  <Header>
    <Header.Logo />
    <Header.Menu>
      <Header.MenuItem>Item 1</Header.MenuItem>
      <Header.MenuItem>Item 2</Header.MenuItem>
      <Header.MenuItem isSelected={true}>Item 3</Header.MenuItem>
    </Header.Menu>
  </Header>
);

Will render:

<div class="header">
  <div class="header__logo"></div>
  <div class="header__menu">
    <div class="header__menu-item">Item 1</div>
    <div class="header__menu-item">Item 2</div>
    <div class="header__menu-item header__menu-item--selected">Item 3</div>
  </div>
</div>

Specifying a structure

You can also specify a structure for a block, where a new component will be created that renders this structure. You have to manage the props yourself, just like any other component. Starting from the component we had above:

const Header = bem(
  'header',
  header => ({
    Header: header,
    Menu: header.element('menu'),
    MenuItem: header.element('menu-item').modifier('isSelected'),
    Logo: header.element('logo'),
  }),
  ({ Header, Menu, MenuItem, Logo }, props) => (
    <Header>
      <Logo />
      <Menu>
        {props.menuItems.map((menuItem, key) => (
          <MenuItem
            key={key}
            isSelected={menuItem === props.selectedItem}
          >
            {menuItem}
          </MenuItem>
        ))}
      </Menu>
    </Header>
  )
);

With this structure you can map simple props to values inside of the components. To replciate the HTML from the first example you can simply:

ReactDOM.render(
  <Header menuItems={['Item 1', 'Item 2', 'Item 3']} selectedItem="Item 3" />
);

Customising blocks and elements

There are a few methods avaiable on the block that you can use to customise the underlying HTML element.

const Header = bem('header', header => ({
  Header: header,
  Menu: header.element('menu').asTag('ul'),
  MenuItem: header
    .element('menu-item')
    .modifier('isSelected')
    .asTag('li'),
  Logo: header
    .element('logo')
    .asTag('img')
    .withProp('alt', 'Site logo'),
}));

Available methods:

// Returns BEM element
element((elementName: string));

// Sets modifier, will add the BEM modifier when a matching prop is passed to the component.
modifier((bemModifier: string));

// Adds static prop/html attribute
withProp((propName: string), (propValue: any));

// Set multiple props e.g. withProps({ alt: 'some alt tag' })
withProps(props);

// Changes HTML tag (default: div)
asTag((htmlTag: string));

Themes

Using React 16.3s new context API, there is a theme API for any components that use BEM classes. Taking our first simple example, we can customise the block names, class names or simply add a prefix for all components, not matter how nested by wrapping in a Theme component.

import bem, { Theme } from 'react-bem';

const Header = bem('header', header => ({
  Header: header,
  Menu: header.element('menu'),
  MenuItem: header.element('menu-item').modifier('isSelected'),
  Logo: header.element('logo'),
}));

ReactDOM.render(
  <div>
    <Header.Logo />
    <Theme prefix="custom-">
      <Header.Logo />
    </Theme>
    <Theme blockMap={{ header: 'header-alternate' }}>
      <Header.Logo />
    </Theme>
    <Theme classMap={{ header__logo: 'not-even-bem' }}>
      <Header.Logo />
    </Theme>
    <Theme prefix="custom-" classMap={{ header__logo: 'not-even-bem' }}>
      <Header.Logo />
    </Theme>
  </div>
);

This will render:

<div>
  <div class="header__logo"></div>
  <div class="custom-header__logo"></div>
  <div class="header-alternate__logo"></div>
  <div class="not-even-bem"></div>
  <div class="custom-not-even-bem"></div>
</div>

You can mix and match as you need. This can be put at the root of your project and all components in your projects will be configured from that Theme. Themes can also be nested, so you can separate individual pieces of your application with its own Themes. This makes BEM a lot more flexible, especially when you want to share components across various projects.

1.1.4

7 years ago

1.1.2

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.2

7 years ago

0.0.3

7 years ago

0.0.1

8 years ago