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

propmods

v0.4.1

Published

Propmods is a low-boilerplate way to BEM-annotate your React components. Propmods keeps your JSX clean by turning component props and state into BEM modifiers automatically.

Downloads

26

Readme

Propmods

Propmods is a low-boilerplate way to BEM-annotate your React components. Propmods keeps your JSX clean by turning component props and state into BEM modifiers automatically.

import React from 'react';
import block from 'propmods';

const b = block('button');

class Button extends React.Component {
    render() {
        return <button {...b(this)}>
            <i {...b('icon', ['fa', 'fa-hand-pointer-o'])} />
            {this.props.children}
        </button>;
    }
}


// This React component:
<Button size="L" theme="action" blinking>Push me!</Button>

// Turns into this DOM element:
<button class="button button_size_L button_theme_action button_blinking">
    <i class="button__icon fa fa-hand-pointer-o"></i>
    Push me!
</button>

Install

Yarn:

yarn add propmods

NPM:

npm install --save propmods

Usage

This documentation assumes that you are using a modern JavaScript bundler like Webpack or Browserify, NPM package manager, and an ES2015 compiler (e.g. Babel).

First, import Propmods and choose a block name:

import block from 'propmods';
const b = block('button');

If you use CommonJS modules, you need to require default export:

const block = require('propmods').default;
const b = block('button');

Blocks

The intended way to use Propmods is to call function b with your component as the argument and to merge results into the rendered component's props:

<button {...b(this)} />

Propmods will take your component's props and state, and will create appropriate classes. It is smart enough not to pick props which are not valid in CSS, so don't worry that a nested object or a page-long text gets into your class name.

You can also pass any other modifiers as arguments, or point directly to props, state or context if you don't wrap components into classes:

const Button = (props) => {
    return <button {...b({foo: 'bar'}, props)}>{props.children}</button>;
};

You can also mix in arbitrary classnames:

<button {...b(this, ['fancy-button', 'fancy-button-large'])} />

Elements

To create BEM elements, pass the element's name as the first argument:

<i {...b('icon', {visible: false})} />

Customization

Q: What if I don't want all props to be used as modifiers?

A: You can pick whatever props and state you like.

import _ from 'lodash';

// ...

<button {...b(_.pick(this.props, ['size', 'theme']))} />

Q: JavaScript uses camel case in prop names, and I don't want camel case in my CSS. What do I do?

A: Propmods will apply a case converting function if you supply one when creating your block.

import _ from 'lodash';
const b = block('button', {
    transformKeys: _.kebabCase
});

Q: I don't like your BEM conventions. Can I use my own BEM delimiters?

A: Yes, you can configure Propmods to use any delimiters you prefer. These are the defaults:

const b = block('button', {
    elementDelimiter: '__',
    modDelimiter: '_',
    modValueDelimiter: '_'
});

Q: I don't want to pass these options each time

A: Create a wrapped function and put it in a separate file in your project. For example:

// lib/bem.js:

import block from 'propmods';

const options = {
    // Your options
}

export default function(name) {
    return block(name, options);
}


// button.js:

import block from '../lib/bem';

const b = block('button');

Typescript

Propmods is written in Typescript and compiled down to ES5. This package already includes type declarations, so you can use it in your Typescript projects.

License

MIT.