Apr 23, 2024
almost 2 years ago
Apr 23, 2024
almost 2 years ago
Feb 28, 2025
12 months ago
Apr 23, 2024
almost 2 years ago
Currently open
Total resolved
Open vs total issues
A tiny (239B) utility for constructing
classNamestrings conditionally.
Also serves as a faster [blocked] & smaller drop-in replacement for theclassnamesmodule.
This module is available in three formats:
dist/clsx.mjsdist/clsx.jsdist/clsx.min.js$ npm install --save clsximport clsx from 'clsx';// orimport { clsx } from 'clsx';// Strings (variadic)clsx('foo', true && 'bar', 'baz');//=> 'foo bar baz'// Objectsclsx({ foo:true, bar:false, baz:isTrue() });//=> 'foo baz'// Objects (variadic)clsx({ foo:true }, { bar:false }, null, { '--foobar':'hello' });//=> 'foo --foobar'// Arraysclsx(['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'Returns: String
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);//=> ''There are multiple "versions" of clsx available, which allows you to bring only the functionality you need!
clsxSize (gzip): 239 bytes
Availability: CommonJS, ES Module, UMD
The default clsx module; see API for info.
import { clsx } from 'clsx';// orimport clsx from 'clsx';clsx/liteSize (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';// orimport clsx from 'clsx/lite';// stringclsx('hello', true && 'foo', false && 'bar');// => "hello foo"// NOTE: Any non-string input(s) ignoredclsx({ foo: true });//=> ""For snapshots of cross-browser results, check out the bench [blocked] directory~!
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.xand beware of #17.
Here some additional (optional) steps to enable classes autocompletion using clsx with Tailwind CSS.
Install the "Tailwind CSS IntelliSense" Visual Studio Code extension
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);MIT © Luke Edwards
Median days to close
Pending contributions