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

use-classy

v1.3.1

Published

A utility for generating React [className] strings, with support for CSS modules and BEM expansions.

Downloads

56

Readme

Classy

A utility for generating class name strings, with support for CSS Modules and BEM expansions.


Installation

npm i use-classy

Usage

Classy does a lot of things. At it's simplest, it’s a utility method for generating normalized class name strings for the HTML [class] attribute. It also supports CSS Modules and BEM expansions, and ships with a React-ready hook.

To get started, you can pass Classy any number of selectors (either strings or nested arrays of strings) and it will generate a normalized class string. Here's a contrived example:

import { classy } from 'use-classy';

classy('class1', [[false && 'class2'], [[['class3']]]], '.class4, class5')

Under the hood, this will flatten everything in to a single array, filter out any falsey values, and more! All of which gives you a nice, simple, space-separated class string:

'class1 class2 class3 class4 class5';

💄  Auto-Scoping

import classes from './style.module.scss';
// assuming ^this stylesheet exports something like { someClass: "r2984fh9wnc" }

classy(classes, 'someClass'); // r2984fh9wnc

If you'd like to reuse the same scope in a bunch of places, you can construct an instance of classy for reuse, like so:

import classes from './style.module.scss';

const bem = new classy({ classes });
bem('someClass'); // r2984fh9wnc

🧨  BEM Expansion

Say, for example, you had the following SCSS module…

.Block {
  &--title {
  }
  &__modifier {
  }
}

We can construct a new instance of classy, specifying a base class against which to expand partial BEM selectors. For the ultimate classy-ness, we can also pass in our module classes, since BEM expansion works with auto-scoping!

import classes from './style.module.scss';

const bem = new classy({
  bem: 'Block',
  classes,
});

Now we can reuse a single classy instance throughout our component to generate markup structures against our BEM selectors on the fly! 💥

bem();            // Block
bem('&');         // Block
bem('-element');  // Block-element
bem('_modifier'); // Block_modifier

(The above comments give the "naked" selectors for clarity; in reality this would actually output the scoped classnames.)

🪝  React Hook Usage

import { useClassy } from 'use-classy';
import classes from './style.module.scss';

const MyElement = ({ title, className }) => {
  const bem = useClassy('MyElement', classes);
  return (
    <header className={bem('&', className)}>
      <h2 className={bem('-title')}>{title}</h2>
    </header>
  );
};

Given this JSX, you'd end up with the following HTML structure: (This example shows the "naked" class names for clarity; in reality it would actually render the scoped selectors!)

<header className="MyElement additional-classes">
  <h2 className="MyElement-title">Some Title</h2>
</header>