项目作者: redneckz

项目描述 :
BEM library for React
高级语言: JavaScript
项目地址: git://github.com/redneckz/react-bem-helper.git
创建时间: 2017-04-23T07:08:10Z
项目社区:https://github.com/redneckz/react-bem-helper

开源协议:MIT License

下载


react-bem-helper

BEM library for React

NPM Version
Build Status
Coverage Status
@redneckz/react-bem-helper"">Bundle size

Table of Contents

Installation

  1. $ npm install --save @redneckz/react-bem-helper
  1. $ yarn add @redneckz/react-bem-helper

Motivation

This utility helps to declare BEM entities in terms of React components.
Primarily it useful for projects with CSS artifacts (Sass, Less, PostCSS, …).

Also compared to other libraries this one is aimed at simplicity and
incremental adaptation of BEM (even for proprietary projects).

Features

  1. Configurable
  2. BEM mixins support
  3. Modular CSS support
  4. Flow definitions
  5. Very small bundle ~8Kb
  6. Almost no dependencies

Prerequisites

  1. BEM Methodology
  2. Higher-Order Components
  3. Flow
  4. classnames

Usage

  1. .some-button
  2. padding: 8px
  3. &--major
  4. background: blue
  5. &--size
  6. &--small
  7. padding: 4px
  8. &--large
  9. padding: 16px
  1. import React from 'react';
  2. import { BEM } from '@redneckz/react-bem-helper';
  3. import styles from './some-button.sass';
  4. const someButton = BEM(styles);
  5. export const SomeButton = someButton(({ className, disabled, children }) => (
  6. <button className={className} disabled={disabled}>
  7. {children}
  8. </button>
  9. ));
  1. <React.Fragment>
  2. <SomeButton major>Major</SomeButton>
  3. <SomeButton size="small">Small</SomeButton>
  4. <SomeButton major size="large">
  5. Large
  6. </SomeButton>
  7. <SomeButton disabled>Disabled</SomeButton>
  8. </React.Fragment>

will produce

  1. <button class="some-button some-button--major">Major</button>
  2. <button class="some-button some-button--size--small">Small</button>
  3. <button class="some-button some-button--major some-button--size--large">Large</button>
  4. <button class="some-button" disabled>Disabled</button>

Any valid component can be used to declare block or its elements

  1. export const SomeButton = someButton(
  2. class extends React.PureComponent {
  3. render() {
  4. const { className, disabled, children } = this.props;
  5. return (
  6. <button className={className} disabled={disabled}>
  7. {children}
  8. </button>
  9. );
  10. }
  11. },
  12. );
  1. export const SomeButton = someButton('button'); // DOM component

Also library provides several factory functions to declare DOM components
with restricted attributes list (div, span, form, button, input, label, textarea)

  1. import React from 'react';
  2. import { BEM, div } from '@redneckz/react-bem-helper';
  3. import styles from './some-button.sass';
  4. const someButton = BEM(styles);
  5. export const SomeButton = someButton(div({ role: 'button' }));

Block with elements

  1. .panel
  2. display: flex
  3. &__item
  4. flex-grow: 0
  5. &--align
  6. &--start
  7. align-self: flex-start
  8. &--center
  9. align-self: center
  10. &--end
  11. align-self: flex-end
  12. &__spread
  13. flex-grow: 1
  1. import React from 'react';
  2. import { BEM, div } from '@redneckz/react-bem-helper';
  3. import styles from './panel.sass';
  4. const panel = BEM(styles);
  5. export const Panel = panel('div');
  6. export const PanelItem = panel.item(div());
  7. export const PanelSpread = panel.spread('div');

Configuration

BEM naming convention

  1. import React from 'react';
  2. import { Config } from '@redneckz/react-bem-helper';
  3. Config.ELEMENT_SEPARATOR = '__';
  4. Config.MODIFIER_SEPARATOR = '--';

Flow

Generate stub for classnames

  1. $ flow-typed create-stub classnames@x.x.x

Do not forget to configure includes and ignores to put library into scope.

License

MIT