@windi-ui/tailwind 0.7.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-PRESENT Waleed Khaled <https://github.com/Waleed-KH>
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/dist/index.cjs ADDED
@@ -0,0 +1,330 @@
1
+ 'use strict';
2
+
3
+ const createPlugin = require('tailwindcss/plugin');
4
+ const core = require('@windi-ui/core');
5
+ const node_module = require('node:module');
6
+ const node_fs = require('node:fs');
7
+
8
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
9
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
10
+
11
+ const createPlugin__default = /*#__PURE__*/_interopDefaultCompat(createPlugin);
12
+
13
+ function hslToRgb(h, s, l) {
14
+ let r, g, b;
15
+ if (s === 0) {
16
+ r = g = b = l;
17
+ } else {
18
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
19
+ const p = 2 * l - q;
20
+ r = hueToRgb(p, q, h + 1 / 3);
21
+ g = hueToRgb(p, q, h);
22
+ b = hueToRgb(p, q, h - 1 / 3);
23
+ }
24
+ return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
25
+ }
26
+ function hueToRgb(p, q, t) {
27
+ if (t < 0) t += 1;
28
+ if (t > 1) t -= 1;
29
+ if (t < 1 / 6) return p + (q - p) * 6 * t;
30
+ if (t < 1 / 2) return q;
31
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
32
+ return p;
33
+ }
34
+
35
+ function _interopDefaultCompat$1(e) {
36
+ return e && typeof e === "object" && "default" in e ? e.default : e;
37
+ }
38
+ const require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
39
+ const flattenColorPalette = _interopDefaultCompat$1(
40
+ require$1(require$1.resolve("tailwindcss/lib/util/flattenColorPalette", { paths: [process.cwd()] }))
41
+ );
42
+ const NUM_REGEX = /(\d*\.\d+|\d+)/;
43
+ const ANGLE_REGEX = new RegExp(`^${NUM_REGEX.source}(deg|rad|grad|turn)?$`);
44
+ const PERCENT_REGEX = new RegExp(`^${NUM_REGEX.source}%?$`);
45
+ let twPkgPath;
46
+ let twPkg;
47
+ try {
48
+ twPkgPath = node_module.findPackageJSON(undefined("tailwindcss", process.cwd()));
49
+ if (twPkgPath) {
50
+ twPkg = JSON.parse(node_fs.readFileSync(twPkgPath, { encoding: "utf8" }));
51
+ }
52
+ } catch (e) {
53
+ console.error("Failed to read Tailwind CSS package.json:", e);
54
+ }
55
+ const TW_VER_REGEX = /^(3|4)\./;
56
+ const match = twPkg?.version.match(TW_VER_REGEX);
57
+ if (!match) {
58
+ console.warn(`Unsupported Tailwind CSS version: ${twPkg?.version}. Expected 3.x or 4.x.`);
59
+ }
60
+ const TailwindVer = match ? Number(match[1]) : 3;
61
+ function createTailwindTheme(tw) {
62
+ return TailwindVer === 4 ? new TailwindV4Theme(tw) : new TailwindV3Theme(tw);
63
+ }
64
+ class TailwindTheme {
65
+ theme;
66
+ fSize;
67
+ constructor(pluginApi) {
68
+ this.theme = pluginApi.theme;
69
+ this.fSize = this.theme("fontSize");
70
+ }
71
+ colors(name) {
72
+ const cname = name ? `colors.${name}` : "colors";
73
+ const c = this.theme(cname);
74
+ return typeof c === "object" ? flattenColorPalette(c) : c;
75
+ }
76
+ spacing(name) {
77
+ return this.theme(`spacing[${name}]`);
78
+ }
79
+ fontSize(name) {
80
+ return this.fSize[name];
81
+ }
82
+ }
83
+ class TailwindV4Theme extends TailwindTheme {
84
+ ver = 4;
85
+ constructor(pluginApi) {
86
+ super(pluginApi);
87
+ }
88
+ colorValue(value, alpha) {
89
+ return this.withAlpha(value, alpha);
90
+ }
91
+ applyTextColor(value, target, alpha) {
92
+ target.color = this.withAlpha(value, alpha);
93
+ }
94
+ applyBackgroundColor(value, target, alpha) {
95
+ target.backgroundColor = this.withAlpha(value, alpha);
96
+ }
97
+ applyBorderColor(value, target, alpha) {
98
+ target.borderColor = this.withAlpha(value, alpha);
99
+ }
100
+ // https://github.com/tailwindlabs/tailwindcss/blob/main/packages/tailwindcss/src/utilities.ts#L165C1-L182C2
101
+ withAlpha(value, alpha) {
102
+ if (alpha == null || alpha === "") return value;
103
+ const alphaAsNumber = Number(alpha);
104
+ if (!Number.isNaN(alphaAsNumber)) {
105
+ alpha = `${alphaAsNumber * 100}%`;
106
+ }
107
+ if (alpha === "100%") {
108
+ return value;
109
+ }
110
+ if (typeof alpha === "string" && alpha.startsWith("var(")) {
111
+ alpha = `calc(${alpha} * 100%)`;
112
+ }
113
+ return `color-mix(in oklab, ${value} ${alpha}, transparent)`;
114
+ }
115
+ }
116
+ class TailwindV3Theme extends TailwindTheme {
117
+ ver = 3;
118
+ withAlphaVariable = _interopDefaultCompat$1(require$1("tailwindcss/lib/util/withAlphaVariable"));
119
+ parseColorFormat = require$1("tailwindcss/lib/util/pluginUtils").parseColorFormat;
120
+ parseColor = require$1("tailwindcss/lib/util/color").parseColor;
121
+ corePlugins;
122
+ constructor(pluginApi) {
123
+ super(pluginApi);
124
+ this.corePlugins = (p) => pluginApi.corePlugins(p);
125
+ }
126
+ colors(name) {
127
+ const c = super.colors(name);
128
+ return typeof c === "string" ? this.colorToRgb(c) : this.colorsToRgb(c);
129
+ }
130
+ colorValue(value, alpha) {
131
+ const c = this.parseRgbFormat(value);
132
+ return typeof c === "string" ? c : c({ opacityValue: alpha?.toString() });
133
+ }
134
+ applyTextColor(value, target, alpha) {
135
+ this.applyColor({
136
+ color: value,
137
+ property: "color",
138
+ variable: "--tw-text-opacity"
139
+ }, "textOpacity", target, alpha);
140
+ }
141
+ applyBackgroundColor(value, target, alpha) {
142
+ this.applyColor({
143
+ color: value,
144
+ property: "background-color",
145
+ variable: "--tw-bg-opacity"
146
+ }, "backgroundOpacity", target, alpha);
147
+ }
148
+ applyBorderColor(value, target, alpha) {
149
+ this.applyColor({
150
+ color: value,
151
+ property: "border-color",
152
+ variable: "--tw-border-opacity"
153
+ }, "borderOpacity", target, alpha);
154
+ }
155
+ applyColor(args, opacityPlugin, target, alpha) {
156
+ args = {
157
+ ...args,
158
+ color: this.parseRgbFormat(args.color)
159
+ };
160
+ Object.assign(target, this.corePlugins?.(opacityPlugin) ? this.withAlphaVariable(args) : { [args.property]: this.toColorValue(args.color, alpha) });
161
+ if (alpha != null && alpha !== "" && args.variable in target) {
162
+ const alphaValue = alpha.toString();
163
+ target[args.variable] = alphaValue;
164
+ }
165
+ }
166
+ toColorValue(maybeFunction, alpha) {
167
+ return typeof maybeFunction === "function" ? maybeFunction(alpha != null && alpha !== "" ? { opacityValue: alpha.toString() } : {}) : maybeFunction;
168
+ }
169
+ parseRgbFormat(color) {
170
+ return this.parseColorFormat(`rgb(${color} / <alpha-value>)`);
171
+ }
172
+ colorsToRgb(colors) {
173
+ for (const c in colors) {
174
+ if (c === "black" || c === "white" || !c.startsWith("c-") && /^(?:.+-)?(?:50|950|[1-9]00)$/.exec(c))
175
+ colors[c] = this.colorToRgb(colors[c]);
176
+ }
177
+ return colors;
178
+ }
179
+ colorToRgb(color) {
180
+ const parsed = this.parseColor(color, { loose: true });
181
+ if (parsed?.mode.startsWith("rgb")) {
182
+ return parsed.color.join(" ");
183
+ } else if (parsed?.mode.startsWith("hsl")) {
184
+ if (parsed.color.length === 3) {
185
+ const rh = ANGLE_REGEX.exec(parsed.color[0]);
186
+ const rs = PERCENT_REGEX.exec(parsed.color[1]);
187
+ const rl = PERCENT_REGEX.exec(parsed.color[2]);
188
+ if (rh && rs && rl) {
189
+ const h = parseFloat(rh[1]) / (rh[2] === "rad" ? 2 * Math.PI : rh[2] === "grad" ? 400 : rh[2] === "turn" ? 1 : 360);
190
+ const s = parseFloat(rs[1]) / 100;
191
+ const l = parseFloat(rl[1]) / 100;
192
+ return hslToRgb(h, s, l).join(" ");
193
+ }
194
+ }
195
+ }
196
+ console.warn(`Unsupported color format: ${color}`);
197
+ return color;
198
+ }
199
+ }
200
+
201
+ const BLOCK_SELECTOR_REGEX = /^([^{]+)\{([^}]+)\}$/;
202
+ function getDarkSelector(darkMode) {
203
+ let [mode, selector = ".dark"] = Array.isArray(darkMode) ? darkMode : [darkMode];
204
+ if (typeof selector === "function") {
205
+ selector = selector();
206
+ }
207
+ if (typeof selector === "string") {
208
+ selector = [selector];
209
+ }
210
+ if (mode === "variant") {
211
+ for (const s of selector) {
212
+ if (!s.startsWith("@") && !s.includes("&")) {
213
+ console.warn("Invalid `variant` selector");
214
+ return [];
215
+ }
216
+ }
217
+ } else if (selector.length !== 1) {
218
+ console.warn("Invalid `darkMode` selector");
219
+ return [];
220
+ }
221
+ switch (mode) {
222
+ case "media":
223
+ return [["@media (prefers-color-scheme: dark)"]];
224
+ case "class":
225
+ return [[`&:is(${selector[0]} *)`]];
226
+ case "selector":
227
+ return [[`&:where(${selector[0]}, ${selector[0]} *)`]];
228
+ case "variant": {
229
+ const ss = [];
230
+ for (const s of selector) {
231
+ const bsMatch = BLOCK_SELECTOR_REGEX.exec(s);
232
+ ss.push(bsMatch ? [bsMatch[1].trim(), bsMatch[2].trim()] : [s]);
233
+ }
234
+ return ss;
235
+ }
236
+ default:
237
+ return [selector];
238
+ }
239
+ }
240
+ function setNestedValue(obj, keys, value) {
241
+ if (keys.length === 0) {
242
+ return obj;
243
+ }
244
+ let curr = obj;
245
+ for (let i = 0; i < keys.length; i++) {
246
+ const K = keys[i];
247
+ if (i === keys.length - 1) {
248
+ curr[K] = value;
249
+ } else {
250
+ if (typeof curr[K] !== "object" || curr[K] === null) {
251
+ curr[K] = {};
252
+ }
253
+ curr = curr[K];
254
+ }
255
+ }
256
+ return obj;
257
+ }
258
+ function applyDark(selector, styles) {
259
+ const { "@dark": darkStyle, ...style } = styles;
260
+ if (darkStyle) {
261
+ for (const s of selector) {
262
+ setNestedValue(style, s, darkStyle);
263
+ }
264
+ }
265
+ return style;
266
+ }
267
+
268
+ function keyValuePairs(keys, val = (k) => k) {
269
+ return keys.reduce((acc, k) => {
270
+ acc[k] = val(k);
271
+ return acc;
272
+ }, {});
273
+ }
274
+ function WindUITailwindCSS(config = {}) {
275
+ const wiu = core.create(config);
276
+ return createPlugin__default((tw) => {
277
+ const tp = createTailwindTheme(tw);
278
+ const gt = wiu.generator(tp);
279
+ const darkMode = tw.config("darkMode", "media");
280
+ const darkSelector = getDarkSelector(darkMode);
281
+ tw.addBase({ ":root": gt.colors.rootVars() });
282
+ tw.addBase({ ":root": gt.sizes.rootVars() });
283
+ tw.addBase({ "*": applyDark(darkSelector, gt.variants.rootVars()) });
284
+ tw.matchUtilities({
285
+ c: (val) => gt.colors.cssVars(val)
286
+ }, { values: keyValuePairs(gt.colors.names()) });
287
+ tw.matchUtilities({
288
+ v: (val) => applyDark(darkSelector, gt.variants.cssVars(val))
289
+ }, { values: keyValuePairs(gt.variants.names()) });
290
+ tw.matchUtilities({
291
+ "bg": (val, ext) => {
292
+ return gt.variants.utilCss("bg", val, ext.modifier);
293
+ },
294
+ "border": (val, ext) => {
295
+ return gt.variants.utilCss("border", val, ext.modifier);
296
+ },
297
+ "text": (val, ext) => {
298
+ return gt.variants.utilCss("text", val, ext.modifier);
299
+ }
300
+ }, {
301
+ values: { v: "default", "v-soft": "soft", "v-muted": "muted", "v-accent": "accent" },
302
+ modifiers: tw.theme("opacity")
303
+ });
304
+ tw.matchUtilities({
305
+ s: (val) => gt.sizes.cssVars(val)
306
+ }, {
307
+ values: keyValuePairs(gt.sizes.names())
308
+ });
309
+ tw.addComponents([...gt.components.css()]);
310
+ }, {
311
+ content: [],
312
+ theme: {
313
+ extend: {
314
+ colors({ colors }) {
315
+ return {
316
+ c: keyValuePairs(Object.keys(colors.gray), (v) => {
317
+ const c = wiu.vars.color(v);
318
+ return TailwindVer === 4 ? c : `rgb(${c} / <alpha-value>)`;
319
+ }),
320
+ // TODO: add as configurable option
321
+ default: colors.gray,
322
+ accent: colors.blue
323
+ };
324
+ }
325
+ }
326
+ }
327
+ });
328
+ }
329
+
330
+ module.exports = WindUITailwindCSS;
@@ -0,0 +1,6 @@
1
+ import createPlugin from 'tailwindcss/plugin';
2
+ import { Config } from '@windi-ui/core';
3
+
4
+ declare function WindUITailwindCSS(config?: Config): ReturnType<typeof createPlugin>;
5
+
6
+ export = WindUITailwindCSS;
@@ -0,0 +1,6 @@
1
+ import createPlugin from 'tailwindcss/plugin';
2
+ import { Config } from '@windi-ui/core';
3
+
4
+ declare function WindUITailwindCSS(config?: Config): ReturnType<typeof createPlugin>;
5
+
6
+ export { WindUITailwindCSS as default };
@@ -0,0 +1,6 @@
1
+ import createPlugin from 'tailwindcss/plugin';
2
+ import { Config } from '@windi-ui/core';
3
+
4
+ declare function WindUITailwindCSS(config?: Config): ReturnType<typeof createPlugin>;
5
+
6
+ export = WindUITailwindCSS;
package/dist/index.mjs ADDED
@@ -0,0 +1,323 @@
1
+ import createPlugin from 'tailwindcss/plugin';
2
+ import { create } from '@windi-ui/core';
3
+ import { createRequire, findPackageJSON } from 'node:module';
4
+ import { readFileSync } from 'node:fs';
5
+
6
+ function hslToRgb(h, s, l) {
7
+ let r, g, b;
8
+ if (s === 0) {
9
+ r = g = b = l;
10
+ } else {
11
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
12
+ const p = 2 * l - q;
13
+ r = hueToRgb(p, q, h + 1 / 3);
14
+ g = hueToRgb(p, q, h);
15
+ b = hueToRgb(p, q, h - 1 / 3);
16
+ }
17
+ return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
18
+ }
19
+ function hueToRgb(p, q, t) {
20
+ if (t < 0) t += 1;
21
+ if (t > 1) t -= 1;
22
+ if (t < 1 / 6) return p + (q - p) * 6 * t;
23
+ if (t < 1 / 2) return q;
24
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
25
+ return p;
26
+ }
27
+
28
+ function _interopDefaultCompat(e) {
29
+ return e && typeof e === "object" && "default" in e ? e.default : e;
30
+ }
31
+ const require = createRequire(import.meta.url);
32
+ const flattenColorPalette = _interopDefaultCompat(
33
+ require(require.resolve("tailwindcss/lib/util/flattenColorPalette", { paths: [process.cwd()] }))
34
+ );
35
+ const NUM_REGEX = /(\d*\.\d+|\d+)/;
36
+ const ANGLE_REGEX = new RegExp(`^${NUM_REGEX.source}(deg|rad|grad|turn)?$`);
37
+ const PERCENT_REGEX = new RegExp(`^${NUM_REGEX.source}%?$`);
38
+ let twPkgPath;
39
+ let twPkg;
40
+ try {
41
+ twPkgPath = findPackageJSON(import.meta.resolve("tailwindcss", process.cwd()));
42
+ if (twPkgPath) {
43
+ twPkg = JSON.parse(readFileSync(twPkgPath, { encoding: "utf8" }));
44
+ }
45
+ } catch (e) {
46
+ console.error("Failed to read Tailwind CSS package.json:", e);
47
+ }
48
+ const TW_VER_REGEX = /^(3|4)\./;
49
+ const match = twPkg?.version.match(TW_VER_REGEX);
50
+ if (!match) {
51
+ console.warn(`Unsupported Tailwind CSS version: ${twPkg?.version}. Expected 3.x or 4.x.`);
52
+ }
53
+ const TailwindVer = match ? Number(match[1]) : 3;
54
+ function createTailwindTheme(tw) {
55
+ return TailwindVer === 4 ? new TailwindV4Theme(tw) : new TailwindV3Theme(tw);
56
+ }
57
+ class TailwindTheme {
58
+ theme;
59
+ fSize;
60
+ constructor(pluginApi) {
61
+ this.theme = pluginApi.theme;
62
+ this.fSize = this.theme("fontSize");
63
+ }
64
+ colors(name) {
65
+ const cname = name ? `colors.${name}` : "colors";
66
+ const c = this.theme(cname);
67
+ return typeof c === "object" ? flattenColorPalette(c) : c;
68
+ }
69
+ spacing(name) {
70
+ return this.theme(`spacing[${name}]`);
71
+ }
72
+ fontSize(name) {
73
+ return this.fSize[name];
74
+ }
75
+ }
76
+ class TailwindV4Theme extends TailwindTheme {
77
+ ver = 4;
78
+ constructor(pluginApi) {
79
+ super(pluginApi);
80
+ }
81
+ colorValue(value, alpha) {
82
+ return this.withAlpha(value, alpha);
83
+ }
84
+ applyTextColor(value, target, alpha) {
85
+ target.color = this.withAlpha(value, alpha);
86
+ }
87
+ applyBackgroundColor(value, target, alpha) {
88
+ target.backgroundColor = this.withAlpha(value, alpha);
89
+ }
90
+ applyBorderColor(value, target, alpha) {
91
+ target.borderColor = this.withAlpha(value, alpha);
92
+ }
93
+ // https://github.com/tailwindlabs/tailwindcss/blob/main/packages/tailwindcss/src/utilities.ts#L165C1-L182C2
94
+ withAlpha(value, alpha) {
95
+ if (alpha == null || alpha === "") return value;
96
+ const alphaAsNumber = Number(alpha);
97
+ if (!Number.isNaN(alphaAsNumber)) {
98
+ alpha = `${alphaAsNumber * 100}%`;
99
+ }
100
+ if (alpha === "100%") {
101
+ return value;
102
+ }
103
+ if (typeof alpha === "string" && alpha.startsWith("var(")) {
104
+ alpha = `calc(${alpha} * 100%)`;
105
+ }
106
+ return `color-mix(in oklab, ${value} ${alpha}, transparent)`;
107
+ }
108
+ }
109
+ class TailwindV3Theme extends TailwindTheme {
110
+ ver = 3;
111
+ withAlphaVariable = _interopDefaultCompat(require("tailwindcss/lib/util/withAlphaVariable"));
112
+ parseColorFormat = require("tailwindcss/lib/util/pluginUtils").parseColorFormat;
113
+ parseColor = require("tailwindcss/lib/util/color").parseColor;
114
+ corePlugins;
115
+ constructor(pluginApi) {
116
+ super(pluginApi);
117
+ this.corePlugins = (p) => pluginApi.corePlugins(p);
118
+ }
119
+ colors(name) {
120
+ const c = super.colors(name);
121
+ return typeof c === "string" ? this.colorToRgb(c) : this.colorsToRgb(c);
122
+ }
123
+ colorValue(value, alpha) {
124
+ const c = this.parseRgbFormat(value);
125
+ return typeof c === "string" ? c : c({ opacityValue: alpha?.toString() });
126
+ }
127
+ applyTextColor(value, target, alpha) {
128
+ this.applyColor({
129
+ color: value,
130
+ property: "color",
131
+ variable: "--tw-text-opacity"
132
+ }, "textOpacity", target, alpha);
133
+ }
134
+ applyBackgroundColor(value, target, alpha) {
135
+ this.applyColor({
136
+ color: value,
137
+ property: "background-color",
138
+ variable: "--tw-bg-opacity"
139
+ }, "backgroundOpacity", target, alpha);
140
+ }
141
+ applyBorderColor(value, target, alpha) {
142
+ this.applyColor({
143
+ color: value,
144
+ property: "border-color",
145
+ variable: "--tw-border-opacity"
146
+ }, "borderOpacity", target, alpha);
147
+ }
148
+ applyColor(args, opacityPlugin, target, alpha) {
149
+ args = {
150
+ ...args,
151
+ color: this.parseRgbFormat(args.color)
152
+ };
153
+ Object.assign(target, this.corePlugins?.(opacityPlugin) ? this.withAlphaVariable(args) : { [args.property]: this.toColorValue(args.color, alpha) });
154
+ if (alpha != null && alpha !== "" && args.variable in target) {
155
+ const alphaValue = alpha.toString();
156
+ target[args.variable] = alphaValue;
157
+ }
158
+ }
159
+ toColorValue(maybeFunction, alpha) {
160
+ return typeof maybeFunction === "function" ? maybeFunction(alpha != null && alpha !== "" ? { opacityValue: alpha.toString() } : {}) : maybeFunction;
161
+ }
162
+ parseRgbFormat(color) {
163
+ return this.parseColorFormat(`rgb(${color} / <alpha-value>)`);
164
+ }
165
+ colorsToRgb(colors) {
166
+ for (const c in colors) {
167
+ if (c === "black" || c === "white" || !c.startsWith("c-") && /^(?:.+-)?(?:50|950|[1-9]00)$/.exec(c))
168
+ colors[c] = this.colorToRgb(colors[c]);
169
+ }
170
+ return colors;
171
+ }
172
+ colorToRgb(color) {
173
+ const parsed = this.parseColor(color, { loose: true });
174
+ if (parsed?.mode.startsWith("rgb")) {
175
+ return parsed.color.join(" ");
176
+ } else if (parsed?.mode.startsWith("hsl")) {
177
+ if (parsed.color.length === 3) {
178
+ const rh = ANGLE_REGEX.exec(parsed.color[0]);
179
+ const rs = PERCENT_REGEX.exec(parsed.color[1]);
180
+ const rl = PERCENT_REGEX.exec(parsed.color[2]);
181
+ if (rh && rs && rl) {
182
+ const h = parseFloat(rh[1]) / (rh[2] === "rad" ? 2 * Math.PI : rh[2] === "grad" ? 400 : rh[2] === "turn" ? 1 : 360);
183
+ const s = parseFloat(rs[1]) / 100;
184
+ const l = parseFloat(rl[1]) / 100;
185
+ return hslToRgb(h, s, l).join(" ");
186
+ }
187
+ }
188
+ }
189
+ console.warn(`Unsupported color format: ${color}`);
190
+ return color;
191
+ }
192
+ }
193
+
194
+ const BLOCK_SELECTOR_REGEX = /^([^{]+)\{([^}]+)\}$/;
195
+ function getDarkSelector(darkMode) {
196
+ let [mode, selector = ".dark"] = Array.isArray(darkMode) ? darkMode : [darkMode];
197
+ if (typeof selector === "function") {
198
+ selector = selector();
199
+ }
200
+ if (typeof selector === "string") {
201
+ selector = [selector];
202
+ }
203
+ if (mode === "variant") {
204
+ for (const s of selector) {
205
+ if (!s.startsWith("@") && !s.includes("&")) {
206
+ console.warn("Invalid `variant` selector");
207
+ return [];
208
+ }
209
+ }
210
+ } else if (selector.length !== 1) {
211
+ console.warn("Invalid `darkMode` selector");
212
+ return [];
213
+ }
214
+ switch (mode) {
215
+ case "media":
216
+ return [["@media (prefers-color-scheme: dark)"]];
217
+ case "class":
218
+ return [[`&:is(${selector[0]} *)`]];
219
+ case "selector":
220
+ return [[`&:where(${selector[0]}, ${selector[0]} *)`]];
221
+ case "variant": {
222
+ const ss = [];
223
+ for (const s of selector) {
224
+ const bsMatch = BLOCK_SELECTOR_REGEX.exec(s);
225
+ ss.push(bsMatch ? [bsMatch[1].trim(), bsMatch[2].trim()] : [s]);
226
+ }
227
+ return ss;
228
+ }
229
+ default:
230
+ return [selector];
231
+ }
232
+ }
233
+ function setNestedValue(obj, keys, value) {
234
+ if (keys.length === 0) {
235
+ return obj;
236
+ }
237
+ let curr = obj;
238
+ for (let i = 0; i < keys.length; i++) {
239
+ const K = keys[i];
240
+ if (i === keys.length - 1) {
241
+ curr[K] = value;
242
+ } else {
243
+ if (typeof curr[K] !== "object" || curr[K] === null) {
244
+ curr[K] = {};
245
+ }
246
+ curr = curr[K];
247
+ }
248
+ }
249
+ return obj;
250
+ }
251
+ function applyDark(selector, styles) {
252
+ const { "@dark": darkStyle, ...style } = styles;
253
+ if (darkStyle) {
254
+ for (const s of selector) {
255
+ setNestedValue(style, s, darkStyle);
256
+ }
257
+ }
258
+ return style;
259
+ }
260
+
261
+ function keyValuePairs(keys, val = (k) => k) {
262
+ return keys.reduce((acc, k) => {
263
+ acc[k] = val(k);
264
+ return acc;
265
+ }, {});
266
+ }
267
+ function WindUITailwindCSS(config = {}) {
268
+ const wiu = create(config);
269
+ return createPlugin((tw) => {
270
+ const tp = createTailwindTheme(tw);
271
+ const gt = wiu.generator(tp);
272
+ const darkMode = tw.config("darkMode", "media");
273
+ const darkSelector = getDarkSelector(darkMode);
274
+ tw.addBase({ ":root": gt.colors.rootVars() });
275
+ tw.addBase({ ":root": gt.sizes.rootVars() });
276
+ tw.addBase({ "*": applyDark(darkSelector, gt.variants.rootVars()) });
277
+ tw.matchUtilities({
278
+ c: (val) => gt.colors.cssVars(val)
279
+ }, { values: keyValuePairs(gt.colors.names()) });
280
+ tw.matchUtilities({
281
+ v: (val) => applyDark(darkSelector, gt.variants.cssVars(val))
282
+ }, { values: keyValuePairs(gt.variants.names()) });
283
+ tw.matchUtilities({
284
+ "bg": (val, ext) => {
285
+ return gt.variants.utilCss("bg", val, ext.modifier);
286
+ },
287
+ "border": (val, ext) => {
288
+ return gt.variants.utilCss("border", val, ext.modifier);
289
+ },
290
+ "text": (val, ext) => {
291
+ return gt.variants.utilCss("text", val, ext.modifier);
292
+ }
293
+ }, {
294
+ values: { v: "default", "v-soft": "soft", "v-muted": "muted", "v-accent": "accent" },
295
+ modifiers: tw.theme("opacity")
296
+ });
297
+ tw.matchUtilities({
298
+ s: (val) => gt.sizes.cssVars(val)
299
+ }, {
300
+ values: keyValuePairs(gt.sizes.names())
301
+ });
302
+ tw.addComponents([...gt.components.css()]);
303
+ }, {
304
+ content: [],
305
+ theme: {
306
+ extend: {
307
+ colors({ colors }) {
308
+ return {
309
+ c: keyValuePairs(Object.keys(colors.gray), (v) => {
310
+ const c = wiu.vars.color(v);
311
+ return TailwindVer === 4 ? c : `rgb(${c} / <alpha-value>)`;
312
+ }),
313
+ // TODO: add as configurable option
314
+ default: colors.gray,
315
+ accent: colors.blue
316
+ };
317
+ }
318
+ }
319
+ }
320
+ });
321
+ }
322
+
323
+ export { WindUITailwindCSS as default };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@windi-ui/tailwind",
3
+ "version": "0.7.1",
4
+ "license": "MIT",
5
+ "author": "Waleed Khaled <https://github.com/Waleed-KH>",
6
+ "description": "Modern, modular, and customizable UI component library",
7
+ "keywords": [
8
+ "ui",
9
+ "component",
10
+ "tailwindcss"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/windi-ui/windi-ui.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/windi-ui/windi-ui/issues"
18
+ },
19
+ "type": "module",
20
+ "module": "dist/index.mjs",
21
+ "main": "dist/index.cjs",
22
+ "types": "dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "import": "./dist/index.mjs",
26
+ "require": "./dist/index.cjs",
27
+ "types": "./dist/index.d.ts"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "dependencies": {
34
+ "@windi-ui/core": "0.7.1"
35
+ },
36
+ "peerDependencies": {
37
+ "tailwindcss": ">=3.3.0"
38
+ },
39
+ "devDependencies": {
40
+ "tailwindcss": "^3.4.17"
41
+ },
42
+ "scripts": {
43
+ "build": "unbuild",
44
+ "stub": "unbuild --stub"
45
+ }
46
+ }