Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

lite-classnames

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lite-classnames

A lightweight ("lite") classnames generation utility function

latest
Source
npmnpm
Version
1.3.0
Version published
Maintainers
1
Created
Source

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 and
  • Record<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.

Usage

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'

Background

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.

FAQ

Why only support 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.

Why no 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.

Why no support for arrays of strings or records?

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 🤷

Will you accept feature requests?

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.

Keywords

css

FAQs

Package last updated on 03 Jul 2025

Did you know?

Socket

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.

Install

Related posts