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

@puck/react-bem

v1.0.2

Published

Automate creating BEM classes in React components

Downloads

79

Readme

React BEM template functions

Installation

npm install @puck/react-bem

Usage

[!NOTE] Learn how to inject the bem prop in the next chapter

Simplest way to create a block with some elements

function Acme({ bem: { className, element } }) {
  return <div className={className}>
    <h1 class={element`heading`}>Hello</h1>
  </div>
}
<div class="acme">
  <h1 class="acme__heading">Hello</h1>
</div>

BEM helper as a shorthand if there are no elements

function Acme({ bem }) {
  return <div className={bem}>Hello</div>
}
<div class="acme">Hello</div>

Adding block modifiers

function Acme({ bem: { block } }) {
  const [toggle, setToggle] = useState(true);
  const onClick = useCallback(
      () => setToggle(current => !current),
      [setToggle],
  );

  return <div className={block`${{ toggle }} always-enabled`}>
    <button onClick={onClick}>Toggle</button>
  </div>
}
<div class="acme acme--toggle acme--always-enabled"/>

Mixing the block with other classes

function Acme({ bem: { block } }) {
  return <div className={mix`me-2 d-flex`}>
  </div>
}
<div class="acme me-2 d-flex">...</div>

Mixing with parent block

To mix a block with a parent element just pass the element name as className and it will be appended automatically

function Child({ bem: { block } }) {
  return <div className={block`${{ active: true }}`.mix`me-2`}/> 
}

function Parent({ bem: { className, element } }) {
  return <div className={className}>
    <Child className={element`element`}/>
  </div>
}
<div class="parent">
  <div class="child parent__element child--active me2"/>
</div>

Using elements with modifiers

function Acme({ bem: { block, element } }) {
  return <div className={block}>
    <div class={element`item ${{ selected: true }} me-2`} />
    <div class={element`item ${{ variant: 'primary' }}`} />
    <div class={element`item ${['theme-dark']}`} />
    <div class={element`item`.mix`d-flex`} />
  </div>
}
<div class="acme">
  <div class="acme__item acme__item--selected me-2" />
  <div class="acme__item acme__item--variant-primary" />
  <div class="acme__item acme__item--theme-dark" />
  <div class="acme__item d-flex" />
</div>

Adding the BEM helper to your components

Let’s assume you have a component you’d like to use BEM with:

type Props = {
    title: string;
}

export default function AcmeBanner({ title }: Props) {
    return <div>
        Hello {title}
    </div>;
}

BEM is a naming strategy, so let’s reuse the same name between your React component display name and your CSS block name. The CSS name will be converted to snake-case.

  1. Import the withBem Higher Order Component
  2. Add a displayName to your component
  3. (Typescript): Wrap your prop types with withBem.props<> type
  4. Wrap your export in a withBem() HOC
import { withBem } from '@puck/react-bem'

type Props = {
    title: string;
}

function AcmeBanner({ bem: { className }, title }: withBem.props<Props>) {
    return <div className={className}>
        Hello {title}
    </div>;
}
AcmeBanner.displayName = 'AcmeBanner';
export default withBem(AcmeBanner);

Optionally, you might pass the component name explicitly using withBem.named():

export default withBem.named('AcmeComponent', ({ bem, title }) => {
    return <div className={bem}>
        Hello {title}
    </div>;
});