Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

tailwindcss-classnames

> 前端框架
Open source

Functional typed classnames for TailwindCSS

709 stars0 likes0 views
WebsiteGitHub

About

Functional typed classnames for TailwindCSS

tailwindcss-classnames

Functional typed classnames for TailwindCSS

TailwindCSS is a CSS library that has gained a lot of traction. The developer experience is pretty epic and you ensure a low footprint on your css by composing existing classes for most of your css.

So why mess with it?

TailwindCSS is based on strings and with some nice tooling on top like TailwindCSS VSCode extension you get a pretty descent experience. That said, there are limitations to a purely declarative approach of strings. When using tailwindcss-classnames you will get additional power in the form of:

  • Validation of classnames: You can not write the wrong classname, cause the API only allows you to insert valid classnames
  • Functional approach: Since we are working in Typescript we get more freedom in using functional powers like composition and dynamic composition
  • Defining by variables: Even though it is nice to write TailwindCSS inline with your elements, it does not scale. You want to move definitions outside of the component for reusability and composition
  • Support for all editors and IDEs: Because it's just TypeScript types, you get these powers in all editors and IDEs that support TypeScript.

You can not get this experience using pure TailwindCSS and the VSCode extension, but you do get it with tailwindcss-classnames.

Install

Please follow the guide to set up TailwindCSS. Now do:

npm install tailwindcss-classnames

NOTE: This project versions match with TailwindCSS versions except for semver patch releases

The project is literally the clsx project with custom typing. That means it arrives at your browser at approximately 2.7kB minified and gzipped (bundlephobia).

What's New in v3

  • Way better performance overall (thanks to @dylanvann's idea and suggestions):

    • Generated file size is reduced to be ** classnames(styles.button, { [styles.disabled]: disabled, });

// App.tsx import * as React from "react"; import * as styles from "./styles";

export const App: React.FC = ({ disabled }) => { return (

			Submit
		
		Cancel
	
);

};

…

json "scripts": { "generate-css-types": "tailwindcss-classnames -i path/to/tailwind.config.js -o path/to/output-file.ts" }


**⚠️ NOTE:** that if you want to add custom types from external file, the type must be a default export:

```ts
export default MyCustomType;
type MyCustomType =
  | "btn"
  | "sidebar"
  ...

How to use generated types

Method 1

import the generated file (and NOT the actual library) into your code in order to get the customized classnames, like this:

import { classnames } from "path/to/generated/tailwindcss-classnames";

Method 2

A more elegant approach is to add the generated file to your projects tsconfig.json compilerOptions paths which should allow you to keep the import the same and use your generated version of tailwindcss-classnames instead of the node_modules one.

{
	"compilerOptions": {
		"paths": {
			"tailwindcss-classnames": ["path/to/generated/tailwindcss-classnames"]
		}
	}
}

then:

import { classnames } from "tailwindcss-classnames";

From original comment by @andykenward

Known limitiations

  • Relative imports inside the config does not work. use __dirname instead. See #120 .
  • npx tailwindcss-classnames won't work. Use as an npm script as mentioned above.
  • Only official TailwindLabs plugins are supported.
  • Some JIT features are not supported (#204).

Any help with these issues is very much appreciated.

Contributing

All contributions from everyone are very welcome.

Please read the contributing guidelines before submitting a Pull Request.

Credits

This project was started by Christian Alfoni and is now maintained by Muhammad Sammy. The full list of contributors can be found here.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptcssfunctional-cssreactreactjs

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架