react-classset
Utility for easily manipulating the DOM class string
Last updated 10 years ago by chenglou .
Apache 2 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install react-classset 
SYNC missed versions from official npm registry.

react-classset

classSet() is a neat utility for easily manipulating the DOM class string. This previously resided inside React as a utility, but is now pulled out and library-agnostic.

Here's a common scenario

var classString = 'message';
if (isImportant) {
  classString += ' message-important';
}
if (isRead) {
  classString += ' message-read';
}

This can quickly get tedious, as assigning class name strings can be hard to read and error-prone. classSet() solves this problem:

var classString = classSet({
  'message': true,
  'message-important': isImportant,
  'message-read': isRead
});

When using classSet(), pass an object with keys of the CSS class names you might or might not need. Truthy values will result in the key being a part of the resulting string.

Alternatively, you can also use it this way:

cx('a', 0, null, undefined, 'b'); // 'a 0 b', discards null values

No more hacky string concatenations!

Current Tags

  • 0.0.2                                ...           latest (10 years ago)

2 Versions

  • 0.0.2                                ...           10 years ago
  • 0.0.1                                ...           11 years ago
Downloads
Total 2
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE