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

@shinspiegel/use-bem-css

v0.2.3

Published

A hook to generate BEM styled string to use on classNames for react components

Downloads

312

Readme

@shinspiegel/use-bem-css

A hook to generate BEM styled string to use on classNames for react components

NPM

Install

npm install --save @shinspiegel/use-bem-css

Usage

import React from "react";
import { useBemCss } from "@shinspiegel/use-bem-css";

const App = () => {
  const { button, buttonInput, buttonLabel, buttonSpan } = useBemCss({
    className: "button",
    blocks: ["button"],
    elements: ["label", "input", "span"],
  });

  return (
    <div className={button}>
      <label className={buttonLabel}>
        <span className={buttonSpan}>Label Text</span>
        <button type="button" className={buttonInput}>
          Info
        </button>
      </label>
    </div>
  );
};

Basic Usage

It will generate an object with BEM styles strings to use on the react components as values for classNames.

const { button, buttonInput, buttonLabel, buttonSpan } = useBemCss({
  className: "button",
  blocks: ["button"],
  elements: ["label", "input"],
});

console.log(button); // "button"
console.log(buttonInput); // "button__input"
console.log(buttonLabel); // "button__label"

Usage with modifiers

You can add modifier on the classes. To be active you need to pass a property isActive, otherwise it will not allow. If a modifier does not provide a array of with the affected items it will not generate.

const { button, buttonInput, buttonLabel, buttonSpan } = useBemCss({
  className: "button",
  blocks: ["button"],
  elements: ["label", "input"],
  [{ modifier: "white", isActive: true }]
});

console.log(button); // "button button--white"
console.log(buttonInput); // "button__input button__input--white"
console.log(buttonLabel); // "button__label button__label--white"

Usage with affected modifiers

You can add modifier on the classes. To be active you need to pass a property isActive, otherwise it will not allow. If a modifier does not provide a array of with the affected items it will not generate.

const { button, buttonInput, buttonLabel, buttonSpan } = useBemCss({
  className: "button",
  blocks: ["button"],
  elements: ["label", "input"],
  [{ modifier: "white", isActive: true, affects: ["label"] }]
});

console.log(button); // "button"
console.log(buttonInput); // "button__input"
console.log(buttonLabel); // "button__label button__label--white"

Usage multiple blocks and elements

In some cases you may need to add some extra block level classes, in this cases you just need to add extra items in the block.

const { button, buttonInput, buttonLabel, buttonSpan } = useBemCss({
  className: "button",
  blocks: ["buttonBase", "alternativeButton"],
  elements: ["label", "input"],
  [{ modifier: "white", isActive: true, affects: ["label"] }]
});

console.log(button); // "base-button alternative-button"
console.log(buttonInput); // "base-button__input alternative-button__input"
console.log(buttonLabel); // "base-button__label alternative-button__label base-button__label--white alternative-button__label--white"

License

MIT © ShinSpiegel


This hook is created using create-react-hook.