Socket
Socket
Sign inDemoInstall

classnames-minimal

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    classnames-minimal

Simple utility function for conditionally joining class names together


Version published
Weekly downloads
5
Maintainers
1
Install size
3.58 kB
Created
Weekly downloads
 

Readme

Source

classnames-minimal

NPM version

Simple utility function for conditionally joining class names together

This is inspired by JedWatson’s package classnames which I like but is a bit too bloated for my needs. I rarely need to support non-ES5 environments and I only need a subset of the API, so I created a more minimal version of it.

Install

$ npm install --save classnames-minimal

How to use

Pass it an object literal with class names as keys and a predicaty1 value as value. Only the keys with a truthy value will be part of the generated string.

import classNames from 'classnames-minimal';

const classes = classNames({
  'block': true,
  'block--disabled': this.props.disabled,
  'block--highlighted': this.props.highlighted
});

console.log(classes); // => contains 'block block--highlighted' if this.props.highlighted == true and this.props.disabled == false

1: I think I just made that word up. Predicaty = a value that’s either truthy or falsy.

Bonus

If you’re using CSS modules or something similar and you have your class names in an object you can use ES2015’s computed property names really nicely like this…

import classNames from 'classnames-minimal';
import styles from './style.css';

const classes = classNames({
  [styles.root]: true,
  [styles.disabled]: this.props.disabled,
  [styles.highlighted]: this.props.highlighted
});

console.log(classes); // => contains the class names generated by CSS modules and filtered by our predicates

License

MIT © Jonathan Svenheden

FAQs

Last updated on 29 Jul 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc