项目作者: wavevision

项目描述 :
Create and format BEM class names for React components 🖍
高级语言: TypeScript
项目地址: git://github.com/wavevision/class-name.git
创建时间: 2020-01-10T11:51:49Z
项目社区:https://github.com/wavevision/class-name

开源协议:MIT License

下载


Wavevision s.r.o.

Class Name

CI
Coverage Status
@wavevision/class-name"">npm

Create and format BEM class names for React components. The formatter uses simplified BEM syntax.

Installation

Via Yarn

  1. yarn add @wavevision/class-name

or npm

  1. npm install --save @wavevision/class-name

Usage

Simple React component

```typescript jsx
import React, { useState, FunctionComponent } from ‘react’;
import className, { USE_VALUE } from ‘@wavevision/class-name’;

interface ComponentProps {
align: string;
booleanProp: boolean;
nullableProp: string | null;
stringProp: string;
}

interface ComponentState {
visible: boolean;
}

// Define base class name with props and state behaving as modifiers
const componentClassName = className(
‘component-class’,
() => ({
// if booleanProp value is truthy, ‘booleanProp’ will be used as modifier
booleanProp: true,
// if stringProp value is truthy then the value will be used
stringProp: USE_VALUE,
// use callback for custom modifiers, string returned will be used
customModifier: ({ props }) => (props.nullableProp ? ‘custom’ : null),
// if a non-string truthy value is returned, key will be used
anotherModifier: ({ state }) => state.visible,
}),
);

// We can also have modifiers defined only if some condition is met
const anotherClassName = className(
‘another-class’,
({ props, state }) => {
if (props.nullableProp !== null) {
// the whole set of modifiers will be created only if nullableProp is not null
return { stringProps: USE_VALUE, customModifier: () => true };
}
if (state.visible) {
// this set will be created only if state.visible is true
return { customModifier: () => true };
}
},
);

const Component: FunctionComponent = props => {
const [visible] = useState(false);
const className = componentClassName({ props, state: { visible } });
const nextClassName = anotherClassName({ props, state: { visible } });
return (



<div
className={className.compose(
className.element(‘child’),
// extra class name with optional prefix (e.g. Bootstrap text utility)
className.extra(props.align, ‘text’),
)}

  1. ></div>
  2. // modifiers can be nullable and will be used only if not null
  3. <div className={className.element('element', props.nullableProp)} ></div>
  4. <div className={className.element('another', 'element-modifier')} ></div>
  5. </div>

);
};

  1. will output following when rendered
  2. ```typescript jsx
  3. <Component
  4. align="right"
  5. booleanProp={true}
  6. nullableProp={null}
  7. stringProp="something"
  8. ></Component>
  1. <div
  2. class="component-class component-class--boolean-prop component-class--something component-class--inline-modifier"
  3. >
  4. <div class="another-class"></div>
  5. <div class="component-class__child text-right"></div>
  6. <div class="component-class__element"></div>
  7. <div
  8. class="component-class__another component-class__another--element-modifier"
  9. ></div>
  10. </div>