Configs2026年5月30日·1 分钟阅读

classnames — Conditional CSS Class Name Utility for JavaScript

A simple utility for conditionally joining CSS class names together, used by millions of React and JavaScript projects to manage dynamic styling.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
classnames
直接安装命令
npx -y tokrepo@latest install 0fc341e9-5c65-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

classnames is a tiny JavaScript utility that conditionally joins CSS class name strings. It accepts strings, objects, and arrays, making it the standard way to compute dynamic class attributes in React and other UI frameworks.

What classnames Does

  • Joins multiple class name strings into a single space-separated string
  • Conditionally includes classes based on boolean object values
  • Accepts nested arrays and recursively flattens them
  • Ignores falsy values (null, undefined, false, 0) automatically
  • Ships a dedupe variant that removes duplicate class names

Architecture Overview

The library is a single function with no dependencies. It iterates over its arguments, checking each type: strings and numbers are appended directly, objects have their keys appended when the corresponding value is truthy, and arrays are recursively processed. The result is concatenated with spaces and returned. The entire module is under 300 bytes minified.

Self-Hosting & Configuration

  • Install via npm: npm install classnames
  • Import the default export or the classnames/dedupe variant
  • Use with React's className prop or any framework's class binding
  • Works in Node.js, browsers, and bundlers with no configuration
  • TypeScript type definitions are included out of the box

Key Features

  • Tiny footprint at under 300 bytes minified and gzipped
  • Handles strings, objects, arrays, and mixed argument types
  • Dedupe variant prevents duplicate class names in the output
  • Zero dependencies and zero configuration required
  • Over 10 million weekly npm downloads — an ecosystem staple

Comparison with Similar Tools

  • clsx — newer alternative with a nearly identical API and slightly smaller bundle
  • tailwind-merge — specifically handles Tailwind CSS class conflicts; classnames is generic
  • classcat — another tiny utility; classnames has broader adoption and community trust
  • Template literals — manual string interpolation works but is error-prone with conditionals
  • cx (Emotion) — tied to Emotion CSS-in-JS; classnames is framework-agnostic

FAQ

Q: What is the difference between classnames and clsx? A: Both have nearly identical APIs. clsx is marginally smaller; classnames has longer history and broader usage.

Q: Does it work with Tailwind CSS? A: Yes. Use it to conditionally apply Tailwind classes. For conflict resolution, pair it with tailwind-merge.

Q: Can I use it outside React? A: Absolutely. It is a plain JavaScript function with no framework dependency.

Q: Does it support TypeScript? A: Yes. Built-in type definitions ship with the package.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产