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/license3. Used by agents' projects
Dependency in 1,212 public GitHub projects set up for AI coding agents that we scanned (19.4%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
code.visualstudio.com/llms.txtFrom 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
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
Often used with
clsx in practice
Strengths
One function takes any number of mixed arguments and flattens nested arrays. The README example <code>clsx('foo', [1 &amp;&amp; 'bar', { baz:false, bat:null }, ['hello', ['world']]], 'cya')</code> returns <code>'foo bar hello world cya'</code>.
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.
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.
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
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.
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>.
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
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
Alternatives
clsx This page
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.
Pick tailwind-merge when Tailwind CSS classes can clash (two paddings, two colors) and the later one must win.
Pick class-variance-authority when a component has named variants such as size and intent that you want to define in one typed place.
Modular utility library with methods for common programming tasks and data manipulation.
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.”
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'
Rules for your agent
## 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 && '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
clsx/lite accepts only strings; in our run lite('btn', { 'btn-lg': true }) returned 'btn'.
clsx keeps both names, and which one wins then depends on the CSS order, not on the call.
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.
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.