Sourcemap Explorer
Stack · npm package

clsx

A tiny (239B) utility for constructing className strings conditionally.

latest 2.1.1· MIT· 15 versions publishedView on npm

About

A tiny (239B) utility for constructing className strings conditionally.

classesclassnameclassnames

What detecting clsx tells you about a site

clsx is a tiny utility for conditionally joining class names, and its presence is a reliable marker of a utility-CSS workflow — almost always Tailwind. On its own it says little, but combined with tailwind-merge and class-variance-authority it is a near-certain fingerprint of a shadcn/ui-style component layer.

Why the exact clsx version matters

clsx is API-stable and rarely the source of issues; the version mostly matters as a freshness signal rather than a behavioural one.

clsx in a real-world stack

When you find clsx in a bundle, it rarely travels alone. tailwind-merge, class-variance-authority, tailwindcss and the @radix-ui primitives — the shadcn/ui stack.

Quick facts

Latest version2.1.1
LicenseMIT
AuthorLuke Edwards
Installnpm install clsx
Direct dependencies0

How Sourcemap Explorer detects clsx

clsx ships as v2.1.1, published 2024-04-23 and carries 0 direct dependencies, 15 versions on the registry. Those exact numbers are the footprint Sourcemap Explorer matches when clsx rides inside a deployed bundle — here is how the detection works.

We catch clsx from two complementary signals: bundled source paths and the embedded package.json. Modern bundlers (webpack, Vite, esbuild, Rollup, Turbopack) preserve the original node_modules/clsx/ paths inside the JavaScript sourcemap's sources[] array — that's the canonical signal. When the matching package.json is also captured in sourcesContent[], we read the exact version field — patch number included. No regex guessing, no version inference.

  1. 1

    Confirm the site exposes sourcemaps

    In DevTools Network, check the response headers of any application script for `SourceMap` or `X-SourceMap`. Failing that, fetch the script's last 4 KB and look for a `//# sourceMappingURL=` comment — that map is where the `clsx` paths live.

  2. 2

    Find the package in the bundle

    Open DevTools → Network → reload. Click any application script and look at its sourcemap. Inside, search `sources[]` for entries matching `node_modules/clsx/` — every match confirms the package is bundled. The matching `sourcesContent[i]` for `node_modules/clsx/package.json` gives you the exact installed version.

  3. 3

    Read the version directly from package.json

    Run `jq -r '. as $m | $m.sources | to_entries[] | select(.value | endswith("node_modules/clsx/package.json")) | $m.sourcesContent[.key] | fromjson | .version' bundle.js.map`. Sourcemap Explorer automates the same query in the popup.

Major releases of clsx

When each major version first landed. Major bumps are where breaking changes live, so this timeline is the fastest way to date the clsx version a site actually ships against the ecosystem.

Major
First release
Date
v2
2.0.0
2023-07-15
v1
1.0.0
2018-12-25
v0
0.0.1
2018-12-24

Recent versions

Version
Released
2.1.1
2024-04-23
2.1.0
2023-12-29
2.0.1
2023-12-29
2.0.0
2023-07-15
1.2.1
2022-07-06
1.2.0
2022-07-02
1.1.1
2020-05-30
1.1.0
2020-02-03

clsx README

Live mirror of the GitHub README, for reference. Updated whenever the repo's default branch changes.

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

FAQ

What is clsx used for?

A tiny (239B) utility for constructing className strings conditionally.

How can I tell if a website is using clsx?

Open the page in Chrome with the Sourcemap Explorer extension installed and read the Stack tab. We catch `clsx` from two complementary signals: `node_modules/clsx/` paths inside the JavaScript sourcemap, and the embedded `package.json` we read for exact-version detection. Without the extension you can do the same lookup manually in DevTools — the steps are listed in the "How Sourcemap Explorer detects" section above.

How do I find out which version of clsx a website is running?

Read it straight from the site's JavaScript sourcemap. When a build ships source maps, the bundled `clsx/package.json` carries the exact `version` string — Sourcemap Explorer extracts it in one click on the Stack tab, and you can do it by hand in DevTools by opening the `.map` file and searching for `node_modules/clsx/package.json`. That is far more reliable than inferring the version from an asset-hash or a `?ver=` query string, which is all surface-level detectors have to go on. The current npm release is 2.1.1, but real deployments frequently run an older pinned version — which is exactly why reading the bundled number matters.

What is the latest version of clsx?

2.1.1, as published on the npm registry. The "Recent versions" table on this page lists the most recent 8 releases with their release dates. Sourcemap Explorer reports the version actually bundled into a site, which can lag the latest release by months on real-world deployments.

Is clsx actively maintained?

Quiet at the moment — no new release in over two years. Worth checking the repository for an active fork before adopting on a new project. The last published release was 2024-04-23. Source code: https://github.com/lukeed/clsx.

Where can I read more?

Project homepage: https://github.com/lukeed/clsx#readme. Source code: https://github.com/lukeed/clsx. Published on npm: https://www.npmjs.com/package/clsx. Licensed as MIT.

Detected by Sourcemap Explorer

When a bundle ships sourcemaps, we read the embedded package.json for clsx and report the precise version (the registry's latest is v2.1.1, published 2024-04-23; the bundled copy is often older). Without sourcemaps, an import / require in the page's scripts is enough to flag it.

Install free on Chrome