
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
The classcat npm package is a utility for conditionally combining class names. It is useful in scenarios where you need to dynamically apply multiple class names based on certain conditions, making it particularly handy in React and other component-based frameworks.
Combining Class Names
This feature allows you to combine multiple class names into a single string. It is useful when you have a list of class names that you want to apply to an element.
const cc = require('classcat');
const className = cc(['class1', 'class2']);
console.log(className); // Output: 'class1 class2'
Conditional Class Names
This feature allows you to conditionally apply class names based on boolean values. Only the class names with a true value will be included in the final string.
const cc = require('classcat');
const className = cc({ 'class1': true, 'class2': false });
console.log(className); // Output: 'class1'
Nested Arrays
This feature supports nested arrays and objects, allowing for more complex class name combinations. It will recursively process the nested structures to generate the final class name string.
const cc = require('classcat');
const className = cc(['class1', ['class2', { 'class3': true }]]);
console.log(className); // Output: 'class1 class2 class3'
The classnames package is another utility for conditionally combining class names. It offers similar functionality to classcat, allowing you to combine class names based on various conditions. The main difference is in the API design and usage patterns.
The clsx package is a tiny utility for constructing className strings conditionally. It is similar to classcat in terms of functionality but is designed to be even smaller and faster. It also supports nested arrays and objects for complex class name combinations.
Build a
class
attribute string quickly.
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
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"
npm --prefix bench start
FAQs
Build a class attribute string quickly.
The npm package classcat receives a total of 1,038,391 weekly downloads. As such, classcat popularity was classified as popular.
We found that classcat demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.