Gathering detailed insights and metrics for classnames
Gathering detailed insights and metrics for classnames
Gathering detailed insights and metrics for classnames
Gathering detailed insights and metrics for classnames
@types/classnames
Stub TypeScript definitions entry for classnames, which provides its own types definitions
prefix-classnames
classnames with prefix
clsx
A tiny (239B) utility for constructing className strings conditionally.
@elliemae/ds-classnames
ICE MT - Dimsum Library - Aggregated classNames to component
A simple javascript utility for conditionally joining classNames together
npm install classnames
Typescript
Module System
Node Version
NPM Version
99.7
Supply Chain
99.5
Quality
81.2
Maintenance
100
Vulnerability
100
License
JavaScript (81.92%)
TypeScript (16.81%)
HTML (1.28%)
Total Downloads
2,757,029,154
Last Day
2,206,034
Last Week
13,851,142
Last Month
59,451,544
Last Year
683,392,485
17,637 Stars
443 Commits
564 Forks
122 Watching
5 Branches
46 Contributors
Latest Version
2.5.1
Package Id
classnames@2.5.1
Unpacked Size
23.03 kB
Size
8.46 kB
File Count
10
NPM Version
10.2.3
Node Version
20.10.0
Publised On
29 Dec 2023
Cumulative downloads
Total Downloads
Last day
-1.6%
2,206,034
Compared to previous day
Last week
0.6%
13,851,142
Compared to previous week
Last month
0.4%
59,451,544
Compared to previous month
Last year
6.7%
683,392,485
Compared to previous year
A simple JavaScript utility for conditionally joining classNames together.
Install from the npm registry with your package manager:
1npm install classnames
Use with Node.js, Browserify, or webpack:
1const classNames = require('classnames'); 2classNames('foo', 'bar'); // => 'foo bar'
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.
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.
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.
1classNames('foo', 'bar'); // => 'foo bar'
2classNames('foo', { bar: true }); // => 'foo bar'
3classNames({ 'foo-bar': true }); // => 'foo-bar'
4classNames({ 'foo-bar': false }); // => ''
5classNames({ foo: true }, { bar: true }); // => 'foo bar'
6classNames({ foo: true, bar: true }); // => 'foo bar'
7
8// lots of arguments of various types
9classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
10
11// other falsy values are just ignored
12classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
Arrays will be recursively flattened as per the rules above:
1const arr = ['b', { c: true, d: false }]; 2classNames('a', arr); // => 'a b c'
If you're in an environment that supports computed keys (available in ES2015 and Babel) you can use dynamic class names:
1let buttonType = 'primary'; 2classNames({ [`btn-${buttonType}`]: true });
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:
1import React, { useState } from 'react'; 2 3export default function Button (props) { 4 const [isPressed, setIsPressed] = useState(false); 5 const [isHovered, setIsHovered] = useState(false); 6 7 let btnClass = 'btn'; 8 if (isPressed) btnClass += ' btn-pressed'; 9 else if (isHovered) btnClass += ' btn-over'; 10 11 return ( 12 <button 13 className={btnClass} 14 onMouseDown={() => setIsPressed(true)} 15 onMouseUp={() => setIsPressed(false)} 16 onMouseEnter={() => setIsHovered(true)} 17 onMouseLeave={() => setIsHovered(false)} 18 > 19 {props.label} 20 </button> 21 ); 22}
You can express the conditional classes more simply as an object:
1import React, { useState } from 'react'; 2import classNames from 'classnames'; 3 4export default function Button (props) { 5 const [isPressed, setIsPressed] = useState(false); 6 const [isHovered, setIsHovered] = useState(false); 7 8 const btnClass = classNames({ 9 btn: true, 10 'btn-pressed': isPressed, 11 'btn-over': !isPressed && isHovered, 12 }); 13 14 return ( 15 <button 16 className={btnClass} 17 onMouseDown={() => setIsPressed(true)} 18 onMouseUp={() => setIsPressed(false)} 19 onMouseEnter={() => setIsHovered(true)} 20 onMouseLeave={() => setIsHovered(false)} 21 > 22 {props.label} 23 </button> 24 ); 25}
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:
1const btnClass = classNames('btn', this.props.className, {
2 'btn-pressed': isPressed,
3 'btn-over': !isPressed && isHovered,
4});
dedupe
versionThere 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:
1const classNames = require('classnames/dedupe');
2
3classNames('foo', 'foo', 'bar'); // => 'foo bar'
4classNames('foo', { foo: false, bar: true }); // => 'bar'
For standalone (global / AMD) use, include dedupe.js
in a <script>
tag on your page.
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.
1const classNames = require('classnames/bind'); 2 3const styles = { 4 foo: 'abc', 5 bar: 'def', 6 baz: 'xyz', 7}; 8 9const cx = classNames.bind(styles); 10 11const className = cx('foo', ['bar'], { baz: true }); // => 'abc def xyz'
Real-world example:
1/* components/submit-button.js */ 2import { useState } from 'react'; 3import classNames from 'classnames/bind'; 4import styles from './submit-button.css'; 5 6const cx = classNames.bind(styles); 7 8export default function SubmitButton ({ store, form }) { 9 const [submissionInProgress, setSubmissionInProgress] = useState(store.submissionInProgress); 10 const [errorOccurred, setErrorOccurred] = useState(store.errorOccurred); 11 const [valid, setValid] = useState(form.valid); 12 13 const text = submissionInProgress ? 'Processing...' : 'Submit'; 14 const className = cx({ 15 base: true, 16 inProgress: submissionInProgress, 17 error: errorOccurred, 18 disabled: valid, 19 }); 20 21 return <button className={className}>{text}</button>; 22}
classNames >=2.0.0
Array.isArray
: see MDN for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill.
Copyright (c) 2018 Jed Watson. Copyright of the Typescript bindings are respective of each contributor listed in the definition file.
No vulnerabilities found.
Reason
all changesets reviewed
Reason
28 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-09
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More