clsx-react 0.9.0
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/LICENSE.txt +21 -0
- package/README.md +141 -0
- package/dist/jsx-dev-runtime.cjs +1 -0
- package/dist/jsx-dev-runtime.js +1 -0
- package/dist/jsx-runtime.cjs +1 -0
- package/dist/jsx-runtime.js +1 -0
- package/package.json +81 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zsolt Tövis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
[](https://github.com/toviszsolt/clsx-react/blob/main/LICENSE) [](https://www.npmjs.com/package/clsx-react) [](https://github.com/toviszsolt/clsx-react/stargazers) [](https://github.com/toviszsolt/clsx-react/actions/workflows/main.yml) [](https://codecov.io/gh/toviszsolt/clsx-react) [](https://github.com/sponsors/toviszsolt)
|
|
2
|
+
|
|
3
|
+
# `clsx-react`
|
|
4
|
+
|
|
5
|
+
**Stop importing `clsx` or `classnames` manually.**
|
|
6
|
+
|
|
7
|
+
A custom React JSX runtime that natively supports **arrays** and **objects** in the `className` prop. It automatically applies `clsx` logic at the runtime level, keeping your code clean and your imports empty.
|
|
8
|
+
|
|
9
|
+
## The Problem
|
|
10
|
+
|
|
11
|
+
```jsx
|
|
12
|
+
// ❌ Old way: Boilerplate everywhere
|
|
13
|
+
import clsx from 'clsx'; // or classnames
|
|
14
|
+
|
|
15
|
+
export const Button = ({ active, disabled }) => (
|
|
16
|
+
<button className={clsx('btn', { 'btn-active': active, 'btn-disabled': disabled })}>Click me</button>
|
|
17
|
+
);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## The Solution
|
|
21
|
+
|
|
22
|
+
```jsx
|
|
23
|
+
// ✅ New way: Zero imports, native syntax
|
|
24
|
+
export const Button = ({ active, disabled }) => (
|
|
25
|
+
<button className={['btn', { 'btn-active': active, 'btn-disabled': disabled }]}>Click me</button>
|
|
26
|
+
);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install clsx-react
|
|
35
|
+
# or
|
|
36
|
+
yarn add clsx-react
|
|
37
|
+
# or
|
|
38
|
+
pnpm add clsx-react
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> **Note:** Requires `react` >= 17.0.0.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Configuration
|
|
46
|
+
|
|
47
|
+
To make this work, you need to tell your compiler to use this package as the JSX Import Source instead of the default `react`.
|
|
48
|
+
|
|
49
|
+
### 1. TypeScript (`tsconfig.json`) - **Recommended**
|
|
50
|
+
|
|
51
|
+
This handles both the compilation and the type definitions (so TS won't complain about arrays in `className`).
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"compilerOptions": {
|
|
56
|
+
"jsx": "react-jsx",
|
|
57
|
+
"jsxImportSource": "clsx-react"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 2. Vite (`vite.config.ts`)
|
|
63
|
+
|
|
64
|
+
If you are using Vite, you can set it explicitly in the config:
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import { defineConfig } from 'vite';
|
|
68
|
+
import react from '@vitejs/plugin-react';
|
|
69
|
+
|
|
70
|
+
export default defineConfig({
|
|
71
|
+
plugins: [react()],
|
|
72
|
+
esbuild: {
|
|
73
|
+
jsxImportSource: 'clsx-react',
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 3. Next.js / SWC
|
|
79
|
+
|
|
80
|
+
Next.js usually respects `tsconfig.json`. Ensure your `compilerOptions` are set as shown in step 1.
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Usage Examples
|
|
85
|
+
|
|
86
|
+
Once configured, you can use `className` just like you would use the `clsx` function arguments.
|
|
87
|
+
|
|
88
|
+
### Conditional Classes (Object)
|
|
89
|
+
|
|
90
|
+
```jsx
|
|
91
|
+
<div className={{ hidden: isHidden, flex: isFlex }}>...</div>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Arrays
|
|
95
|
+
|
|
96
|
+
```jsx
|
|
97
|
+
<div className={['text-lg', 'font-bold', isError && 'text-red-500']}>...</div>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Mixed & Nested
|
|
101
|
+
|
|
102
|
+
```jsx
|
|
103
|
+
<div className={['p-4', { 'bg-gray-100': !dark }, ['shadow-md', 'rounded']]}>...</div>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Standard String (Still works)
|
|
107
|
+
|
|
108
|
+
```jsx
|
|
109
|
+
<div className="just-a-string">...</div>
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## How it works
|
|
115
|
+
|
|
116
|
+
This package wraps the standard `react/jsx-runtime` and `react/jsx-dev-runtime`. It intercepts the creation of every JSX element:
|
|
117
|
+
|
|
118
|
+
1. Checks if `className` prop exists.
|
|
119
|
+
2. Checks if `className` is **not** a string (array or object).
|
|
120
|
+
3. If so, it processes it with a bundled, lightweight version of `clsx`.
|
|
121
|
+
4. Passes the processed props to the original React runtime.
|
|
122
|
+
|
|
123
|
+
It adds negligible overhead (bytes) and eliminates the need to manually import and call class utilities in every single component file.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## TypeScript Support
|
|
128
|
+
|
|
129
|
+
This package includes a global augmentation for `React.HTMLAttributes`. Once you set `"jsxImportSource": "clsx-react"` in your `tsconfig.json`, TypeScript will automatically understand that `className` accepts arrays and objects. No extra `.d.ts` configuration needed!
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Guidelines
|
|
134
|
+
|
|
135
|
+
See [Code of Conduct](./CODE_OF_CONDUCT.md), [Contributing](./CONTRIBUTING.md), and [Security Policy](./SECURITY.md).
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
MIT License © 2022–2024 [Zsolt Tövis](https://github.com/toviszsolt)
|
|
140
|
+
|
|
141
|
+
If you find this project useful, please consider [sponsoring me on GitHub](https://github.com/sponsors/toviszsolt), [PayPal](https://www.paypal.com/paypalme/toviszsolt), or [give the repo a star](https://github.com/toviszsolt/clsx-react).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var f=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var l=(r,t)=>{for(var n in t)f(r,n,{get:t[n],enumerable:!0})},x=(r,t,n,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of u(t))!m.call(r,a)&&a!==n&&f(r,a,{get:()=>t[a],enumerable:!(e=i(t,a))||e.enumerable});return r};var g=r=>x(f({},"__esModule",{value:!0}),r);var D={};l(D,{Fragment:()=>o.Fragment,jsxDEV:()=>v});module.exports=g(D);function s(r){var t,n,e="";if(typeof r=="string"||typeof r=="number")e+=r;else if(typeof r=="object")if(Array.isArray(r)){var a=r.length;for(t=0;t<a;t++)r[t]&&(n=s(r[t]))&&(e&&(e+=" "),e+=n)}else for(n in r)r[n]&&(e&&(e+=" "),e+=n);return e}function y(){for(var r,t,n=0,e="",a=arguments.length;n<a;n++)(r=arguments[n])&&(t=s(r))&&(e&&(e+=" "),e+=t);return e}var c=y;var o=require("react/jsx-dev-runtime");function N(r){return function(t,n,...e){if(n&&n.className&&typeof n.className!="string"){let a={...n,className:c(n.className)};return r(t,a,...e)}return r(t,n,...e)}}var v=N(o.jsxDEV);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function o(r){var e,t,n="";if(typeof r=="string"||typeof r=="number")n+=r;else if(typeof r=="object")if(Array.isArray(r)){var a=r.length;for(e=0;e<a;e++)r[e]&&(t=o(r[e]))&&(n&&(n+=" "),n+=t)}else for(t in r)r[t]&&(n&&(n+=" "),n+=t);return n}function s(){for(var r,e,t=0,n="",a=arguments.length;t<a;t++)(r=arguments[t])&&(e=o(r))&&(n&&(n+=" "),n+=e);return n}var f=s;import{Fragment as x,jsxDEV as c}from"react/jsx-dev-runtime";function i(r){return function(e,t,...n){if(t&&t.className&&typeof t.className!="string"){let a={...t,className:f(t.className)};return r(e,a,...n)}return r(e,t,...n)}}var g=i(c);export{x as Fragment,g as jsxDEV};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var o=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var l=(r,t)=>{for(var s in t)o(r,s,{get:t[s],enumerable:!0})},g=(r,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of m(t))!x.call(r,e)&&e!==s&&o(r,e,{get:()=>t[e],enumerable:!(n=u(t,e))||n.enumerable});return r};var y=r=>g(o({},"__esModule",{value:!0}),r);var b={};l(b,{Fragment:()=>a.Fragment,jsx:()=>N,jsxs:()=>v});module.exports=y(b);function f(r){var t,s,n="";if(typeof r=="string"||typeof r=="number")n+=r;else if(typeof r=="object")if(Array.isArray(r)){var e=r.length;for(t=0;t<e;t++)r[t]&&(s=f(r[t]))&&(n&&(n+=" "),n+=s)}else for(s in r)r[s]&&(n&&(n+=" "),n+=s);return n}function j(){for(var r,t,s=0,n="",e=arguments.length;s<e;s++)(r=arguments[s])&&(t=f(r))&&(n&&(n+=" "),n+=t);return n}var c=j;var a=require("react/jsx-runtime");function i(r){return function(t,s,...n){if(s&&s.className&&typeof s.className!="string"){let e={...s,className:c(s.className)};return r(t,e,...n)}return r(t,s,...n)}}var N=i(a.jsx),v=i(a.jsxs);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function a(r){var n,t,s="";if(typeof r=="string"||typeof r=="number")s+=r;else if(typeof r=="object")if(Array.isArray(r)){var e=r.length;for(n=0;n<e;n++)r[n]&&(t=a(r[n]))&&(s&&(s+=" "),s+=t)}else for(t in r)r[t]&&(s&&(s+=" "),s+=t);return s}function c(){for(var r,n,t=0,s="",e=arguments.length;t<e;t++)(r=arguments[t])&&(n=a(r))&&(s&&(s+=" "),s+=n);return s}var o=c;import{Fragment as g,jsx as i,jsxs as u}from"react/jsx-runtime";function f(r){return function(n,t,...s){if(t&&t.className&&typeof t.className!="string"){let e={...t,className:o(t.className)};return r(n,e,...s)}return r(n,t,...s)}}var y=f(i),j=f(u);export{g as Fragment,y as jsx,j as jsxs};
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "clsx-react",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "Custom React JSX Runtime to support arrays and objects in className prop natively.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"jsx",
|
|
8
|
+
"jsx-runtime",
|
|
9
|
+
"jsx-dev-runtime",
|
|
10
|
+
"className",
|
|
11
|
+
"clsx",
|
|
12
|
+
"styling",
|
|
13
|
+
"utility",
|
|
14
|
+
"typescript",
|
|
15
|
+
"css"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"types": "./dist/clsx-react.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
"./jsx-runtime": {
|
|
21
|
+
"types": "./dist/clsx-react.d.ts",
|
|
22
|
+
"import": "./dist/jsx-runtime.js",
|
|
23
|
+
"require": "./dist/jsx-runtime.cjs"
|
|
24
|
+
},
|
|
25
|
+
"./jsx-dev-runtime": {
|
|
26
|
+
"types": "./dist/clsx-react.d.ts",
|
|
27
|
+
"import": "./dist/jsx-dev-runtime.js",
|
|
28
|
+
"require": "./dist/jsx-dev-runtime.cjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"clsx-react.d.ts",
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"author": {
|
|
39
|
+
"name": "Zsolt Tövis",
|
|
40
|
+
"email": "tovis.zsolt@gmail.com",
|
|
41
|
+
"url": "https://toviszsolt.stacklegend.com/"
|
|
42
|
+
},
|
|
43
|
+
"funding": [
|
|
44
|
+
{
|
|
45
|
+
"type": "github",
|
|
46
|
+
"url": "https://github.com/sponsors/toviszsolt"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"type": "paypal",
|
|
50
|
+
"url": "https://www.paypal.com/paypalme/toviszsolt"
|
|
51
|
+
}
|
|
52
|
+
],
|
|
53
|
+
"homepage": "https://github.com/toviszsolt/clsx-react",
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "git+https://github.com/toviszsolt/clsx-react.git"
|
|
57
|
+
},
|
|
58
|
+
"bugs": {
|
|
59
|
+
"url": "https://github.com/toviszsolt/clsx-react/issues"
|
|
60
|
+
},
|
|
61
|
+
"license": "MIT",
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "yarn test && node scripts/build.js",
|
|
64
|
+
"test": "vitest run",
|
|
65
|
+
"test:watch": "vitest",
|
|
66
|
+
"test:coverage": "vitest run --coverage"
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"react": ">=17.0.0"
|
|
70
|
+
},
|
|
71
|
+
"devDependencies": {
|
|
72
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
73
|
+
"@testing-library/react": "^16.3.1",
|
|
74
|
+
"@vitest/coverage-v8": "^4.0.16",
|
|
75
|
+
"clsx": "^2.1.1",
|
|
76
|
+
"esbuild": "^0.27.2",
|
|
77
|
+
"jsdom": "^27.4.0",
|
|
78
|
+
"react": "^19.2.3",
|
|
79
|
+
"vitest": "^4.0.16"
|
|
80
|
+
}
|
|
81
|
+
}
|