GitHub - JorgeBucaran/classwrap: 0.3 KB JavaScript utility for conditionally con...
source link: https://github.com/JorgeBucaran/classwrap
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Classcat
Build a
class
attribute string quickly.
- Framework agnostic, reusable, plain vanilla JavaScript.
- Up to 2.5x faster than alternatives.
- 217 B (minified+gzipped).
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.
import cc from "classcat"
export const ToggleButton = ({ isOn, toggle }) => (
<div className="btn" onClick={() => toggle(!isOn)}>
<div
className={cc({
circle: true,
off: !isOn,
on: isOn,
})}
/>
<span className={cc({ textOff: !isOn })}>{isOn ? "ON" : "OFF"}</span>
</div>
)
Try with React, lit-html, Mithril, Superfine
Installation
npm install classcat
Or without a build step—import it right in your browser.
<script type="module">
import cc from "https://unpkg.com/classcat"
</script>
cc(names)
cc(names: string | number | object | array): string
import cc from "classcat"
cc("elf") //=> "elf"
cc(["elf", "orc", "gnome"]) //=> "elf orc gnome"
cc({
elf: false,
orc: null,
gnome: undefined,
}) //=> ""
cc({
elf: true,
orc: false,
gnome: true,
}) //=> "elf gnome"
cc([
{
elf: true,
orc: false,
},
"gnome",
]) //=> "elf gnome"
Benchmarks
npm --prefix bench start
License
Recommend
-
150
Getopts Parse CLI arguments. Lightweight drop-in replacement for minimist and clones. Small (180 LOC), focused, no dependencies. Up to
-
50
Conditionally Disabling and Filtering Tests in JUnit 5 Attention: The following article was published over 3 years ago, and the information provided may be aged or outdated. Please keep that in mind as you read the post.
-
30
GitHub is where people build software. More than 27 million people use GitHub to discover, fork, and contribute to over 80 million projects.
-
36
readme.md class-names
-
58
I have ASP.NET Core web application where I need to include partial view only if it exists. The application uses areas and some areas may have their specific side menu. This blog post shows two ways how to conditionally i...
-
29
Superfine Superfine is a minimal view layer for building web interfaces. Think Hyperapp without the framework—no state machines, effects, or subscriptions—jus...
-
4
Conditionally deploying parts of your CloudFormation stack with the Serverless Framework What do you do when you want to customize which resources are deployed via CloudFo...
-
7
Not every weakly constant function is conditionally constant As discussed at length on the...
-
3
-
0
Conditionally spreading objects in JavaScript In JavaScript, if you want to populate an object with some properties from another object, you can use the spread operator (...) to do...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK