An open API service indexing awesome lists of open source software.

https://github.com/agudulin/dumb-bem

Simple BEM react components generator
https://github.com/agudulin/dumb-bem

bem react react-components

Last synced: 5 months ago
JSON representation

Simple BEM react components generator

Awesome Lists containing this project

README

          

# Dumb BEM

**Dumb BEM** is a simple BEM class name [transformation](https://github.com/robinpokorny/transform-props-with) for React components.

[![npm](https://img.shields.io/npm/v/dumb-bem.svg?style=flat-square)](https://www.npmjs.com/package/dumb-bem)
[![Build Status](https://img.shields.io/travis/agudulin/dumb-bem/master.svg?style=flat-square)](https://travis-ci.org/agudulin/dumb-bem)
[![license](https://img.shields.io/npm/l/dumb-bem.svg?style=flat-square)](https://github.com/agudulin/dumb-bem/blob/master/license)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-lightgrey.svg?style=flat-square)](http://standardjs.com/)
[![Managed by Yarn](https://img.shields.io/badge/managed%20by-Yarn-2C8EBB.svg?style=flat-square)](https://yarnpkg.com/)
[![Developed at Wimdu](https://img.shields.io/badge/developed%20at-Wimdu-FCAF16.svg?style=flat-square)](http://tech.wimdu.com/)

## Install

```sh
npm install --save dumb-bem
```

## Usage

```js
import dumbBem from 'dumb-bem'
import tx from 'transform-props-with'

const dumbHeader = dumbBem('header')
const Header = tx(dumbHeader)('div')
const HeaderTitle = tx([{ element: 'title' }, dumbHeader])('h1')

ReactDOM.render(

Lorem Ipsum

, node)
// Would render:
//


//


// Lorem Ipsum
//


//

```

## API
### `dumbBem(block, options={})`

#### Arguments

- `block` (*String*): name of the base block.
- `options` (*Object*): Override default options.
- [`plugins`] \(*Array*):
array of plugins for modifying class names.

Plugin is an object with two properties:
- `maker` (*function*)
Maker function takes `block` and `props` as arguments and should return anything suitable for [classnames](https://www.npmjs.com/package/classnames) input. E.g. [it can be a string, array of string or object](https://github.com/JedWatson/classnames#usage).
- [`propsToRemove`] \(*Array*)
An array of properties which are used in the plugin but should not be injected into the corresponding DOM element in the end.

See also [built-in makers](docs/plugins.md).

#### Returns

A function which takes `props` object as a parameter, transforms `className` prop on execution and returns back changed props object.

```js
(props) => ({
...props,
className: classNameModifiedByMakers
})
```

#### Examples

**Use built-in maker function**

```js
import dumbBem from 'dumb-bem'
import { makeStates } from 'dumb-bem/lib/plugins'
import tx from 'transform-props-with'

const dumbList = dumbBem('list', { plugins: [makeStates] })
const List = tx(dumbList)('ul')
const ListItem = tx([{ element: 'item' }, dumbList])('li')

ReactDOM.render(

Item 1
Item 2
Item 3
Item 4

, node)

// Would render:
//


    //
  • Item 1

  • //
  • Item 2

  • //
    //
  • Item 4

  • //

```

**Use custom maker function**

```js
import dumbBem from 'dumb-bem'
import tx from 'transform-props-with'

const colorModifierPlugin = {
maker: (block, props) => {
if (props.color) {
return `${block}--${props.color}`
}
},
propsToRemove: ['color']
}

const dumbList = dumbBem('list', { plugins: [colorModifierPlugin] })
const List = tx(dumbList)('ul')
const ListItem = tx([{ element: 'item' }, dumbList])('li')

ReactDOM.render(

Item 1
Item 2

, node)

// Would render:
//


    //
  • Item 1

  • //
  • Item 2

  • //

```

### [Built-in Plugins](docs/plugins.md)

## License

MIT © [Alexander Gudulin](http://gudulin.com)

[travis-url]: https://travis-ci.org/agudulin/dumb-bem
[travis-image]: https://travis-ci.org/agudulin/dumb-bem.svg?branch=master