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

stilr-classnames

v0.2.1

Published

Define Stilr styles within component definitions.

Downloads

4

Readme

#stilr-classnames

The function returned by this module helps you define Stilr styles within a component definition.

var stilrcx = require('stilr-classnames');

<button className={stilrcx({color: 'red'})}>Hello</button>
// -> <button className="_a39va">Hello</button>

What the function does under the hood

Every argument passed into the function is processed in a certain way and gets passed into Jed Watson's classnames functions.

If an argument is not a plain object, no processing occurs.

If an argument is a plain object, each of its key/value pairs gets checked as follows. If the value is a finite number or a string or a plain object, the pair is moved out of the object and into a new one - call it objA. (The plain object case takes care of pseudoclasses and media queries.) Otherwise, the pair is moved out and into objB.

When every argument has been processed, all the objAs are merged into one big object - call it OBJ_A.

Finally, the following is returned:

require('classnames')(objB_1, objB_2, objB_3, ...,
require('stilr').create({x: OBJ_A}).x)

More examples

For starters, you should understand that the following two are equivalent.

stilrcx({float: 'left'}, 'yo1', {yo2: false})
var cx = require('classnames');

cx('_v092z', 'yo1', {yo2: false})

Second, third, and so on, parameters can define classNames that will be included in the final className.

<button className={stilrcx({color: 'red'}, 'yo1', 'yo2')}>Hello</button>
// -> <button className="_a39va yo1 yo2">Hello</button>

This is particularly helpful when you embed components within other components.

class ButtonWithRedText extends React.Component {
  render() {
    return (
      <button className={stilrcx({color: 'red'}, this.props.className)}>
        {this.props.children}
      </button>
    );
  }
}

class ButtonWithRedTextAndBlueBackground extends React.Component {
  render() {
    return (
      <Foo className={stilrcx({backgroundColor: 'blue'})} />
    );
  }
}

Thanks to classnames's handling of falsy values, you can comfortably disregard falsy values being passed into the function.

<div className={stilrcx({float: 'left'}, false, undefined)} />
// -> <div className="_xo3r3" />

Also note that you can also use stilr-classnames for extending your styles prototypally.

var commonStyle = {width: 200};

var divWithRedText = <div className={stilrcx({color: 'red'}, commonStyle})} />
// -> <div className="_2nvzs" />

var divWithBlueText = <div className={stilrcx({color: 'blue'}, commonStyle})} />
// -> <div className="_y39vz" />