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-composer

v1.2.0

Published

BEM classnames generator for react, styled components, linaria

Downloads

7

Readme

BEM composer

Lines code style: prettier Build Status

A easy BEM classname tool for React, styled-components, linaria. Recommend using with classnames.

Install

npm

npm i bem-composer

yarn

yarn add bem-composer

API

Output

import { bem } from 'bem-composer';

// create a `button` block
const b = bem('button');

console.log(b.toString()); // 'button'
// shortcut for .toString()
console.log(b()); // 'button'
console.log(b.toSelector()); // '.button'

// create a `icon` element based on `b` block
const e = b('icon');

console.log(e.toString()); // 'button--icon'
// shortcut for .toString()
console.log(e()); // 'button--icon'
console.log(e.toSelector()); // '.button--icon'
console.log(e.toSuffix()); // '--icon'
console.log(e.toParentSelector()); // '&--icon'

// create a modifier based on `e` element
const m = e('active');

console.log(m.toString()); // 'button--icon__active'
// shortcut for .toString()
console.log(m().toString()); // 'button--icon__active'
console.log(m.toSelector().toString()); // '.button--icon__active'
console.log(m.toSuffix().toString()); // '__active'
console.log(m.toParentSelector().toString()); // '&__active'
// get suffix with element
console.log(m.toSuffix(true).toString()); // '--icon__active'
// get parent selector with element
console.log(m.toParentSelector(true).toString()); // '&--icon__active'

// multiple modifiers support
console.log(m('large')({
  name: 'rounded',
  value: true
}).toString()); // 'button--icon__active_large_rounded'

// create a modifier with value
const mv = e({
  name: 'active',
  value: 'hover'
});
console.log(mv.toString()); // 'button--icon__active-hover'

// modifier with condition
console.log(e({
  'active',
  value: false
}).toString()) // 'button--icon'
console.log(e({
  'active',
  value: true
}).toString()) // 'button--icon__active'

// block with modifier
console.log(b('', 'active').toString()); // 'button_active'



Configure delimiters

import { configure, DefaultBEMDelimiters } from 'bem-composer';

// Configure BEM constructor with custom delimiters.
const cbem = configure({
  prefix: 'bem-',
  element: '__',
  modifier: '~~',
  modifierValue: '~'
});

console.log(
  cbem('block', 'icon', {
    name: 'active',
    value: 'hover'
  }).toString()
); // bem-block__icon~~active~hover

Curried API

Provide a curried API to easily create elements and modifiers.

import { bem } from 'bem-composer';

console.log(bem('button', 'icon').toString()); // button--icon
console.log(bem('button')('icon').toString()); // button--icon
console.log(bem('button', 'icon', 'active').toString()); // 'button--icon__active'
console.log(bem('button')('icon', 'active').toString()); // 'button--icon__active'
console.log(bem('button', 'icon')('active').toString()); // 'button--icon__active'

BEM Constructor type definition

function bem(blockName: string): BEMBlock;
function bem(blockName: string, elementName: string): BEMElement;
function bem(
  blockName: string,
  elementName: string,
  modifierValue: BEMModifierValue
): BEMModifier;

export type BEMModifierValue =
  | {
      name: string;
      value?: string | boolean;
    }
  | string;

export interface BEMBlock {
  (): string; // shortcut for toString()
  (elementName: string): BEMElement;
  (
    elementName: undefined | string,
    modifierValue: BEMModifierValue
  ): BEMModifier;
  toString: () => string; // Return classname
  toSelector: () => string; // Return classname selector
}

export interface BEMElement {
  (): string; // shortcut for toString()
  (modifierValue: BEMModifierValue): BEMModifier;
  toString: () => string;
  toSelector: () => string;
  toSuffix: () => string;
  toParentSelector: () => string;
}

export interface BEMModifier {
  (): string;
  (modifierValue: BEMModifierValue): BEMModifier; // modifier chain, for multiple modifiers support
  toString: () => string;
  toSelector: () => string;
  toSuffix: (withElement?: boolean) => string;
  toParentSelector: (withElement?: boolean) => string;
}

React

import React from 'react';
import { bem } from 'bem-composer';
import classNames from 'classnames';

const b = bem('button');
const e = b('icon');
const m = e('active');

export default function Button(props) {
  const { active } = props;
  return (
    <button
      classname={classNames([
        b(), // 'button'
        b('', {
          name: 'active',
          value: active
        }) // button__active
      ])}
    >
      <img
        classname={classNames([
          e(), // 'button-icon'
          e({
            name: 'active',
            value: active
          }), // active === true ? 'button--icon__active' : 'button--icon'
          e('primary') // 'button--icon__primary'
        ])}
      />
    </button>
  );
}

Styled-components & Linaria

import styled from 'styled-components';

const b = bem('button');
const e = b('icon');
const m = e('active');

export const StyledButton = styled.button`
  /* .button */
  ${b.toSelector()} {
    border: 1px solid red;
    outline: none;
  }

  /* &--icon */
  ${e.toParentSelector()} {
    width: 20px;
    height: 20px;

    /* &__active */
    ${m.toParentSelector()} {
      opacity: 0.5;
    }
  }
`;

Trouble shooting

Multiple elements support

As we don't want BEM classname to be too complicated, multiple elements is not provided by this library, but you can do some workaround like this:

bem('block', 'e1--e2--e3').toString();