clsx

A tiny (223B) utility for constructing `className` strings conditionally.

  • Owner: lukeed/clsx
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    4
      Compare:

Github stars Tracking Chart

clsx Build Status

A tiny (223B) utility for constructing className strings conditionally.Also serves as a faster & smaller drop-in replacement for the classnames module.

This module is available in three formats:

  • ES Module: dist/clsx.m.js
  • CommonJS: dist/clsx.js
  • UMD: dist/clsx.min.js

Install

$ npm install --save clsx

Usage

import clsx from 'clsx';

// Strings (variadic)
clsx('foo', true && 'bar', 'baz');
//=> 'foo bar baz'

// Objects
clsx({ foo:true, bar:false, baz:isTrue() });
//=> 'foo baz'

// Objects (variadic)
clsx({ foo:true }, { bar:false }, null, { '--foobar':'hello' });
//=> 'foo --foobar'

// Arrays
clsx(['foo', 0, false, 'bar']);
//=> 'foo bar'

// Arrays (variadic)
clsx(['foo'], ['', 0, false, 'bar'], [['baz', [['hello'], 'there']]]);
//=> 'foo bar baz hello there'

// Kitchen sink (with nesting)
clsx('foo', [1 && 'bar', { baz:false, bat:null }, ['hello', ['world']]], 'cya');
//=> 'foo bar hello world cya'

API

clsx(...input)

Returns: String

input

Type: Mixed

The clsx function can take any number of arguments, each of which can be an Object, Array, Boolean, or String.

Important: Any falsey values are discarded!Standalone Boolean values are discarded as well.

clsx(true, false, '', null, undefined, 0, NaN);
//=> ''

Benchmarks

For snapshots of cross-browser results, check out the bench directory~!

  • obj-str - Similar utility that is faster and smaller (96B) but only works with Objects.

License

MIT © Luke Edwards

Main metrics

Overview
Name With Ownerlukeed/clsx
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 2)
Platform
License:MIT License
所有者活动
Created At2018-12-24 22:33:11
Pushed At2024-06-10 15:48:22
Last Commit At2024-04-22 22:26:02
Release Count15
Last Release Namev2.1.1 (Posted on 2024-04-22 22:26:02)
First Release Namev0.0.1 (Posted on 2018-12-24 14:41:20)
用户参与
Stargazers Count9.1k
Watchers Count17
Fork Count157
Commits Count82
Has Issues Enabled
Issues Count59
Issue Open Count6
Pull Requests Count13
Pull Requests Open Count6
Pull Requests Close Count31
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private