
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
lite-classnames
Advanced tools
lite-classnames
A lightweight ("lite") classnames generation utility function.
This is a simpler alternative to the classnames package with a smaller API surface for lean classnames string generation.
It's not meant to be a 1-to-1 drop-in replacement, but rather a more opinionated lightweight approach that encourages intentional usage. It only supports the following types as input:
string
andRecord<string, boolean>
Instead of supporting various input types and conditionally checking every user input, the responsibility shifts to the user to handle this programmatically. This leaves the function with less work to do, resulting in a smaller footprint and better performance.
import classNames from 'lite-classNames';
classNames('foo', 'bar', 'quux'); // => 'foo bar quux'
classNames({ foo: true, bar: true, quux: true }); // => 'foo bar quux'
classNames('foo', { bar: true }, 'quux'); // => 'foo bar quux'
classNames('foo', { bar: false }, 'quux'); // => 'foo quux'
classNames({ foo: true, bar: false, quux: true }); // => 'foo quux'
At my previous workplace, we frequently used the classnames utility function, but it became difficult to reason about due to varying preferences for how arguments were passed to the function.
We also noticed performance issues on low-end devices that caused janky CSS animations and poor user experience, since the classnames evaluation could be skipped on some animation frames.
We needed something with better performance that enforced consistent usage with fewer input options, resulting in code that was easier to understand and more performant.
This is the result of that effort.
string
and Record<string, boolean>
?To discourage multiple ways of expressing the same result in code. The current API surface provides the minimum functionality we needed to get things done.
null
, undefined
, or number
support?Less is more. While numbers
can be coerced to strings, we leave this to be handled at the call site rather than within the function itself. CSS classes are rarely numeric values alone without some string prefix/suffix.
This is by design - we wanted to avoid complex and/or nested structures and prevent the need for a recursive function. You can always destructure directly in your code:
classNames(...list);
However, this approach makes code less readable in our opinion.
You could always wrap the function yourself to add array support 🤷
Most likely not. If you prefer a different approach, consider using the original classnames library or other alternatives such as:
Alternatively, you can copy/paste the code itself (about 20 lines) and modify it to suit your needs.
FAQs
A lightweight ("lite") classnames generation utility function
We found that lite-classnames 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
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.