Install clsx via npm
masterInstall the clsx utility to construct className strings conditionally. It is a tiny, fast, and small drop-in replacement for the classnames module.
$ npm install --save clsxrepository·master·Indexed 27 days ago
https://github.com/lukeed/clsxA tiny (239B) utility for conditionally constructing className strings. It serves as a fast, small drop-in replacement for the classnames module, allowing the composition of class names using strings, objects, and arrays while discarding falsey values. Includes a lightweight clsx/lite version (140 bytes gzip) for string-only composition.
Install the clsx utility to construct className strings conditionally. It is a tiny, fast, and small drop-in replacement for the classnames module.
$ npm install --save clsxTo enable class autocompletion when using clsx with Tailwind CSS in Visual Studio Code:
settings.json:{
"tailwindCSS.experimental.classRegex": [
["clsx\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"]
]
}The clsx function allows you to conditionally compose class names using strings, objects, and arrays. Any falsey values (including standalone Booleans) are discarded from the resulting string.
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'The clsx/lite module is a smaller version (140 bytes gzip) of the utility. It is ideal for applications that only use the string-builder pattern.
CAUTION: It accepts ONLY string arguments. 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 });
//=> ""The clsx function accepts any number of arguments of type Mixed (Object, Array, Boolean, or String). It returns a String containing the concatenated class names.
Important: Any falsey values are discarded, including standalone Boolean values.
clsx(true, false, '', null, undefined, 0, NaN);
//=> ''All versions of Node.js are supported.
Array.isArray are supported (IE9+).clsx@1.0.x.The clsx function is used to conditionally join class names together. It accepts any number of arguments and processes them to return a single space-separated string.
Supported input types:
Falsy values (like null, undefined, false, 0, or '') are automatically ignored.
clsx function provides a minimal implementation for conditionally joining strings. It iterates through arguments and appends non-empty strings to a single result string, separated by spaces. Note that this lightweight version only processes string arguments and ignores other types (like objects or arrays) that the full clsx package supports.clsx function accepts a variable number of arguments of type ClassValue and returns a single string of space-separated class names. It automatically handles falsy values (like null, undefined, or false) by ignoring them, making it ideal for conditional styling.The following types are exported within the clsx namespace:
type ClassValue = ClassArray | ClassDictionary | string | number | bigint | null | boolean | undefined;
type ClassDictionary = Record<string, any>;
type ClassArray = ClassValue[];A ClassValue is any of the following types that can be passed to clsx:
string or number or bigintClassArray: An array of ClassValuesClassDictionary: An object where keys are class names and values are truthy/falsy triggers (e.g., { 'is-active': true })null, boolean, or undefined (these are ignored in the output)clsx function is also available as a named export.