clsx vs classnames
CSS Class Management Comparison
1 Year
clsxclassnames
What's CSS Class Management?

CSS Class Management libraries in JavaScript help developers dynamically manage and conditionally apply CSS classes to HTML elements. These libraries simplify the process of combining multiple class names based on certain conditions, making it easier to create responsive and interactive user interfaces without manually handling class logic. They are particularly useful in frameworks like React, Vue, and Angular, where class names often need to change based on component state, props, or other dynamic factors. classnames is a widely-used utility that allows for conditional class name generation, while clsx is a smaller, faster alternative that offers similar functionality with a more modern API.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
clsx20,999,4668,7068.55 kB1410 months agoMIT
classnames12,112,14317,67523.6 kB5a year agoMIT
Feature Comparison: clsx vs classnames

Size and Performance

  • clsx:

    clsx is extremely lightweight (around 1KB minified) and optimized for performance. Its smaller size and faster execution make it an excellent choice for performance-sensitive applications, especially those built with modern frameworks.

  • classnames:

    classnames is relatively small (about 1.5KB minified), but it is larger than clsx. The performance is good for most use cases, but it may introduce slight overhead in highly dynamic applications where class names change frequently.

API Design

  • clsx:

    clsx features a minimalist API that simplifies class name merging while maintaining flexibility. It supports strings, arrays, and objects, but with a more concise syntax that reduces boilerplate code.

  • classnames:

    classnames has a flexible API that allows for complex class name merging, including support for arrays, objects, and nested conditions. However, its API can be a bit verbose for simple use cases.

Community and Ecosystem

  • clsx:

    clsx is gaining popularity, especially among developers looking for lightweight alternatives. While it has a smaller community compared to classnames, it is well-documented and actively maintained.

  • classnames:

    classnames has a large and active community, with extensive documentation and numerous examples. It is widely used in the industry, making it a reliable choice for both small and large projects.

TypeScript Support

  • clsx:

    clsx also offers excellent TypeScript support, with a focus on type safety and minimalistic design. Its type definitions are clear and concise, making it a great choice for TypeScript developers.

  • classnames:

    classnames provides good TypeScript support, with type definitions included in the package. This makes it easy to use in TypeScript projects without additional configuration.

Ease of Use: Code Examples

  • clsx:

    clsx Example

    import clsx from 'clsx';
    
    const buttonClass = clsx('btn', {
      'btn-primary': isPrimary,
      'btn-large': size === 'large',
    });
    
  • classnames:

    classnames Example

    import classNames from 'classnames';
    
    const buttonClass = classNames('btn', {
      'btn-primary': isPrimary,
      'btn-large': size === 'large',
    });
    
How to Choose: clsx vs classnames
  • clsx:

    Choose clsx if you prefer a lightweight, modern alternative with a simpler API. It is perfect for projects where performance and bundle size are concerns, and you want a no-frills solution for conditional class name management.

  • classnames:

    Choose classnames if you need a well-established, feature-rich library with extensive documentation and community support. It is ideal for projects that require a robust solution with support for various class name merging scenarios.

README for clsx

clsx CI codecov licenses

A tiny (239B) utility for constructing className strings conditionally.
Also serves as a faster & smaller drop-in replacement for the classnames module.

This module is available in three formats:

  • ES Module: dist/clsx.mjs
  • CommonJS: dist/clsx.js
  • UMD: dist/clsx.min.js

Install

$ npm install --save clsx

Usage

import clsx from 'clsx';
// or
import { clsx } from 'clsx';

// Strings (variadic)
clsx('foo', true && 'bar', 'baz');
//=> 'foo bar baz'

// Objects
clsx({ foo:true, bar:false, baz:isTrue() });
//=> 'foo baz'

// Objects (variadic)
clsx({ foo:true }, { bar:false }, null, { '--foobar':'hello' });
//=> 'foo --foobar'

// Arrays
clsx(['foo', 0, false, 'bar']);
//=> 'foo bar'

// Arrays (variadic)
clsx(['foo'], ['', 0, false, 'bar'], [['baz', [['hello'], 'there']]]);
//=> 'foo bar baz hello there'

// Kitchen sink (with nesting)
clsx('foo', [1 && 'bar', { baz:false, bat:null }, ['hello', ['world']]], 'cya');
//=> 'foo bar hello world cya'

API

clsx(...input)

Returns: String

input

Type: Mixed

The clsx function can take any number of arguments, each of which can be an Object, Array, Boolean, or String.

Important: Any falsey values are discarded!
Standalone Boolean values are discarded as well.

clsx(true, false, '', null, undefined, 0, NaN);
//=> ''

Modes

There are multiple "versions" of clsx available, which allows you to bring only the functionality you need!

clsx

Size (gzip): 239 bytes
Availability: CommonJS, ES Module, UMD

The default clsx module; see API for info.

import { clsx } from 'clsx';
// or
import clsx from 'clsx';

clsx/lite

Size (gzip): 140 bytes
Availability: CommonJS, ES Module
CAUTION: Accepts ONLY string arguments!

Ideal for applications that only use the string-builder pattern.

Any non-string arguments are ignored!

import { clsx } from 'clsx/lite';
// or
import clsx from 'clsx/lite';

// string
clsx('hello', true && 'foo', false && 'bar');
// => "hello foo"

// NOTE: Any non-string input(s) ignored
clsx({ foo: true });
//=> ""

Benchmarks

For snapshots of cross-browser results, check out the bench directory~!

Support

All versions of Node.js are supported.

All browsers that support Array.isArray are supported (IE9+).

Note: For IE8 support and older, please install clsx@1.0.x and beware of #17.

Tailwind Support

Here some additional (optional) steps to enable classes autocompletion using clsx with Tailwind CSS.

Visual Studio Code
  1. Install the "Tailwind CSS IntelliSense" Visual Studio Code extension

  2. Add the following to your settings.json:

 {
  "tailwindCSS.experimental.classRegex": [
    ["clsx\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"]
  ]
 }

You may find the clsx/lite module useful within Tailwind contexts. This is especially true if/when your application only composes classes in this pattern:

clsx('text-base', props.active && 'text-primary', props.className);

Related

  • obj-str - A smaller (96B) and similiar utility that only works with Objects.

License

MIT © Luke Edwards