Home
Docs
GitHub
Pricing
Blog
Log In

Run Sandworm Audit for your App

Get started
Hold on, we're currently generating a fresh version of this report

clsx 2.1.0

A tiny (239B) utility for constructing className strings conditionally.
Package summary
Share
0
issues
0
licenses
Package created
24 Dec 2018
Version published
29 Dec 2023
Maintainers
1
Total deps
0
Direct deps
0
License
MIT
Generating a report...
Hold on while we generate a fresh audit report for this package.

Frequently Asked Questions

What does clsx do?

clsx is a lightweight JavaScript utility that generates className strings conditionally. At a minute 234B in size, it serves as a faster and tinier drop-in alternative for the commonly used classnames module. clsx can accept any mix of Objects, Arrays, Booleans, or Strings as its input, and discards any falsy values. This makes it an excellent tool for assigning class names to HTML elements conditionally in your web projects.

How do you use clsx?

To add clsx to your project, first install it through npm with the command npm install --save clsx. You can then import the clsx function into your JavaScript file with the line import clsx from 'clsx';.

Here is an example of how to use clsx:

import clsx from 'clsx';

// To generate class strings...
let className = clsx('foo', true && 'bar', 'baz');

// To conditionally include classes...
let isActive = true;
let className = clsx('button', isActive && 'button-active');

// To use with Objects, Arrays, mixed types....
let className = clsx('foo', [1 && 'bar', { baz: false, bat: null }, ['hello', ['world']]], 'cya');

In these examples, clsx generates a class string based on the conditions given in its arguments. If a condition is true, clsx includes the related string in the class name.

Where are the clsx docs?

Comprehensive documentation for the clsx package can be found on its GitHub page. This includes detailed usage examples, an API reference, and benchmarks comparing clsx's performance to other className utilities. Furthermore, the page details support for various Node.js versions and browsers, and provides steps to enable class autocompletion using clsx with the popular Tailwind CSS framework. For an even deeper dive, the source code can be examined by following the provided git link: git+https://github.com/lukeed/clsx.git.