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

v0.14.0

Published

BEM class names generator for TypeScript

Downloads

10,509

Readme

bem-ts

npm npm bundle size node

BEM (Block Element Modifier) class names generator for TypeScript.

Inspired by bem-cn.

Policy

  • No extra features.
  • No dependencies.
  • TypeScript support.

Install

npm install bem-ts

Usage

A basic usage:

import bem from "bem-ts";

const b = bem("block");

b();
//=> "block"

b({ mod1: true, mod2: false });
//=> "block block--mod1"

b({ mod1: true, mod2: false, mod3: true });
//=> "block block--mod1 block--mod3"

b(["mod1", null, "mod3"]);
//=> "block block--mod1 block--mod3"

b("element");
//=> "block__element"

b("element", { mod1: true, mod2: false });
//=> "block__element block__element--mod1"

b("element", { mod1: true, mod2: false, mod3: true });
//=> "block__element block__element--mod1 block__element--mod3"

b("element", ["mod1", null, "mod3"]);
//=> "block__element block__element--mod1 block__element--mod3"

A more complex example using React and CSS (closer to the real world):

// Button.tsx
import React from "react";
import bem from "bem-ts";
import "./Button.css";

const b = bem("Button");

type Props = {
  state: "success" | "danger";
  icon: React.ReactNode;
  children: React.ReactNode;
};

export function Button({ state, icon, children }: Props) {
  return (
    <button className={b([state])}>
      <i className={b("icon", [state])}>{icon}</i>
      {children}
    </button>
  );
}
/* Button.css */
.Button {
  /* Block */
}
.Button--success {
  /* Modifier */
}
.Button--danger {
  /* Modifier */
}
.Button__icon {
  /* Element */
}
.Button__icon--success {
  /* Element's modifier */
}
.Button__icon--danger {
  /* Element's modifier */
}

Demo

Edit bem-ts demo

API

bem()

The bem() function receives a block name and creates a new function that generates class names. The created function can receive elements or modifiers.

The function can receive the following options:

| Name | Type | Default | | ------------------------------------------- | -------------------- | ------- | | elementDelimiter | string | "__" | | modifierDelimiter | string | "--" | | namespace | string, string[] | "" | | namespaceDelimiter | string | "-" | | strict | boolean | true |

elementDelimiter

const b = bem("block", { elementDelimiter: "_" });

b("element");
//=> "block_element"

modifierDelimiter

const b = bem("block", { modifierDelimiter: "-" });

b({ mod: true });
//=> block "block-mod"

b("element", { mod: true });
//=> "block__element block__element-mod"

namespace

const b = bem("block", { namespace: "ns" });

b();
//=> "ns-block"

b("element", { mod1: true, mod2: true });
//=> "ns-block__element ns-block__element--mod1 ns-block__element--mod2"
const b = bem("block", { namespace: ["ns1", "ns2"] });

b();
//=> "ns1-ns2-block"

b("element", { mod1: true, mod2: true });
//=> "ns1-ns2-block__element ns1-ns2-block__element--mod1 ns1-ns2-block__element--mod2"

namespaceDelimiter

const b = bem("block", { namespace: "ns", namespaceDelimiter: "---" });

b();
//=> "ns---block"

b("element", { mod1: true, mod2: true });
//=> "ns---block__element ns---block__element--mod1 ns---block__element--mod2"

When namespace is not given, namespaceDelimiter will be ignored.

const b = bem("block", { namespaceDelimiter: "---" });

b();
//=> "block"

b("element", { mod1: true, mod2: true });
//=> "block__element block__element--mod1 block__element--mod2"

strict

When you set true to this option, given elements or modifiers are checked. And if the check fails, then a runtime error is thrown.

For example, when setting true, the following code throws an error.

const b = bem("foo", { strict: true });
b("element__");
b({ modifier--: true });

When setting false, the following code throws no errors.

const b = bem("foo", { strict: false });
b("element__");
//=> foo__element__
b({ modifier_: true });
//=> foo__modifier_

setup()

The setup() function can change the default options.

import bem, { setup } from "bem-ts";

setup({
  elementDelimiter: "_",
  modifierDelimiter: "-",
  namespace: "ns",
  namespaceDelimiter: "---",
  strict: false,
});

const b = bem("block");

b("element", { mod: true });
//=> "ns---block_element ns---block_element-mod"

License

MIT © Masafumi Koba