UNPKG

2.63 kBMarkdownView Raw
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
5Classcat is a JavaScript function for conditionally concatenating CSS classes.
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
21Here is a button you can toggle on or off. Go ahead and [try it online](https://codepen.io/jorgebucaran/pen/NYgLwG).
22
23```js
24import cc from "classcat"
25
26export 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
48Classcat 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
50If the value associated with a given key is false or evaluates to false it will be ignored.
51
52```js
53cc([
54 "btn",
55 {
56 "btn-active": true,
57 "btn-large": false
58 }
59]) // => btn btn-active
60```
61
62## Benchmarks
63
64All 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```
67npm run bench
68```
69
70<pre>
71<em>Classcat – Strings × 11,992,788 ops/sec</em>
72classNames – Strings × 3,615,118 ops/sec
73Fastest is Classcat – Strings
74
75<em>Classcat – Objects × 15,036,351 ops/sec</em>
76classNames – Objects × 3,631,691 ops/sec
77Fastest is Classcat – Objects
78
79<em>Classcat – Strings & Objects × 9,677,306 ops/sec</em>
80classNames – Strings & Objects × 3,405,325 ops/sec
81Fastest is Classcat – Strings & Objects
82
83<em>Classcat – Mixed × 5,186,675 ops/sec</em>
84classNames – Mixed × 2,487,460 ops/sec
85Fastest is Classcat – Mixed
86
87<em>Classcat – Arrays × 3,225,468 ops/sec</em>
88classNames – Arrays × 818,056 ops/sec
89Fastest is Classcat – Arrays
90</pre>
91
92## License
93
94Classcat is MIT licensed. See [LICENSE](LICENSE.md).