simple-merge-class-names 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,107 @@
|
|
|
1
|
-
|
|
1
|
+
# simple-merge-class-names
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A straightforward utility for merging CSS class names in `React`, `Tailwind` and other JavaScript projects.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add simple-merge-class-names
|
|
9
|
+
# or
|
|
10
|
+
yarn add simple-merge-class-names
|
|
11
|
+
# or
|
|
12
|
+
npm install simple-merge-class-names
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Install Prettier with VSCode (Recommended)
|
|
16
|
+
|
|
17
|
+
For optimal developer experience, install Prettier for VS Code: [https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode), or an equivalent auto code formatter for your IDE.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
The single function provided by this package is `mergeClassNames(...args)`.
|
|
22
|
+
|
|
23
|
+
```jsx
|
|
24
|
+
import { mergeClassNames } from "simple-merge-class-names";
|
|
25
|
+
|
|
26
|
+
function MyComponent() {
|
|
27
|
+
return (
|
|
28
|
+
<div
|
|
29
|
+
className={mergeClassNames(
|
|
30
|
+
"app",
|
|
31
|
+
"min-h-dvh",
|
|
32
|
+
"grid",
|
|
33
|
+
"grid-rows-[auto_1fr_auto]",
|
|
34
|
+
"outline"
|
|
35
|
+
)}
|
|
36
|
+
>
|
|
37
|
+
Hello, world!
|
|
38
|
+
</div>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Using individual strings for each class name can be tedious, however it significantly enhances code readability and Developer Experience (DX). This is in contrast to hard-to-read strings like `className="app min-h-dvh grid grid-rows-[auto_1fr_auto] outline"`.
|
|
44
|
+
|
|
45
|
+
## To minimize typing strain:
|
|
46
|
+
|
|
47
|
+

|
|
48
|
+
|
|
49
|
+
- Use single quotes for class names, often a single key press on many keyboards.
|
|
50
|
+
- Save the file (e.g., with `Ctrl+S`), and Prettier does the rest; it automatically:
|
|
51
|
+
- Replaces single quotes with double quotes.
|
|
52
|
+
- Neatly arranges each class name on a new line.
|
|
53
|
+
|
|
54
|
+
## Why the mismatch between Function, and Package name?
|
|
55
|
+
|
|
56
|
+
Although I wanted to name the package as `mergeClassNames` to reflect the single exported function, the NPM Package Registry does not allow capital letters, only lower case and dash characters. In addition there was already a package named merge-class-names but it is no longer maintained.
|
|
57
|
+
|
|
58
|
+
## Where this Package excels
|
|
59
|
+
|
|
60
|
+
While similar packages exist (see `clsx`) with often better features and potentially improved performance, `simple-merge-class-names` focuses on being very straightforward and easy to reason about, as defined in its source code.
|
|
61
|
+
|
|
62
|
+
## Source Code
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
/**
|
|
66
|
+
* mergeClassNames - a utility to merge CSS class names I developed for use in my `React` + `Tailwind` projects. Use it in conjunction with an auto-formatting tool like `Prettier`
|
|
67
|
+
*
|
|
68
|
+
* Example usage: <div className = {mergeClassNames("flex", "flex-col")}/>
|
|
69
|
+
*
|
|
70
|
+
* @license AGPL-3.0
|
|
71
|
+
* Copyright (C) 2025 Abdullah Fatota
|
|
72
|
+
*
|
|
73
|
+
* This program is free software: you can redistribute it and/or modify
|
|
74
|
+
* it under the terms of the GNU Affero General Public License as published by
|
|
75
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
76
|
+
* (at your option) any later version.
|
|
77
|
+
*
|
|
78
|
+
* This program is distributed in the hope that it will be useful,
|
|
79
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
80
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
81
|
+
* GNU Affero General Public License for more details.
|
|
82
|
+
*
|
|
83
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
84
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
const isDefined = (val) => val !== undefined && val !== null;
|
|
88
|
+
|
|
89
|
+
const isNotEmptyString = (val) => val !== "";
|
|
90
|
+
|
|
91
|
+
export const mergeClassNames = (...args) => {
|
|
92
|
+
const space = " ";
|
|
93
|
+
const values = args.filter(
|
|
94
|
+
(val) => isDefined(val) && isNotEmptyString(val)
|
|
95
|
+
);
|
|
96
|
+
const className = values.join(space);
|
|
97
|
+
return className;
|
|
98
|
+
};
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Production Considerations
|
|
102
|
+
|
|
103
|
+
If you are considering this package for production, you might also want to look into `clsx` for more advanced features: [https://www.npmjs.com/package/clsx](https://www.npmjs.com/package/clsx)
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
This project is licensed under the AGPL-3.0 License. See `LICENSE.txt` for full details.
|
package/package.json
CHANGED