JedWatson /classnames

A simple javascript utility for conditionally joining classNames together

★ 17,773⑂ 557👁 116⑃ 417● 176▤ 0
JavaScriptMITCreated 2014-11-05
-1
Daily
17,773
stars
557
forks
116
watchers
417
prs
176
issues
0
releases

README

Classnames

 A simple JavaScript utility for conditionally joining classNames together.


  
    
  
  
    
  
  
    
  

  

Install from the npm registry with your package manager:
bash
npm install classnames


Use with Node.js, Browserify, or webpack:

js
const classNames = require('classnames');
classNames('foo', 'bar'); // = 'foo bar'


Alternatively, you can simply include index.js on your page with a standalone  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.

js
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:

js
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:

js
const buttonType = 'primary';
classNames({ [btn-${buttonType}]: true });


 Usage with React.js

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  in React:

js
import React, { useState } from 'react';

export default function Button (props) {
  const [isPressed, setIsPressed] = useState(false);
  const [isHovered, setIsHovered] = useState(false);

  let btnClass = 'btn';
  if (isPressed) btnClass += ' btn-pressed';
  else if (isHovered) btnClass += ' btn-over';

  return (
     setIsPressed(true)}
      onMouseUp={() = setIsPressed(false)}
      onMouseEnter={() 

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON