1 | # Classcat
|
2 |
|
3 | [![Travis 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 |
|
5 | Classcat is a JavaScript function for conditionally concatenating CSS classes.
|
6 |
|
7 | ## Installation
|
8 |
|
9 | <pre>
|
10 | npm i <a href="https://www.npmjs.com/package/classcat">classcat</a>
|
11 | </pre>
|
12 |
|
13 | Don'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 |
|
21 | Here is a button you can toggle on or off. Go ahead and [try it online](https://codepen.io/jorgebucaran/pen/NYgLwG).
|
22 |
|
23 | ```js
|
24 | import cc from "classcat"
|
25 |
|
26 | export function ToggleButton({ toggle, isOn }) {
|
27 | return (
|
28 | <div class="btn" onclick={toggle}>
|
29 | <div
|
30 | class={cc({
|
31 | circle: true,
|
32 | off: !isOn,
|
33 | on: isOn
|
34 | })}
|
35 | />
|
36 | <span
|
37 | class={cc({
|
38 | textOff: !isOn
|
39 | })}
|
40 | >
|
41 | {isOn ? "ON" : "OFF"}
|
42 | </span>
|
43 | </div>
|
44 | )
|
45 | }
|
46 | ```
|
47 |
|
48 | Classcat is a unary function (accepts a single argument) expecting an array of elements or an object of keys and returns a string that is the result of joining all elements of the array or object keys.
|
49 |
|
50 | If the value associated with a given key is false or evaluates to false it will be ignored.
|
51 |
|
52 | ```js
|
53 | cc([
|
54 | "btn",
|
55 | {
|
56 | "btn-active": true,
|
57 | "btn-large": false
|
58 | }
|
59 | ]) // => btn btn-active
|
60 | ```
|
61 |
|
62 | ## Benchmarks
|
63 |
|
64 | All 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.
|
65 |
|
66 | ```
|
67 | npm run bench
|
68 | ```
|
69 |
|
70 | <pre>
|
71 | <em>Classcat – Strings × 11,992,788 ops/sec</em>
|
72 | classNames – Strings × 3,615,118 ops/sec
|
73 | Fastest is Classcat – Strings
|
74 |
|
75 | <em>Classcat – Objects × 15,036,351 ops/sec</em>
|
76 | classNames – Objects × 3,631,691 ops/sec
|
77 | Fastest is Classcat – Objects
|
78 |
|
79 | <em>Classcat – Strings & Objects × 9,677,306 ops/sec</em>
|
80 | classNames – Strings & Objects × 3,405,325 ops/sec
|
81 | Fastest is Classcat – Strings & Objects
|
82 |
|
83 | <em>Classcat – Mixed × 5,186,675 ops/sec</em>
|
84 | classNames – Mixed × 2,487,460 ops/sec
|
85 | Fastest is Classcat – Mixed
|
86 |
|
87 | <em>Classcat – Arrays × 3,225,468 ops/sec</em>
|
88 | classNames – Arrays × 818,056 ops/sec
|
89 | Fastest is Classcat – Arrays
|
90 | </pre>
|
91 |
|
92 | ## License
|
93 |
|
94 | Classcat is MIT licensed. See [LICENSE](LICENSE.md).
|