classnames

A tiny utility for conditionally joining CSS class names together.

Library
npm
v2.5.1
17,770 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
56 /100 Fair
Development Activity 56
Maintenance 8
Community 60
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
87 /100 Excellent
Architecture 82
Code Quality 88
Innovation 85
Learning Curve 92

classnames is a lightweight JavaScript utility, ubiquitous in the React ecosystem, that merges strings, objects, and arrays into a single space-separated className string. Instead of hand-building conditional className logic with ternaries and string concatenation, developers pass truthy/falsy flags as a plain object and let classnames decide which class names survive.

With zero runtime dependencies and a footprint of well under 1KB, it has become a de facto standard for building dynamic UI class lists in component-based frameworks, and ships bind and dedupe variants for CSS Modules and dedup-sensitive use cases respectively.

What You Get

  • Zero-dependency, sub-1KB core classNames() function for conditional class joining
  • classnames/bind variant pre-bound to a CSS Modules style map via a cx() helper
  • classnames/dedupe variant that removes duplicate and overridden class names
  • Full TypeScript type definitions shipped inline (index.d.ts, bind.d.ts, dedupe.d.ts)
  • ESM-only package with sideEffects: false for optimal tree-shaking

Common Use Cases

  • Toggling a CSS class based on component state (e.g. isActive, isPressed, isHovered)
  • Merging a consumer-supplied className prop with a component’s own base classes
  • Building dynamic utility-class strings from multiple conditional sources
  • Binding to CSS Modules style objects to reference local class names instead of raw strings

Under The Hood

Architecture classnames is deliberately minimal: three independent entry points (index.js, bind.js, dedupe.js) share the same conceptual pipeline of recursively flattening arguments (string, object, or array) into class names, but each swaps its accumulation strategy for a different goal. The default build concatenates into a string as it goes; bind.js layers a this-bound lookup so cx() can translate logical names into CSS Modules’ hashed class names; dedupe.js instead accumulates into a null-prototype StorageObject keyed by class name so later truthy/falsy values naturally overwrite earlier ones, producing de-duplicated output at roughly 5x the cost of the default. Module boundaries are exposed cleanly via package.json exports subpaths rather than a single monolithic API.

Tech Stack The package itself has zero runtime dependencies and ships as pure ESM ("type": "module") with sideEffects: false for tree-shaking. Its devDependency set is entirely tooling: Rollup for browser benchmark bundling, tsd for asserting the shipped .d.ts files against real usage, and tinybench/http-server for the in-repo benchmark suite that runs in CI. TypeScript types are hand-written and versioned alongside the JS, not generated.

Code Quality The tests/ directory covers all three entry points (index.js, bind.js, dedupe.js) plus .test-d.ts type-assertion files, using Node’s built-in node:test runner — no external test framework dependency. Coverage is thorough for a small library: truthy/falsy edge cases (NaN, -0, empty string, empty object/array), deep array recursion, custom/inherited toString() overrides, and even execution inside a Node vm context to catch cross-realm bugs. CI (.github/workflows/node.js.yml) runs the test suite across Node 18/20/21, type-checks the .d.ts files, and runs the benchmark suite on every push and PR — there is no separate ESLint/Prettier config in the repo, so style consistency relies on convention rather than tooling.

API Design The public API is a single variadic function call that accepts strings, objects, arrays, or any mix thereof, which removes essentially all boilerplate compared to manual ternary/string-concatenation class building. Argument handling is intuitive and well documented in the README with realistic React examples. The bind and dedupe variants reuse the exact same calling convention as the default export, so switching between them is a one-line import change with no API relearning. The main friction point is the ESM-only, type: module packaging, which is a minor breaking change for consumers still on CommonJS-only tooling.

Used by 68 apps in this directory

Go
58%
Apache 2.0

Authgear

Authentication

2,123

Open-source, self-hostable authentication platform with passkeys, biometric login, SSO, MFA, and GraphQL admin API — a full Auth0/Clerk/Firebase alternative for SaaS and mobile apps.

View details
89
Repo Health
81
Technical
65
Dependency
Built with
Go 58%
HTML 21%
TypeScript 18%
Updated 1 weeks ago
TypeScript
97%
AGPL 3.0

Bigcapital

Invoicing Finance

3,956

Self-hostable double-entry accounting platform with invoicing, inventory, multi-currency, and real-time financial reporting for small and medium businesses.

View details
91
Repo Health
77
Technical
61
Dependency
Built with
TypeScript 97%
Updated 1 weeks ago
TypeScript
96%
Other

Cal.diy

Scheduling

49,266

The 100% MIT-licensed, community-driven scheduling platform — self-host your own booking infrastructure with no enterprise strings attached.

View details
88
Repo Health
86
Technical
64
Dependency
Built with
TypeScript 96%
Updated 1 weeks ago
Java
49%
Apache 2.0

ClearFlask

Community · Product Management

450

Open-source feedback management and roadmap tool that lets product teams collect, prioritize, and respond to user input — with AI-powered summarization and full self-hosting control.

View details
82
Repo Health
73
Technical
62
Dependency
Built with
Java 49%
TypeScript 46%
Updated 2 weeks ago
TypeScript
45%
Other

Convex Backend

Databases · Developer Tools

12,754

Open-source reactive database that lets developers build live-updating apps with pure TypeScript, strong consistency, and real-time subscriptions—no separate API layer required.

View details
88
Repo Health
82
Technical
65
Dependency
Built with
TypeScript 45%
Rust 43%
Updated 6 days ago
TypeScript
88%
MIT

Drawnix

Design Tools · Productivity

14,977

All-in-one open-source whiteboard combining mind maps, flowcharts, and freehand drawing on a single infinite canvas.

View details
60
Repo Health
69
Technical
79
Dependency
Built with
TypeScript 88%
Updated 1 months ago
TypeScript
94%
AGPL 3.0

Element Web

Collaboration · Team Chat

13,609

A polished, self-hostable Matrix client for secure, decentralized messaging and collaboration that puts your organization in full control of its data.

View details
96
Repo Health
83
Technical
64
Dependency
Built with
TypeScript 94%
Updated 1 weeks ago
Go
51%
MIT

Fathom Lite

Analytics

8,019

A simple, self-hosted website analytics tool built with Go and Preact that lets you understand your traffic without handing data to third parties.

View details
47
Repo Health
67
Technical
67
Dependency
Built with
Go 51%
JavaScript 31%
CSS 15%
Updated 7 months ago
Python
63%
BSD 3

Flagsmith

Ab Testing Experimentation · Developer Tools · Devops

6,613

Open-source feature flagging, remote config, and A/B/multivariate testing platform for web, mobile, and server-side apps — self-host or use the hosted SaaS.

View details
90
Repo Health
82
Technical
63
Dependency
Built with
Python 63%
TypeScript 32%
Updated 6 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers