npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bem-react

v0.0.10

Published

BEM-flavoured React

Downloads

131

Readme

bem-react NPM version Build Status

bem-react is a module on top of React which joins awesome React with some good BEM-specific features.

Its main goals:

  • provide ability to use some kind of bemjson in templates and during usage (instead of ugly jsx or plain js)
  • take over manipulation of css classes based on BEM (instead of annoying string concatenation or React.addons.classSet which is also clumsy for BEM-like css classes)

Getting Started

Installation

via npm: npm install bem-react

via bower: bower install bem-react

Building a component

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
        };
    }
});

Using a component

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>

Composition of components

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
                    }
                }
            ]
        };
    }
});

BEMJSON

There're two kinds of bemjson items.

1. Current rendered component

You're able to use following fields in top-level item returned from render:

  • String block block name, required
  • String tag html tag, optional, <div> by default
  • Object mods modifiers (boolean modifiers are supported as well), optional
  • Object props properties (similar to the attrs in the traditional bemjson), optional
    • content inner content, optional

Be careful, you aren't allowed to use mix field there

2. Usage of components

You're able to use following fields:

  • Function block link to another block, required
  • Object props properties, optional
  • Array mix mixed elements, optional

Top-Level API

API is the similar to the original React's API:

createClass(Object specification)

render(Object componentJson, DOMElement container, [Function callback])

renderToString(Object componentJson)

renderToStaticMarkup(Object componentJson)

unmountComponentAtNode(DOMElement container)