Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 4 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

clsx

Build class names from conditions.

UtilitiesNPM

GitHubWebsite

1. License

“to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software”

MIT allows use in paid and closed-source products.

github.com/lukeed/clsx/blob/master/license

From the package registry and GitHub, checked daily; card checked Nov 10, 2026. Tap a verdict to see its source.How we checkedReport an error

What clsx does

clsx is a tiny free JavaScript helper that writes the class names of a button, link or card from its state. Your app says "add the active style only when this tab is open", and clsx returns the right text, such as "tab tab-active". It does not style anything itself; your CSS or Tailwind CSS does that.

What people build with it

Buttons that change with state

A Save button looks different when it is the main action, pressed or turned off; your code passes the state and gets the matching class names back.

Tailwind CSS classes by condition

In a Tailwind CSS project, a link gets a highlight color only when it is the current page, and the editor can still suggest class names inside clsx.

Swap out the classnames package

An older project that uses the classnames package can switch to clsx without rewriting the calls, and the app gets a bit smaller.

Let a parent add its own classes

A reusable card or button keeps its own look and still accepts extra class names from the screen that uses it.

Red border on a form field with an error Idea

A sign-up form marks the email field in red only after the check fails, and clears it when the person fixes the address.

clsx popularity

82 / 100
Gid Score, #62 overall
179M
downloads a week, npm
9,848
GitHub stars
1,212
agent projects use it
clsx: downloads per weeknpm, clsx 179,138,382
clsx: GitHub starslukeed/clsx 9,848

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How clsx is used

  • clsx is a dependency in 1,212 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (24.9% of the 4,867 JavaScript/TypeScript ones), #1 of 408 in utilities.
  • Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 4,538; those are left out of every share on this page.
  • The most used alternative in our scan is Lodash (111 projects).
  • Projects that use it most often also use Tailwind Merge (94.9%), Lucide React (85.1%), Class Variance Authority (82.4%).
  • It is more common in projects set up for Roo Code (29.7%) than for OpenAI Codex (15.8%).

By coding agent

625 projects, 19.1%
602 projects, 24.7%
559 projects, 18.4%
100 projects, 21.1%
74 projects, 15.8%
74 projects, 26.5%
66 projects, 19.2%
33 projects, 29.7%

Often used with

In 94.9% of its projects
In 85.1% of its projects
In 82.4% of its projects
In 97.9% of its projects
In 93.7% of its projects
In 64.0% of its projects
In 63.0% of its projects
In 46.1% of its projects

clsx in practice

Strengths

Strings, objects and nested arrays in one call

One function takes any number of mixed arguments and flattens nested arrays. The README example <code>clsx('foo', [1 &amp;amp;&amp;amp; 'bar', { baz:false, bat:null }, ['hello', ['world']]], 'cya')</code> returns <code>'foo bar hello world cya'</code>.

A smaller build when you only pass strings

The default module is 239 bytes gzipped. <code>clsx/lite</code> is 140 bytes and handles only string arguments. You import whichever build matches how you compose classes.

Pitched as a swap-in for classnames

The maintainer describes clsx as a faster and smaller drop-in replacement for <code>classnames</code>. Cross-browser benchmark snapshots are kept in the repo's <code>bench</code> directory.

Three module formats

The package ships ESM (<code>dist/clsx.mjs</code>), CommonJS (<code>dist/clsx.js</code>) and a minified UMD file. You can use either a default or a named import.

Watch out for

0, NaN and bare booleans are dropped

Every falsey value is discarded, including <code>0</code> and <code>NaN</code>. Standalone <code>true</code> is discarded too. <code>clsx(true, false, '', null, undefined, 0, NaN)</code> returns an empty string.

clsx/lite silently ignores objects

If you pass an object or array to the lite build, nothing is added and no error is raised. <code>clsx({ foo: true })</code> from <code>clsx/lite</code> returns <code>""</code>.

Tailwind autocompletion needs manual setup

Class autocompletion inside <code>clsx()</code> calls does not work by default. In VS Code, the README says to install the Tailwind CSS IntelliSense extension and add a <code>tailwindCSS.experimental.classRegex</code> entry to <code>settings.json</code>.

