About this project
Classnames is a JavaScript utility that builds a space-separated CSS class name string from conditional inputs. It can be installed from npm as `classnames` and used in Node.js, Browserify, webpack, RequireJS, or directly in a browser through a standalone script.
The main `classNames` function accepts any number of arguments. Strings are included directly, while object keys are included only when their associated value is truthy. Falsy values such as `false`, `null`, `undefined`, `0`, and empty strings are ignored. Arrays are recursively flattened using the same rules. ES2015 computed object keys can be used to generate dynamic class names.
The package is commonly used with React to simplify conditional `className` props, replacing manual string concatenation and the older React `classSet` add-on. It can combine a base class, a component-provided class name, and state-dependent classes in one call.
Two opt-in variants are provided. `classnames/dedupe` removes duplicate classes and allows a later falsy object entry to exclude an earlier class; the README notes that this variant is slower. `classnames/bind` is intended for CSS Modules or similar setups, mapping logical class names to values from an imported styles object.
For older browsers such as Internet Explorer 8 and earlier, version 2.x requires an `Array.isArray` polyfill. The project follows Semantic Versioning, includes a test suite, and is released under the MIT License.
Comments
0 Rating appears after 10 ratings
Sign in to join the discussion.