clsx

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

  • 所有者: lukeed/clsx
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    4
      比较:

Github星跟踪图

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

主要指标

概览
名称与所有者lukeed/clsx
主编程语言JavaScript
编程语言JavaScript (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2018-12-24 22:33:11
推送于2024-06-10 15:48:22
最后一次提交2024-04-22 22:26:02
发布数15
最新版本名称v2.1.1 (发布于 2024-04-22 22:26:02)
第一版名称v0.0.1 (发布于 2018-12-24 14:41:20)
用户参与
星数9.1k
关注者数17
派生数157
提交数82
已启用问题?
问题数59
打开的问题数6
拉请求数13
打开的拉请求数6
关闭的拉请求数31
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?