readme.md
This file
Path
readme.md
154 lines · 3.9 KiB · Markdown
Last changed by
chore: add licenses badgeLuke Edwardsbefore adoption684509c
History of this fileTake it
chore: add licenses badgeLuke Edwardsbefore adoption684509c
line 1why, one step back1# clsx [](https://github.com/lukeed/clsx/actions?query=workflow%3ACI) [](https://codecov.io/gh/lukeed/clsx) [](https://licenses.dev/npm/clsx)chore: add readme docs & pkg keywordsLuke Edwardsbefore adoptionad0c4c5
line 2why, one step back2chore: update module sizeLuke Edwardsbefore adoptionbf64e71
line 3why, one step back3> A tiny (239B) utility for constructing `className` strings conditionally.<Br>Also serves as a [faster](bench) & smaller drop-in replacement for the `classnames` module.chore: add readme docs & pkg keywordsLuke Edwardsbefore adoptionad0c4c5
lines 4–6why, one step back45This module is available in three formats:6chore: readme typo (#76)Andi Pätzoldbefore adoption4c9a55d
line 7why, one step back7* **ES Module**: `dist/clsx.mjs`chore: add readme docs & pkg keywordsLuke Edwardsbefore adoptionad0c4c5
lines 8–22why, one step back8* **CommonJS**: `dist/clsx.js`9* **UMD**: `dist/clsx.min.js`101112## Install1314```15$ npm install --save clsx16```171819## Usage2021```js22import clsx from 'clsx';feat: add named `clsx` export alias (#44)danikazebefore adoption56ab81f
lines 23–24why, one step back23// or24import { clsx } from 'clsx';chore: add readme docs & pkg keywordsLuke Edwardsbefore adoptionad0c4c5
lines 25–64why, one step back2526// Strings (variadic)27clsx('foo', true && 'bar', 'baz');28//=> 'foo bar baz'2930// Objects31clsx({ foo:true, bar:false, baz:isTrue() });32//=> 'foo baz'3334// Objects (variadic)35clsx({ foo:true }, { bar:false }, null, { '--foobar':'hello' });36//=> 'foo --foobar'3738// Arrays39clsx(['foo', 0, false, 'bar']);40//=> 'foo bar'4142// Arrays (variadic)43clsx(['foo'], ['', 0, false, 'bar'], [['baz', [['hello'], 'there']]]);44//=> 'foo bar baz hello there'4546// Kitchen sink (with nesting)47clsx('foo', [1 && 'bar', { baz:false, bat:null }, ['hello', ['world']]], 'cya');48//=> 'foo bar hello world cya'49```505152## API5354### clsx(...input)55Returns: `String`5657#### input58Type: `Mixed`5960The `clsx` function can take ***any*** number of arguments, each of which can be an Object, Array, Boolean, or String.6162> **Important:** _Any_ falsey values are discarded!<br>Standalone Boolean values are discarded as well.6364```jschore(docs): fix typoTariq Porterbefore adoptionead41a3
line 65why, one step back65clsx(true, false, '', null, undefined, 0, NaN);chore: add readme docs & pkg keywordsLuke Edwardsbefore adoptionad0c4c5
lines 66–67why, one step back66//=> ''67```docs: attempt “bench” linksLuke Edwardsbefore adoptionf938b74
line 68why, one step back68feat: add `clsx/lite` moduleLuke Edwardsbefore adoption1a49142
lines 69–107why, one step back69## Modes7071There are multiple "versions" of `clsx` available, which allows you to bring only the functionality you need!7273#### `clsx`74> **Size (gzip):** 239 bytes<br>75> **Availability:** CommonJS, ES Module, UMD7677The default `clsx` module; see [API](#API) for info.7879```js80import { clsx } from 'clsx';81// or82import clsx from 'clsx';83```8485#### `clsx/lite`86> **Size (gzip):** 140 bytes<br>87> **Availability:** CommonJS, ES Module<br>88> **CAUTION:** Accepts **ONLY** string arguments!8990Ideal for applications that ***only*** use the string-builder pattern.9192Any non-string arguments are ignored!9394```js95import { clsx } from 'clsx/lite';96// or97import clsx from 'clsx/lite';9899// string100clsx('hello', true && 'foo', false && 'bar');101// => "hello foo"102103// NOTE: Any non-string input(s) ignored104clsx({ foo: true });105//=> ""106```107docs: attempt “bench” linksLuke Edwardsbefore adoptionf938b74
lines 108–109why, one step back108## Benchmarks109chore: fix readme bench links (#82)Andrew Pattonbefore adoption42354d3
line 110why, one step back110For snapshots of cross-browser results, check out the [`bench`](bench) directory~!fix: use `Array.isArray` check;Luke Edwardsbefore adoption0d6023a
lines 111–118why, one step back111112## Support113114All versions of Node.js are supported.115116All browsers that support [`Array.isArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#Browser_compatibility) are supported (IE9+).117118>**Note:** For IE8 support and older, please install `clsx@1.0.x` and beware of [#17](https://github.com/lukeed/clsx/issues/17).chore: add readme docs & pkg keywordsLuke Edwardsbefore adoptionad0c4c5
line 119why, one step back119chore: add Tailwind readme section (#65)Edgar Lópezbefore adoption496db1d
lines 120–121why, one step back120## Tailwind Support121chore: readme typo (#68)Kevin Lowebefore adoption4a4eadd
line 122why, one step back122Here some additional (optional) steps to enable classes autocompletion using `clsx` with Tailwind CSS.chore: add Tailwind readme section (#65)Edgar Lópezbefore adoption496db1d
line 123why, one step back123feat: add `clsx/lite` moduleLuke Edwardsbefore adoption1a49142
line 124why, one step back124<details>chore: add Tailwind readme section (#65)Edgar Lópezbefore adoption496db1d
lines 125–140why, one step back125<summary>126 Visual Studio Code127</summary>1281291. [Install the "Tailwind CSS IntelliSense" Visual Studio Code extension](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss)1301312. Add the following to your [`settings.json`](https://code.visualstudio.com/docs/getstarted/settings):132133 ```json134 {135 "tailwindCSS.experimental.classRegex": [136 ["clsx\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"]137 ]138 }139 ```140</details>feat: add `clsx/lite` moduleLuke Edwardsbefore adoption1a49142
lines 141–146why, one step back141142You may find the [`clsx/lite`](#clsxlite) module useful within Tailwind contexts. This is especially true if/when your application **only** composes classes in this pattern:143144```js145clsx('text-base', props.active && 'text-primary', props.className);146```chore: add readme docs & pkg keywordsLuke Edwardsbefore adoptionad0c4c5
lines 147–149why, one step back147148## Related149chore: update benchmark resultsLuke Edwardsbefore adoption75053c0
line 150why, one step back150- [obj-str](https://github.com/lukeed/obj-str) - A smaller (96B) and similiar utility that only works with Objects.chore: add readme docs & pkg keywordsLuke Edwardsbefore adoptionad0c4c5
lines 151–154why, one step back151152## License153154MIT © [Luke Edwards](https://lukeed.com)