A straightforward utility for merging CSS class names in React (JSX) and other JavaScript projects.
For Production look into https://www.npmjs.com/package/clsx
- simple-merge-class-names
Palestine is about fundamental non-negotiable human rights. End all financial and diplomatic ties with i*rael.
pnpm add simple-merge-class-namesyarn add simple-merge-class-namesnpm install simple-merge-class-namesIt will nicely format your code especially when you have lots of classes, and will significantly improve your visual experience.
If you have a different IDE use an equivalent auto code formatter tool/extension
| Function | Prints console warnings | Activates debugger |
|---|---|---|
mergeClassNames |
✅ | ❌ |
mergeClassNamesDebugger |
✅ | ✅ |
Example:
import { mergeClassNames } from "simple-merge-class-names";
const Component = ({ condition }) => {
return (
<div
className={mergeClassNames(
"app",
condition ? "min-h-dvh" : false,
"grid",
"grid-rows-[auto_1fr_auto]",
"outline"
)}
>
Hello, world!
</div>
);
};export declare const mergeClassNames: (
...args: (string | false)[]
) => string | false;
export declare const mergeClassNamesDebugger: (
...args: (string | false)[]
) => string | false;Only 2:
-
Valid strings, not whitespace, of length >= 1
(As long as you have content in the string you're OK)
-
false
Example, This is OK:
mergeClassNames(
"mx-auto",
"min-dvh ",
" flex",
" grid ",
"italic font-bold ",
`
gap-y-4
`,
false,
condition ? "daisy-btn-active" : false
);Each below argument will be ignored, and cause a Dev Console warning to be printed to alert you:
- Empty strings: (e.g.
"") - Whitespace any consecutive combination of the following:
- new lines,
- spaces,
- tabs
- (e.g.
" ","\n "," \t \n ", etc.)
trueundefinednull- Objects
- Numbers
- Big Int
- Symbols
// Example: These arguments will be **ignored**, and a console.warn will be printed
const someVariable = "";
mergeClassNames(
someVariable, // empty string
" ", // whitespace
"\n ", // whitespace
" \t \n ", // whitespace
` // whitespace
\n
`,
true, // true
undefined, // undefined
null, // null
{
// object
name: "value",
email: "email@example.com",
},
123, // number
123.45 // number
);- To avoid silent failures, because you will be pulling your hair asking why a Tailwind class isn't working only to figure out you passed an object, array or an empty string instead of a valid string. (It could also be because of an unsupported class name or typo but this is beyond the scope of this package)
Use this pattern:
condition ? "class-name" : falsewithfalseserving as the valid fallback.- or
condition === true ? "class-name" : falseif you want to be specific.
Note: Avoid using the short-circuit implicit syntax like this:
condition && "class-name", beside less readable code, it can produce falsy values which will be ignored (e.g.0,"",undefined, andnull).
import { mergeClassNames } from "simple-merge-class-names";
const Component = () => {
return (
<div
className={mergeClassNames(
"app",
condition ? "min-h-dvh" : false,
"grid",
"grid-rows-[auto_1fr_auto]",
"outline"
)}
>
Hello, world!
</div>
);
};Either:
-
Valid
string, never whitespace, always length >= 1 -
or
false(if all input arguments were invalid)
Because of safe return types you can chain calls safely without worrying about warnings or arguments being ignored.
So this is OK:
mergeClassNames(condition ? "disabled" : mergeClassNames(...) )Once you see warnings in the console, the next step is to use mergeClassNamesDebugger
- Enable Debugger
-
Use
import {mergeClassNamesDebugger as mergeClassNames}to debug the entire file, and keep the rest intact.import { mergeClassNamesDebugger as mergeClassNames } from "simple-merge-class-names"; const Component = ({ condition }) => { return ( <div className={mergeClassNames( "app", condition ? "min-h-dvh" : false, "grid", "grid-rows-[auto_1fr_auto]", "outline" )} > Hello, world! </div> ); };
-
or call
mergeClassNamesDebuggerdirectly.import { mergeClassNamesDebugger } from "simple-merge-class-names"; const Component = ({ condition }) => { return ( <div className={mergeClassNamesDebugger( "app", condition ? "min-h-dvh" : false, "grid", "grid-rows-[auto_1fr_auto]", "outline" )} > Hello, world! </div> ); };
-
Refresh the page, the debugger should connect:
- Navigate to the Call stack
- Click the function/component right before
mergeClassNamesDebugger
Use single quotes around class names, and activate Prettier which will neatly format and arrange the classes.
-
Install
Prettier -
Enable
Editor: Word Wrap:- Open
Settings (UI)→Editor: Word Wrap→on - or
User Settings (JSON)and add this entry"editor.wordWrap": "on"
- Open
-
Use single quotes (') around class names
-
Save the file
Before and after:
This project uses Vitest as the test runner for fast and modern testing.
git clone https://github.com/new-AF/simple-merge-class-names
cd simple-merge-class-names
pnpm testpnpm test:watchThis project is licensed under the AGPL-3.0 License. See LICENSE.txt for full details.
Enjoy 😉




