Indexed by TopGit from live GitHub metadata: JedWatson/classnames has 17.8k stars, written primarily in JavaScript. A simple javascript utility for conditionally joining classNames together
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Alternatively, you can simply include index.js on your page with a standalone <script> tag and it will export a global classNames method, or define the module if you are using RequireJS.
Project philosophy
We take the stability and performance of this package seriously, because it is run millions of times a day in browsers all around the world. Updates are thoroughly reviewed for performance implications before being released, and we have a comprehensive test suite.
Classnames follows the SemVer standard for versioning.
There is also a Changelog.
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, { baz: null }, ''); // => 'bar'
// conditional strings (falsy results are ignored)
classNames('foo', true && 'bar'); // => 'foo bar'
classNames('foo', false && 'bar'); // => 'foo'
Arrays will be recursively flattened as per the rules above:
const 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:
This package is the official replacement for classSet, which was originally shipped in the React.js Addons bundle.
One of its primary use cases is to make dynamic and conditional className props simpler to work with (especially more so than conditional string manipulation). So where you may have the following code to generate a className prop for a <button> in React:
Because you can mix together object, array and string arguments, supporting optional className props is also simpler as only truthy arguments get included in the result:
There is an alternate version of classNames available which correctly dedupes classes and ensures that falsy classes specified in later arguments are excluded from the result set.
This version is slower (about 5x) so it is offered as an opt-in.
To use the dedupe version with Node.js, Browserify, or webpack:
For standalone (global / AMD) use, include dedupe.js in a <script> tag on your page.
Alternate bind version (for css-modules)
If you are using css-modules, or a similar approach to abstract class 'names' and the real className values that are actually output to the DOM, you may want to use the bind variant.
Note that in ES2015 environments, it may be better to use the "dynamic class names" approach documented above.
No homepage URL was recorded for JedWatson/classnames in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
How many stars does JedWatson/classnames have?
JedWatson/classnames has 17.8k GitHub stars — refresh the page for the live number, or check github.com/JedWatson/classnames. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is JedWatson/classnames open source?
Yes — JedWatson/classnames ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/JedWatson/classnames.
What is JedWatson/classnames?
JedWatson/classnames (JedWatson/classnames) is a JavaScript project on GitHub. From the project's own README: A simple javascript utility for conditionally joining classNames together
Where do I read more about JedWatson/classnames?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/JedWatson/classnames is the definitive source.
Read full README in the tab above.
Want a second opinion on classnames?
Ask an AI that can read this page — one click and you get its take on classnames.