
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
nano-classnames
Advanced tools
A bite-size utility for composing classnames.
First, install nano-classnames
via npm/yarn/pnpm:
npm i nano-classnames
yarn add nano-classnames
pnpm i nano-classnames
Then import the class constructor in your project:
import { cn } from "nano-classnames"
You can now compose your classnames:
<div class={cn("class-1", "class-2")}>...</div>
//<div class="class-1 class-2">...<div>
Read the documention to learn more about composing classes with nano-classnames
.
nano-classnames
nano-classnames
was inspired from the great classnames
package with an emphasis on speed and composability. While I love classnames
, I found its syntax for conditional classnames to be disruptive as you must use an object to apply conditional classes, which can be clunky to work with. nano-classnames
does away with this by using tuples (an array) to quickly apply conditional classes.
nano-classnames
also only supports strings for composing classes (hence "nano"). This is an intentional design choice as, personally, I never use nested arrays/objects when composing classes. If you need to use them for composing classes then I recommend using classnames
.
nano-classnames
is not a drop-in replacement for classnames
By design, nano-classnames
is meant to be extremely lightweight and straightforward. It works great if you're using TailwindCSS!
cn
to compose classescn
is the class constructor, it allows you to quickly compose classes together.
You can import cn
from nano-classnames
using a named import:
import { cn } from "nano-classnames"
Then use cn
like this:
cn("text-red-500 text-lg", "font-bold") ➡️ "text-red-500 text-lg font-bold"
We only support string
inputs! If you need to use arrays, objects, or nested arrays/objects then refer to classnames
for a more robust solution!
To conditionally apply classnames, use a tuple (an array)! These tuples are formatted like this:
[boolean, trueClassnames, falseClassnames]
You can then pass these into the cn
constructor:
let state = true;
cn([state, "text-red-500"]) ➡️ "text-red-500"
You can also pass a third value to represent an else
state:
let state = true;
cn([state, "text-red-500", "text-blue-500"]) ➡️ "text-blue-500"
You can also pass in a variable (so as long as it is a string!)
let classes = "..."
cn(classes) ➡️ "..."
cn(...classes)
...classes: (string | [boolean, string, string])[]
string
FAQs
🧹 A bite-size utility for composing classnames.
We found that nano-classnames demonstrated a not healthy version release cadence and project activity because the last version was released 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
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.