bem-react
is a module on top of React which joins awesome React with some good BEM-specific features.
Its main goals:
React.addons.classSet
which is also clumsy for BEM-like css classes)via npm: npm install bem-react
via bower: bower install bem-react
BemReact's component is the same as React's one except you should return bemjson from render
method.
Example:
var BemReact = require('bem-react');
var Button = BemReact.createClass({
getInitialState : function() {
return {
focused : this.props.focused
};
},
_onFocus : function() {
this.setState({ focused : true });
},
_onBlur : function() {
this.setState({ focused : false });
},
render : function() {
return {
block : 'button',
tag : 'button',
mods : {
size : this.props.size,
focused : this.state.focused,
disabled : this.props.disabled
},
props : {
disabled : this.props.disabled,
onFocus : this._onFocus,
onBlur : this._onBlur,
onClick : this.props.onClick
},
content : this.props.text
};
}
});
BemReact.render(
{ block : Button, props : { size : 'xl', disabled : true, text : 'click me' } },
document.body);
// inserts to body following html:
// <button class="button button_size_xl button_disabled">click me</button>
Let's imagine Dropdown
component which is the composition of Button
and Popup
components:
var Dropdown = BemReact.createClass({
getInitialState : function() {
return {
opened : this.props.opened
};
},
_onButtonClick : function() {
this.setState({ opened : !this.state.opened });
},
render : function() {
return {
block : 'dropdown',
mods : {
opened : this.state.opened,
disabled : this.props.disabled
},
content : [
{
block : Button,
props : {
key : 'b',
disabled : this.props.disabled,
text : 'click me',
onClick : this._onButtonClick
}
},
{
block : Popup,
mix : [{ block : 'dropdown', elem : 'popup' }],
props : {
key : 'p',
visible : this.state.opened && !this.props.disabled,
content : this.props.content
}
}
]
};
}
});
There're two kinds of bemjson items.
You're able to use following fields in top-level item returned from render
:
<div>
by defaultattrs
in the traditional bemjson), optionalBe careful, you aren't allowed to use mix
field there
You're able to use following fields:
API is the similar to the original React's API: