@rocksandy/classnames
v1.0.2
Published
classnames =========== fork from https://github.com/JedWatson/classnames
Downloads
4
Readme
classnames
fork from https://github.com/JedWatson/classnames
A simple JavaScript utility for conditionally joining classNames together.
Install with npm, Bower, or Yarn:
npm:
npm install @rocksandy/classnames
import classNames from '@rocksandy/classnames';
classNames('foo', 'bar'); // => 'foo bar'
Usage
The classNames
function takes any number of arguments which can be a string or object.
The argument 'foo'
is short for { foo: true }
. If the value associated with a given key is falsy, that key won't be included in the output.
classNames('foo', 'bar'); // => 'foo bar'
classNames('foo', { bar: true }); // => 'foo bar'
classNames({ 'foo-bar': true }); // => 'foo-bar'
classNames({ 'foo-bar': false }); // => ''
classNames({ foo: true }, { bar: true }); // => 'foo bar'
classNames({ foo: true, bar: true }); // => 'foo bar'
// lots of arguments of various types
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
// other falsy values are just ignored
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
Arrays will be recursively flattened as per the rules above:
var arr = ['b', { c: true, d: false }];
classNames('a', arr); // => 'a b c'
Dynamic class names with ES2015
If you're in an environment that supports computed keys (available in ES2015 and Babel) you can use dynamic class names:
let buttonType = 'primary';
classNames({ [`btn-${buttonType}`]: true });