Every input shape clsx accepts

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'

More about clsx

Funding
  • GitHub Sponsors: https://github.com/lukeed
  • 8 GitHub sponsors

See all

Packages around it
  • tailwind-merge: Tailwind class merging
  • lucide-react: Icon components
  • class-variance-authority: Component variants
  • tailwindcss: Utility-first CSS
  • @radix-ui: UI primitives
  • react: UI library
More in Utilities

Alternatives

clsx This page

MITGid 82179M/wk

classnames

Pick classnames if your project already uses it everywhere; clsx is a drop-in replacement, so there is no need to switch in a hurry.

Tailwind Merge

Pick tailwind-merge when Tailwind CSS classes can clash (two paddings, two colors) and the later one must win.

Class Variance Authority

Pick class-variance-authority when a component has named variants such as size and intent that you want to define in one typed place.

Lodash

Modular utility library with methods for common programming tasks and data manipulation.

MITGid 84212M/wk

Compare clsx with another tool

Get started

Tell your AI assistant

“Use the npm package clsx (pin clsx@2.1.1) to build each component's className from its state, for example clsx('btn', { 'btn-primary': primary, 'btn-disabled': disabled }), instead of joining strings by hand.”

  1. Add clsx to your project.

    $ npm install --save clsx

    From github.com/lukeed/clsx/blob/master/readme.md

  2. Import it and pass strings, objects or lists; false, empty and missing values are dropped.

    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'
    

    From github.com/lukeed/clsx/blob/master/readme.md

Rules for your agent

Paste into AGENTS.md or CLAUDE.md
## clsx
- Import with either `import clsx from 'clsx'` or `import { clsx } from 'clsx'`. Both forms work.
- Never rely on `0`, `NaN` or a bare `true` to produce a class. All falsey values and standalone booleans are discarded.
- Use the object form `{ name: condition }` or the `cond &amp;&amp; 'name'` string form for conditional classes. Arrays may be nested to any depth.
- Import from `clsx/lite` only when every call passes strings. Lite silently ignores objects and arrays, so `clsx({ foo: true })` returns `""`.
- For Tailwind autocompletion in VS Code, add the README's `tailwindCSS.experimental.classRegex` entry for `clsx\(([^)]*)\)` to `settings.json`.
- If IE8 support is required, pin `clsx@1.0.x`. Current versions need `Array.isArray` (IE9+).

Mistakes to avoid

import { clsx } from 'clsx/lite' and pass objects like { 'btn-lg': isLarge }
Use the full import from 'clsx' whenever any argument is an object or a list

clsx/lite accepts only strings; in our run lite('btn', { 'btn-lg': true }) returned 'btn'.

clsx('p-2', isLarge && 'p-4') expecting p-4 to replace p-2 in Tailwind CSS
Wrap it with tailwind-merge, twMerge(clsx(...)), when classes can clash

clsx keeps both names, and which one wins then depends on the CSS order, not on the call.

Deep imports of build files from version 1, such as 'clsx/dist/clsx.m.js'
import clsx from 'clsx' (or 'clsx/lite'); version 2 only exposes these two entry points

Version 2.0.0 added an exports map; the 2.1.1 package we unpacked has dist/clsx.mjs, clsx.js, clsx.min.js and lite files, no clsx.m.js.

clsx('badge', count) to show a class only when there is a count
clsx('badge', count > 0 && 'badge-has-items')

A number is added as text (here the count itself), and 0 is dropped as falsey, so the class you meant never appears.

Questions

Can I use it in a paid, closed-source app?

Yes. It is MIT licensed; keep the copyright and license notice with the package.

Is it the same as the classnames package?

It does the same job and the README calls it a faster and smaller drop-in replacement for classnames.

Why did my class with the value 0 disappear?

clsx drops every falsey value, and 0 is one of them; pass a string or a real true/false condition instead.

Is it still maintained?

The latest release is 2.1.1, published on 23 April 2024; the package is small and stable, and the README names no successor.

Does it fix clashing Tailwind CSS classes?

No. clsx only joins names; tailwind-merge is the separate helper that removes clashing Tailwind CSS classes.