Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsMay 30, 2026·2 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
classnames
Comando de instalación directa
npx -y tokrepo@latest install 0fc341e9-5c65-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con 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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados