simple-merge-class-names 1.0.0 → 1.0.2
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 +132 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,134 @@
|
|
|
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
|
+
```
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
yarn add simple-merge-class-names
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install simple-merge-class-names
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Install `Prettier` with VSCode (Most Recommended)
|
|
20
|
+
|
|
21
|
+
Install Prettier for VS Code for most optimal developer experience: [https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
|
|
22
|
+
|
|
23
|
+
Or install an equivalent auto code formatter for your IDE.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
`mergeClassNames(...args)` is the single exported function from this package package.
|
|
28
|
+
|
|
29
|
+
```jsx
|
|
30
|
+
import { mergeClassNames } from "simple-merge-class-names";
|
|
31
|
+
|
|
32
|
+
function MyComponent() {
|
|
33
|
+
return (
|
|
34
|
+
<div
|
|
35
|
+
className={mergeClassNames(
|
|
36
|
+
"app",
|
|
37
|
+
"min-h-dvh",
|
|
38
|
+
"grid",
|
|
39
|
+
"grid-rows-[auto_1fr_auto]",
|
|
40
|
+
"outline"
|
|
41
|
+
)}
|
|
42
|
+
>
|
|
43
|
+
Hello, world!
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Using individual strings for each class name can be tedious (see [Workflow to minimize typing strain](#workflow-to-minimize-typing-strain)), however it significantly enhances code readability and Developer Experience (DX). This is in contrast to hard-to-read strings like:
|
|
50
|
+
|
|
51
|
+
```jsx
|
|
52
|
+
function MyComponent() {
|
|
53
|
+
return (
|
|
54
|
+
<div className="app min-h-dvh grid grid-rows-[auto_1fr_auto] outline">
|
|
55
|
+
Hello, world!
|
|
56
|
+
</div>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Workflow to minimize typing strain:
|
|
62
|
+
|
|
63
|
+

|
|
64
|
+
|
|
65
|
+
- Have `Prettier` installed
|
|
66
|
+
- Use single quotes (<kbd>'</kbd>) for class names, often a single key press on many keyboards.
|
|
67
|
+
- Save the file (<kbd>Ctrl+S</kbd>), and Prettier does the formatting heavy-lifting, it automatically:
|
|
68
|
+
- Replaces single quotes with double quotes.
|
|
69
|
+
- Neatly arranges each class name on a new line.
|
|
70
|
+
|
|
71
|
+
## Why the mismatch between exported Function, and Package name?
|
|
72
|
+
|
|
73
|
+
I wanted to name the package as `mergeClassNames` to reflect the single exported function, but the NPM Package Registry does not allow capital letters, only lower case and dash characters.
|
|
74
|
+
|
|
75
|
+
In addition there was already a package named `merge-class-names` but it is no longer maintained (and the developer recommends `clsx` instead).
|
|
76
|
+
|
|
77
|
+
## Where this package excels
|
|
78
|
+
|
|
79
|
+
While similar packages exist (`clsx`) with 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.
|
|
80
|
+
|
|
81
|
+
## Source Code
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
/**
|
|
85
|
+
* 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`
|
|
86
|
+
*
|
|
87
|
+
* Example usage: <div className = {mergeClassNames("flex", "flex-col")}/>
|
|
88
|
+
*
|
|
89
|
+
* @license AGPL-3.0
|
|
90
|
+
* Copyright (C) 2025 Abdullah Fatota
|
|
91
|
+
*
|
|
92
|
+
* This program is free software: you can redistribute it and/or modify
|
|
93
|
+
* it under the terms of the GNU Affero General Public License as published by
|
|
94
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
95
|
+
* (at your option) any later version.
|
|
96
|
+
*
|
|
97
|
+
* This program is distributed in the hope that it will be useful,
|
|
98
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
99
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
100
|
+
* GNU Affero General Public License for more details.
|
|
101
|
+
*
|
|
102
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
103
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
const isDefined = (val) => val !== undefined && val !== null;
|
|
107
|
+
|
|
108
|
+
const isNotEmptyString = (val) => val !== "";
|
|
109
|
+
|
|
110
|
+
export const mergeClassNames = (...args) => {
|
|
111
|
+
const space = " ";
|
|
112
|
+
const values = args.filter(
|
|
113
|
+
(val) => isDefined(val) && isNotEmptyString(val)
|
|
114
|
+
);
|
|
115
|
+
const className = values.join(space);
|
|
116
|
+
return className;
|
|
117
|
+
};
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Argument Handling
|
|
121
|
+
|
|
122
|
+
`mergeClassNames` accepts multiple arguments but filters out `null`, `undefined`, and empty strings (`""`), and the remaining values are either strings or are _implicitly converted_ to strings by the JavaScript engine.
|
|
123
|
+
|
|
124
|
+
Finally the end string values are merged and separated by spaces.
|
|
125
|
+
|
|
126
|
+
## Production Considerations
|
|
127
|
+
|
|
128
|
+
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)
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
This project is licensed under the AGPL-3.0 License. See `LICENSE.txt` for full details.
|
|
133
|
+
|
|
134
|
+
## Enjoy 😉
|
package/package.json
CHANGED