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

qbem

v1.0.3

Published

A Quick BEM helper!

Downloads

18

Readme

issues NPM version License types minified size

qbem

A quick BEM helper! More BEM. Less Typing.

/*
 *         ._.
 *         | | +-+ QUICK-BEM +-+
 *     __._| |_.   .__.   ._. ._.
 *  . / _. | ._ \./ _  \. / _V_ \.   * Block
 *   | (_| | |_) |  __./.| ||_|| |   * Element
 *  . \__, |_.__/.\___|. |_|   |_|.  * Modifier
 * ------| |------------------------
 *      .|_|. More BEM. Less Typing.
 */

Install

Using yarn

yarn add qbem

Using npm

npm install qbem

Usage

Quick Guide

/* import or require */
import { QBem } from 'qbem';

/* or */
require { QBem } from 'qbem';

/* create an instance of QBem with your block name */
/*        doesn't matter what you name it          */
/*          I like 'qb' b/c it's short             */

const qb = new QBem('form');


/* need your block style? Do this: */
qb.block(); // returns: 'form'

/* need modifiers for your block? Do this: */
qb.block(['active']); // returns: 'form form--active'

/* want to make those modifiers conditional? Do this: */
qb.block([
  {
   ['active']: someTrueCondition,
   ['inactive']: someFalsyCondition, // modifiers with falsy conditions won't be added
  }
]); // returns: 'form form-active'

/* need an element? Do this: */
qb.element('input'); // returns: 'form__input'

/* need modifiers for your element? Do this: */
qb.element('input', ['active']); // returns: 'form__input form__input--active'

/* want to make those modifiers conditional? Do this: */
qb.element(
  'input',
  [
    {
      ['active']: someTrueCondition,
      ['inactive']: someFalsyCondition, // modifiers with falsy conditions won't be added
    }
  ]
); // returns: 'form__input form__input--active`

Use anywhere you template HTML

const qb = new QBem('form');

const template = `
<form class="${qb.block(['theme-xmas', 'simple'])}">
  <input class="${qb.element('input')}" type="text" />
  <input
  class="${qb.element('submit', ['disabled'])}"
  type="submit" />
</form>
`;

Results in:

<form class="form form--theme-xmas form--simple">
  <input class="form__input" type="text" />
  <input
  class="form__submit form__submit--disabled"
  type="submit" />
</form>

Use with React


// ToggleBtn.tsx
// ...
const qb = new QBem('toggle-btn');

const ToggleBtn: FC = (props) => {
  const { text, isActive } = props;

  return (
    <div className={qb.block()}>
      <span className={qb.element('text')}>
        {text}
      </span>
      <button
        className={qb.element(
          'button',
          [{
            enabled: isActive,
            disabled: !isActive,
          }]
        )} />
    </div>
  );
}

Creating a new instance of QBem

/* import */
import { QBem } from 'qbem';

/* create an instance per block */
const qb = new QBem('block-name');

Block

qb.block(); // block-name

Add modifiers to a block using a string array

qb.block(['active']); // block-name block-name--active
qb.block(['active', 'dark-mode']); // block-name block-name--active block-name--dark-mode

Use conditional modifiers (conditional object keys added as BEM modifier when values are true)

qb.block([
  {
    active: true,
    ['dark-mode']: true,
    disabled: false, // falsy values not added
  },
]); // block-name block-name--active block-name--dark-mode

Use mixed modifiers

qb.block([
  'active',
  'dark-mode',
  {
    disabled: false, // falsy values not added
  },
]); // block-name block-name--active block-name--dark-mode

Element

Block name is automatically added to elements

qb.element('element'); // block-name__element

Add modifiers to elements using a string array

qb.element('element', ['active']);
// block-name__element block-name__element--active

qb.element('element', ['active', 'dark-mode']);
// block-name__element block-name__element--active block-name__element--dark-mode

API

Constructor

QBem(blockName: string) -- Creates a new QBem object using the supplied blockName.

The blockName will be used as the "block" portion of BEM strings created with the instance.

Methods

blockWithModifier(modifier: string)

Manual method for creating a BEM style class string for a BEM block with a BEM modifier using the BEM block name associated with this instance of QBem.

elementWithModifier(element: string, modifider: string)

Manual method for creating a BEM style class string for a BEM element with a BEM modifier using the BEM block name associated with this instance of QBem.

block(modifiers: (string | QBemConditionalModifier)[] | undefined = [])

Method for creating a BEM style class string. Uses the BEM block name associated with this instance of QBem.

element(element: string, modifiers: (string | QBemConditionalModifier)[] | undefined = [])

Method for creating a BEM style class string. Uses the BEM block name associated with this instance of QBem and the supplied BEM element name.