# clsx — card for AI agents

Tiny utility for conditionally constructing className strings.

Category: Utilities · npm package `clsx` · latest version 2.1.1 (checked 2026-11-01)
Install: `npm install clsx`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (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 && '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+).

## Docs written for agents
- [llms.txt (index of the docs)](https://code.visualstudio.com/llms.txt)

## What it is
- clsx builds `className` strings conditionally. The README calls it "tiny (239B)" and says it "serves as a faster & smaller drop-in replacement for the `classnames` module." The repository dates from December 2018 and is MIT licensed.
- You call `clsx(...input)` with any number of arguments. Each one can be a string, an object whose truthy keys are kept, an array, or a nested array. The function returns one space-separated string. All falsey values are dropped, and so are standalone booleans.
- There are two builds. The default `clsx` comes as ES Module, CommonJS and UMD. `clsx/lite` is smaller, and the README says it suits apps that only use the string-builder pattern. The README pitches lite for Tailwind code like `clsx('text-base', props.active &amp;&amp; 'text-primary', props.className)`.

## Caveats
- 0, NaN and bare booleans are dropped: Every falsey value is discarded, including `0` and `NaN`. Standalone `true` is discarded too. `clsx(true, false, '', null, undefined, 0, NaN)` 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. `clsx({ foo: true })` from `clsx/lite` returns `""`.
- Tailwind autocompletion needs manual setup: Class autocompletion inside `clsx()` calls does not work by default. In VS Code, the README says to install the Tailwind CSS IntelliSense extension and add a `tailwindCSS.experimental.classRegex` entry to `settings.json`.
- IE8 means pinning an old version: Current versions support browsers with `Array.isArray` (IE9+). For IE8 and older, the README points to `clsx@1.0.x` and warns about a known issue.

## Numbers (AgentGid data)
- Gid Score: 82.5 (#62 of all tools, 4 of 408 in its category)
- Used in 1,212 public projects set up for AI coding agents (19.4%)
- Downloads last week: 179,138,382 (as of 2026-11-24), +35.0% over 4 weeks
- GitHub stars: 9,848 (as of 2026-11-24)
- Last push: 2024-06-10

## Often used with
Tailwind Merge 94.9%, Lucide React 85.1%, Class Variance Authority 82.4%, Tailwind CSS 97.9%, React 93.7%, Radix UI 64.0%

## Checked alternatives (same job)
- Lodash: https://agentgid.com/tools/lodash/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/clsx/
- Homepage: https://github.com/lukeed/clsx#readme
- Repository: https://github.com/lukeed/clsx
- Source: https://github.com/lukeed/clsx#readme
- Source: https://github.com/lukeed/clsx
- Source: https://github.com/lukeed/clsx/releases


## clsx: build class names from conditions

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.

### Where to use 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: A sign-up form marks the email field in red only after the check fails, and clears it when the person fixes the address. (idea, not from the official pages)

### How to start
- Add clsx to your project.

```
$ npm install --save clsx
```

- 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'
```


### License
Free to use, change and sell as part of your product under the MIT license. Keep its copyright and license notice in copies of the package you ship.

### Good to know
- Any value that counts as empty or false, including the number 0, is dropped from the result.
- The smaller clsx/lite version accepts only plain text pieces; conditions written as objects are silently ignored.
- It works in every Node.js version and in browsers back to Internet Explorer 9.
- It only joins names. When two Tailwind CSS classes clash, such as two paddings, both stay; a separate helper, tailwind-merge, sorts that out.

Checked 2026-11-10 against: https://github.com/lukeed/clsx/blob/master/readme.md, https://github.com/lukeed/clsx/blob/master/license, https://github.com/lukeed/clsx/releases/tag/v2.0.0, https://registry.npmjs.org/clsx, https://registry.npmjs.org/clsx/-/clsx-2.1.1.tgz, https://github.com/dcastil/tailwind-merge/blob/main/README.md

### Mistakes to avoid
- Don't: import { clsx } from 'clsx/lite' and pass objects like { 'btn-lg': isLarge } → Do: 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'.)
- Don't: clsx('p-2', isLarge && 'p-4') expecting p-4 to replace p-2 in Tailwind CSS → Do: 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.)
- Don't: Deep imports of build files from version 1, such as 'clsx/dist/clsx.m.js' → Do: 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.)
- Don't: clsx('badge', count) to show a class only when there is a count → Do: 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.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
