A tiny (239B) utility for constructing `className` strings conditionally.
Installations
npm install clsx
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Min. Node Version
>=6
Node Version
20.10.0
NPM Version
10.2.3
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (97.5%)
TypeScript (2.5%)
Developer
lukeed
Download Statistics
Total Downloads
2,223,516,583
Last Day
2,779,399
Last Week
20,834,398
Last Month
93,865,682
Last Year
986,520,298
GitHub Statistics
8,488 Stars
82 Commits
143 Forks
18 Watching
1 Branches
15 Contributors
Bundle Size
570.00 B
Minified
337.00 B
Minified + Gzipped
Package Meta Information
Latest Version
2.1.1
Package Id
clsx@2.1.1
Unpacked Size
8.35 kB
Size
3.84 kB
File Count
10
NPM Version
10.2.3
Node Version
20.10.0
Publised On
23 Apr 2024
Total Downloads
Cumulative downloads
Total Downloads
2,223,516,583
Last day
-34.1%
2,779,399
Compared to previous day
Last week
-10.4%
20,834,398
Compared to previous week
Last month
-1.9%
93,865,682
Compared to previous month
Last year
63.9%
986,520,298
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
clsx
A tiny (239B) utility for constructing
className
strings conditionally.
Also serves as a faster & smaller drop-in replacement for theclassnames
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
1import clsx from 'clsx'; 2// or 3import { clsx } from 'clsx'; 4 5// Strings (variadic) 6clsx('foo', true && 'bar', 'baz'); 7//=> 'foo bar baz' 8 9// Objects 10clsx({ foo:true, bar:false, baz:isTrue() }); 11//=> 'foo baz' 12 13// Objects (variadic) 14clsx({ foo:true }, { bar:false }, null, { '--foobar':'hello' }); 15//=> 'foo --foobar' 16 17// Arrays 18clsx(['foo', 0, false, 'bar']); 19//=> 'foo bar' 20 21// Arrays (variadic) 22clsx(['foo'], ['', 0, false, 'bar'], [['baz', [['hello'], 'there']]]); 23//=> 'foo bar baz hello there' 24 25// Kitchen sink (with nesting) 26clsx('foo', [1 && 'bar', { baz:false, bat:null }, ['hello', ['world']]], 'cya'); 27//=> '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.
1clsx(true, false, '', null, undefined, 0, NaN); 2//=> ''
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.
1import { clsx } from 'clsx'; 2// or 3import 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!
1import { clsx } from 'clsx/lite'; 2// or 3import clsx from 'clsx/lite'; 4 5// string 6clsx('hello', true && 'foo', false && 'bar'); 7// => "hello foo" 8 9// NOTE: Any non-string input(s) ignored 10clsx({ foo: true }); 11//=> ""
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
-
Install the "Tailwind CSS IntelliSense" Visual Studio Code extension
-
Add the following to your
settings.json
:
1 { 2 "tailwindCSS.experimental.classRegex": [ 3 ["clsx\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"] 4 ] 5 }
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:
1clsx('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
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
Found 7/30 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/lukeed/clsx/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/lukeed/clsx/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/lukeed/clsx/ci.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:26
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:30
- Warn: downloadThenRun not pinned by hash: .github/workflows/ci.yml:46
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 npmCommand dependencies pinned
- Info: 0 out of 1 downloadThenRun dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 7 are checked with a SAST tool
Score
3.6
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to clsx
babel-plugin-optimize-clsx
Babel plugin to optimize the use of clsx, classnames, and all libraries with a compatible API
preact-auto-clsx
主要解决再vite和webpack中className省略clsx,可以像Vue一样去编写样式
babel-plugin-clsx
Add `clsx()` automatically to `className` in `React` and support `Typescript`.
ts-clsx
Rewrite clsx in TypeScript.