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

v1.0.0

Published

BEM class names generator with mixins support

Downloads

2

Readme

bem-clsx

Simple and fast BEM class names generator with mixin support. Inspired by clsx and bem-cn

Installation

$ npm install bem-clsx --save

or

$ yarn add bem-clsx

Usage

import block from 'bem-clsx'

const b = block('block')

Block

b();
// => 'block' 
b({ modifier: true });
// => 'block block_modifier'
b({ modifier: 'value' });
// => 'block block_modifier_value'
b(null, 'mixin');
// => 'block mixin'
b({ modifier: 'value' }, 'mixin');
// => 'block block_modifier_value mixin'
b({ modifier: 'value' }, 'mixin', null, false, undefined, 'mix');
// => 'block block_modifier_value mixin mix'

Element

b('element');
// => 'block__element' 
b('element', { modifier: true });
// => 'block__element block__element_modifier'
b('element', { modifier: 'value' });
// => 'block__element block__element_modifier_value'
b('element', 'mixin');
// 'block__element mixin'
b('element', { modifier: 'value' }, 'mixin');
//=> 'block__element block__element_modifier_value mixin'
b('element', { modifier: 'value' }, 'mixin', null, false, undefined, 'mix');
//=> 'block__element block__element_modifier_value mixin mix'

React example

import React from 'react'
import block from 'bem-clsx'

interface IProps {
  userName: string
  avatarUrl: string
  online: boolean
  className?: string
}

const b = block('user-info')

const UserInfo: React.FC<IProps> = ({
  className,
  userName,
  avatarUrl,
  online
 }) => (
   <div className={b(null, className)}>
     <img className={b('avatar')} src={avatarUrl}/>
     <span className={b('name')}>{userName}<span>
     <div className={b('status', { online })}/>
   </div>
 )

If props className is navbar__user-info and online is true, the result would be the following HTML:

<div class="user-info navbar__user-info">
  <img class="user-info__avatar" src="https://images.com/test" />
  <span class="user-info__name">Some User</span>
  <div class="user-info__status user-info__status_online" />
</div>

Custom separators

For using custom element and modifier separators, you can easily create your own module that calls the original with configuration options:

// ./src/helpers/bem.ts
import block from 'bem-clsx'

const  customConfig  = {
  namespace: 'ns-',
  elementSeparator: '~~',
  modifierSeparator: '--',
  modifierValueSeparator: '-',
}

const customBlock = (blockName: string) => (
  block(blockName, customConfig)
)

export default customBlock

Now you can use this function in your application. Example:

import block from 'helpers/bem'

const b = block('block')

b('element', { modifier: 'value' }, 'mixin', null, false, undefined, 'mix') 
// => 'ns-block~~element ns-block~~element--modifier-value mixin mix'

Default options

{
  elementSeparator: '__',
  modifierSeparator: '_',
  modifierValueSeparator: '_',
}

License

The package is available as open source under the terms of the MIT License.