UNPKG

3.04 kBMarkdownView Raw
1# Classcat
2
3[![CI](https://img.shields.io/travis/jorgebucaran/classcat/master.svg)](https://travis-ci.org/jorgebucaran/classcat) [![Codecov](https://img.shields.io/codecov/c/github/jorgebucaran/classcat/master.svg)](https://codecov.io/gh/jorgebucaran/classcat) [![npm](https://img.shields.io/npm/v/classcat.svg)](https://www.npmjs.org/package/classcat)
4
5Classcat is a JavaScript function to concatenate classNames declaratively.
6
7## Installation
8
9<pre>
10npm i <a href="https://www.npmjs.com/package/classcat">classcat</a>
11</pre>
12
13Don't want to set up a build environment? Download classcat from a CDN and it will be globally available through the `window.classcat` object. Classcat works in all browsers >= IE9.
14
15```html
16<script src="https://unpkg.com/classcat"></script>
17```
18
19## Usage
20
21Classcat is a unary function expecting an array of elements _or_ an object of key/value pairs and returns a string that is the result of joining all the elements in the array or object keys.
22
23```js
24import cc from "classcat"
25
26cc(["foo", "bar"]) //=> "foo bar"
27
28cc(["foo", { bar: true }]) //=> "foo bar"
29
30cc(["foo", { bar: true, fum: false }, "baz"]) //=> "foo bar baz"
31```
32
33[Falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) array elements and object properties will be ignored.
34
35```js
36import cc from "classcat"
37
38cc([
39 null,
40 false,
41 "foo",
42 undefined,
43 0,
44 1,
45 {
46 bar: null
47 }
48]) //=> "foo 1"
49```
50
51Here is an example styling a button that can be toggled on or off. Go ahead and [try it online](https://codepen.io/jorgebucaran/pen/NYgLwG?editors=0010).
52
53```jsx
54import cc from "classcat"
55
56export function ToggleButton({ toggle, isOn }) {
57 return (
58 <div class="btn" onclick={toggle}>
59 <div
60 class={cc({
61 circle: true,
62 off: !isOn,
63 on: isOn
64 })}
65 />
66 <span class={cc({ textOff: !isOn })}>{isOn ? "ON" : "OFF"}</span>
67 </div>
68 )
69}
70```
71
72## Benchmarks
73
74All benchmarks run on a 2.4GHz Intel Core i7 CPU with 16 GB memory. Please be aware that results may vary across browsers and Node.js runtimes.
75
76```
77npm run bench
78```
79
80<pre>
81# Strings
82classnames × 3,187,131 ops/sec
83<em>classcat × 15,132,350 ops/sec</em>
84
85# Objects
86classnames × 3,314,869 ops/sec
87<em>classcat × 20,206,909 ops/sec</em>
88
89# Strings & Objects
90classnames × 2,937,509 ops/sec
91<em>classcat × 11,734,207 ops/sec</em>
92
93# Arrays
94classnames × 903,155 ops/sec
95<em>classcat × 4,270,378 ops/sec</em>
96
97# Arrays & Objects
98classnames × 2,342,018 ops/sec
99<em>classcat × 5,083,398 ops/sec</em>
100</pre>
101
102## Comparisons
103
104Classcat operates similarly to JedWatson/classNames. The only difference is that classNames takes a [variable number of arguments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) whereas classcat takes a single argument.
105
106```js
107cc("foo", "bar", "baz") //=> "foo"
108```
109
110To work around this, wrap the arguments inside an array.
111
112```js
113cc(["foo", "bar", "baz"]) //=> "foo bar baz"
114```
115
116## License
117
118Classcat is MIT licensed. See [LICENSE](LICENSE.md).