|
1 | | -# Classcat [](https://github.com/jorgebucaran/classcat/releases/latest) |
| 1 | +# Classcat |
2 | 2 |
|
3 | | -> Build a space-separated [class attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class) quickly. |
| 3 | +> Build a [`class`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class) attribute string quickly. |
4 | 4 |
|
5 | | -- Easily add and remove class names based on a truthy or falsy value. |
6 | | -- Works best when paired with a view framework. which will it be? |
7 | | -- Up to 2.5x faster than the alternatives ([run the benchmarks](#run-the-benchmarks)). |
8 | | -- Ridiculously tiny at [260B](http://bundlephobia.com/result?p=classcat). No dependencies. |
| 5 | +- Framework agnostic, reusable, plain vanilla JavaScript. |
| 6 | +- Up to [2.5x faster]() than alternatives. |
| 7 | +- [217 B](http://bundlephobia.com/result?p=classcat) (minified+gzipped). 👌 |
9 | 8 |
|
10 | | -## Quickstart |
| 9 | +This module makes it easy to build a space-delimited `class` attribute string from an object or array of CSS class names. Just pair each class with a boolean value to add or remove them conditionally. |
11 | 10 |
|
12 | | -```console |
13 | | -npm i classcat |
14 | | -``` |
15 | | - |
16 | | -Don't want to set up a build step? Import it inside a `<script>` tag as a module. Don't worry; modules are supported in all evergreen, self-updating desktop, and mobile browsers. |
17 | | - |
18 | | -```html |
19 | | -<script type="module"> |
20 | | - import cc from "https://unpkg.com/classcat" |
21 | | -</script> |
22 | | -``` |
23 | | - |
24 | | -Classcat takes an array of strings or name-value object and joins all the [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) values into a space-separated string. Arrays may be nested too. That's really all there is to it. Here's the [first example](https://codepen.io/jorgebucaran/pen/NYgLwG?editors=0010) to get you started. |
25 | | - |
26 | | -```jsx |
| 11 | +```js |
27 | 12 | import cc from "classcat" |
28 | 13 |
|
29 | | -export const ToggleButton = ({ isOn }) => ( |
30 | | - <div class="btn"> |
| 14 | +export const ToggleButton = ({ isOn, toggle }) => ( |
| 15 | + <div className="btn" onClick={() => toggle(!isOn)}> |
31 | 16 | <div |
32 | | - class={cc({ |
| 17 | + className={cc({ |
33 | 18 | circle: true, |
34 | 19 | off: !isOn, |
35 | 20 | on: isOn, |
36 | 21 | })} |
37 | 22 | /> |
38 | | - <span class={cc({ textOff: !isOn })}>{isOn ? "ON" : "OFF"}</span> |
| 23 | + <span className={cc({ textOff: !isOn })}>{isOn ? "ON" : "OFF"}</span> |
39 | 24 | </div> |
40 | 25 | ) |
41 | 26 | ``` |
42 | 27 |
|
43 | | -## API |
| 28 | +[Try with React](https://codepen.io/jorgebucaran/pen/NYgLwG?editors=0010), [lit-html](https://codepen.io/jorgebucaran/pen/LjPJEp?editors=1000), [Mithril](https://codepen.io/jorgebucaran/pen/JjjOjwB?editors=1100), [Superfine](https://codepen.io/jorgebucaran/pen/wrMvjz?editors=1000) |
44 | 29 |
|
45 | | -### `cc(string | number | object | array)` |
| 30 | +## Installation |
46 | 31 |
|
47 | | -```js |
48 | | -import cc from "classcat" |
| 32 | +```console |
| 33 | +npm install classcat |
| 34 | +``` |
49 | 35 |
|
50 | | -cc("foo") //=> "foo" |
| 36 | +Or without a build step—import it right in your browser. |
51 | 37 |
|
52 | | -cc(["foo", "bar", "baz"]) //=> "foo bar baz" |
| 38 | +```html |
| 39 | +<script type="module"> |
| 40 | + import cc from "https://unpkg.com/classcat" |
| 41 | +</script> |
| 42 | +``` |
53 | 43 |
|
54 | | -cc({ foo: false, bar: null, baz: undefined }) //=> "" |
| 44 | +## API |
55 | 45 |
|
56 | | -cc({ foo: true, bar: false, baz: true }) //=> "foo baz" |
| 46 | +### `cc(names)` |
57 | 47 |
|
58 | | -cc([{ foo: true, bar: false }, "baz"]) //=> "foo baz" |
| 48 | +```ps |
| 49 | +cc(names: string | number | object | array): string |
59 | 50 | ``` |
60 | 51 |
|
61 | | -## Run the benchmarks |
| 52 | +```js |
| 53 | +import cc from "classcat" |
62 | 54 |
|
63 | | -```console |
64 | | -npm run build && npm i -C bench && npm -C bench start |
| 55 | +cc("elf") //=> "elf" |
| 56 | + |
| 57 | +cc(["elf", "orc", "gnome"]) //=> "elf orc gnome" |
| 58 | + |
| 59 | +cc({ |
| 60 | + elf: false, |
| 61 | + orc: null, |
| 62 | + gnome: undefined, |
| 63 | +}) //=> "" |
| 64 | + |
| 65 | +cc({ |
| 66 | + elf: true, |
| 67 | + orc: false, |
| 68 | + gnome: true, |
| 69 | +}) //=> "elf gnome" |
| 70 | + |
| 71 | +cc([ |
| 72 | + { |
| 73 | + elf: true, |
| 74 | + orc: false, |
| 75 | + }, |
| 76 | + "gnome", |
| 77 | +]) //=> "elf gnome" |
65 | 78 | ``` |
66 | 79 |
|
| 80 | +## Benchmarks |
| 81 | + |
67 | 82 | ```console |
68 | | -# Strings |
69 | | -classcat × 15,927,163 ops/sec |
70 | | -classnames × 2,694,533 ops/sec |
71 | | -clsx × 8,542,847 ops/sec |
72 | | - |
73 | | -# Objects |
74 | | -classcat × 15,205,051 ops/sec |
75 | | -classnames × 2,873,497 ops/sec |
76 | | -clsx × 8,806,231 ops/sec |
77 | | - |
78 | | -# Strings/Objects |
79 | | -classcat × 13,834,475 ops/sec |
80 | | -classnames × 3,013,424 ops/sec |
81 | | -clsx × 5,890,821 ops/sec |
82 | | - |
83 | | -# Arrays |
84 | | -classcat × 3,649,723 ops/sec |
85 | | -classnames × 709,177 ops/sec |
86 | | -clsx × 2,513,014 ops/sec |
87 | | - |
88 | | -# Arrays/Objects |
89 | | -classcat × 4,290,009 ops/sec |
90 | | -classnames × 1,856,967 ops/sec |
91 | | -clsx × 3,099,573 ops/sec |
92 | | - |
93 | | -# Arguments vs Array |
94 | | -classcat × 3,089,353 ops/sec |
95 | | -classnames × 828,906 ops/sec |
96 | | -clsx × 3,057,879 ops/sec |
| 83 | +npm --prefix bench start |
97 | 84 | ``` |
98 | 85 |
|
99 | 86 | ## License |
|
0 commit comments