@windi-ui/core 0.8.0-beta.1 → 0.8.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/dist/index.cjs CHANGED
@@ -1,20 +1,30 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_nuxt_ui = require("./nuxt-ui-CvNwRMiY.cjs");
2
3
  const require_badge = require("./badge-DP_7SF0i.cjs");
3
4
  // src/config.ts
5
+ const PRESETS = { "nuxt-ui": require_nuxt_ui.nuxtUi };
4
6
  function resolveConfig(config) {
5
- const { prefix = "wui", rootSelector = ":root", variantScope = "element", darkSelector, build, safelist = false, debug = false } = config;
7
+ const { prefix = "wui", rootSelector = ":root", variantScope = "element", overrideMode = "vars", darkSelector, presets = [], build, safelist = false, debug = false } = config;
6
8
  return {
7
9
  prefix,
8
10
  rootSelector,
9
11
  default: config.default,
10
12
  accent: config.accent,
13
+ colors: config.colors,
11
14
  variantScope,
15
+ overrides: config.overrides,
16
+ overrideMode,
17
+ variantUtilities: config.variantUtilities,
12
18
  darkSelector,
19
+ presets: presets.map((p) => typeof p !== "string" ? p : Object.hasOwn(PRESETS, p) ? PRESETS[p]() : unknownPreset(p)),
13
20
  build,
14
21
  safelist,
15
22
  debug
16
23
  };
17
24
  }
25
+ function unknownPreset(name) {
26
+ throw new Error(`WindiUI: unknown preset "${name}". The presets are: ${Object.keys(PRESETS).join(", ")}.`);
27
+ }
18
28
  /** Types a config object, so that one object can be shared by the plugin, a config file and other tools. */
19
29
  function defineConfig(config) {
20
30
  return config;
@@ -43,8 +53,11 @@ var BuilderBase = class {
43
53
  console.error(`"${name}" already exists.`);
44
54
  return;
45
55
  }
46
- const elem = builder(this.ctx);
47
- this.items.set(name, elem);
56
+ this.set(name, builder);
57
+ }
58
+ /** Adds an item, or replaces the one of that name, `default` included. */
59
+ set(name, builder) {
60
+ this.items.set(name, builder(this.ctx));
48
61
  }
49
62
  update(name, builder) {
50
63
  const elem = this.items.get(name);
@@ -85,7 +98,252 @@ var VarsGeneratorBase = class extends BuilderBase {
85
98
  return this.cssVars("default");
86
99
  }
87
100
  };
101
+ // src/colors.ts
102
+ const COLOR_SHADES = [
103
+ "50",
104
+ "100",
105
+ "200",
106
+ "300",
107
+ "400",
108
+ "500",
109
+ "600",
110
+ "700",
111
+ "800",
112
+ "900",
113
+ "950"
114
+ ];
115
+ const COLOR_SHADE_REGEX = /50|950|[1-9]00/;
116
+ const COLOR_SHADE_EX_REGEX = new RegExp(`^((?:(default|accent)-)?(${COLOR_SHADE_REGEX.source})|black|white)$`);
117
+ /** The theme method that paints each main property of `apply-v`, `btn` and `variantUtilities`. */
118
+ const APPLY = {
119
+ bg: "applyBackgroundColor",
120
+ border: "applyBorderColor",
121
+ text: "applyTextColor"
122
+ };
123
+ /** The variant variable that each main property of `apply-v`, `btn` and `variantUtilities` reads. */
124
+ const VAR_PROP = {
125
+ bg: "bg",
126
+ border: "border",
127
+ text: "fg"
128
+ };
129
+ const COLOR_PALETTE_REGEX = new RegExp(`^(.+)-(${COLOR_SHADE_REGEX.source})$`);
130
+ const FLAT_COLORS = {
131
+ black: "#000",
132
+ white: "#fff"
133
+ };
134
+ function getColorMap(colors, debug) {
135
+ const cMap = {
136
+ palette: /* @__PURE__ */ new Map(),
137
+ custom: /* @__PURE__ */ new Map()
138
+ };
139
+ for (const key in colors) {
140
+ const value = colors[key];
141
+ const mRes = COLOR_PALETTE_REGEX.exec(key);
142
+ if (mRes) {
143
+ const cName = mRes[1];
144
+ if (cName === "c") continue;
145
+ if (!cMap.palette.has(cName)) cMap.palette.set(cName, /* @__PURE__ */ new Map());
146
+ cMap.palette.get(cName).set(mRes[2], value);
147
+ } else cMap.custom.set(key, value);
148
+ }
149
+ for (const [cName, shades] of cMap.palette) if (shades.size !== 11) {
150
+ if (debug) console.warn(`Color palette "${cName}" does not have all 11 shades (50, 100, ..., 950).`);
151
+ cMap.palette.delete(cName);
152
+ for (const [sKey, sVal] of shades) cMap.custom.set(`${cName}-${sKey}`, sVal);
153
+ }
154
+ return cMap;
155
+ }
156
+ var ColorsGenerator = class extends VarsGeneratorBase {
157
+ options;
158
+ variants;
159
+ colors;
160
+ items;
161
+ /** The colors with a `c-*` class: `colors.only` and those of the builder. Left out, all of them. */
162
+ only;
163
+ /** @param variants gives the override variables of each `c-*` class */
164
+ constructor(ctx, options, variants) {
165
+ super(ctx);
166
+ this.options = options;
167
+ this.variants = variants;
168
+ this.colors = getColorMap(this.theme.colors(), options.debug);
169
+ this.items = this.colors.palette;
170
+ const only = options.colors?.only;
171
+ if (only) {
172
+ this.only = new Set(only);
173
+ variants.hasColor = (c) => this.only.has(c) || c === "default" || c === "accent";
174
+ }
175
+ for (const name of ["default", "accent"]) {
176
+ const value = options[name];
177
+ if (value == null) continue;
178
+ const palette = this.resolve(value);
179
+ if (!palette) throw new Error(`WindiUI: the \`${name}\` option does not give a palette.`);
180
+ this.items.set(name, palette);
181
+ this.alias(name, value);
182
+ }
183
+ }
184
+ /** The 11 shades of a palette given by name, by CSS variables or as the shades themselves. */
185
+ resolve(value) {
186
+ if (typeof value === "string") {
187
+ const palette = this.items.get(value);
188
+ if (!palette) console.error(`Color "${value}" not found.`);
189
+ return palette && new Map(palette);
190
+ }
191
+ if ("vars" in value) return new Map(COLOR_SHADES.map((s) => [s, `var(${value.vars}-${s})`]));
192
+ const missing = COLOR_SHADES.filter((s) => !value[s]);
193
+ if (missing.length) {
194
+ console.error(`A color needs all 11 shades; ${missing.join(", ")} ${missing.length > 1 ? "are" : "is"} missing.`);
195
+ return;
196
+ }
197
+ return new Map(COLOR_SHADES.map((s) => [s, this.theme.toColor?.(value[s]) ?? value[s]]));
198
+ }
199
+ addColor(name, value) {
200
+ if (this.items.has(name)) {
201
+ console.error(`"${name}" already exists.`);
202
+ return;
203
+ }
204
+ this.setColor(name, value);
205
+ }
206
+ /** Adds a palette, or replaces the one of that name. */
207
+ setColor(name, value) {
208
+ const palette = this.resolve(value);
209
+ if (palette) {
210
+ this.items.set(name, palette);
211
+ this.alias(name, value);
212
+ this.only?.add(name);
213
+ }
214
+ }
215
+ remove(name) {
216
+ super.remove(name);
217
+ if (!this.items.has(name)) this.variants.alias(name);
218
+ }
219
+ alias(name, value) {
220
+ this.variants.alias(name, typeof value === "object" && "vars" in value && value.overrideVars ? {
221
+ ...value.overrideVars,
222
+ vars: value.vars
223
+ } : void 0);
224
+ }
225
+ /** @param all also the palettes that `colors.only` leaves without a class */
226
+ names(all = false) {
227
+ const cNames = super.names().filter((c) => all || (this.only?.has(c) ?? true));
228
+ cNames.push(...cNames.map((c) => `accent-${c}`));
229
+ return cNames;
230
+ }
231
+ /** The names of `colors.only` that are not palettes. */
232
+ unknownOnly() {
233
+ return (this.options.colors?.only ?? []).filter((c) => !this.items.has(c));
234
+ }
235
+ cssVars(name) {
236
+ let palette = this.items.get(name);
237
+ let accentVars = false;
238
+ if (!palette) {
239
+ if (name.startsWith("accent-")) {
240
+ name = name.replace(/^accent-/, "");
241
+ palette = this.items.get(name);
242
+ if (palette) accentVars = true;
243
+ else {
244
+ console.error(`Color "${name}" not found.`);
245
+ return {};
246
+ }
247
+ } else {
248
+ console.error(`Color "${name}" not found.`);
249
+ return {};
250
+ }
251
+ }
252
+ return {
253
+ ...cssVars(Object.fromEntries(palette.entries()), (n) => this.vars.c(accentVars ? `accent-${n}` : n)),
254
+ ...this.variants.colorVars(name, accentVars)
255
+ };
256
+ }
257
+ rootVars() {
258
+ const cRoot = {};
259
+ for (const mn in FLAT_COLORS) cRoot[this.vars.c(mn)] = this.colors.custom.get(mn) ?? FLAT_COLORS[mn];
260
+ const dc = super.rootVars();
261
+ Object.assign(cRoot, dc);
262
+ const { "@dark": accentDark, ...accent } = this.variants.colorVars("accent", true);
263
+ Object.assign(cRoot, accent);
264
+ if (accentDark) cRoot["@dark"] = {
265
+ ...cRoot["@dark"],
266
+ ...accentDark
267
+ };
268
+ const addPalette = (suffix, palette) => {
269
+ for (const [shade, color] of palette) cRoot[this.vars.c(`${suffix}-${shade}`)] = color;
270
+ };
271
+ addPalette("default", this.items.get("default") ?? /* @__PURE__ */ new Map());
272
+ addPalette("accent", this.items.get("accent") ?? /* @__PURE__ */ new Map());
273
+ return cRoot;
274
+ }
275
+ /**
276
+ * `bg-{color}` and the like of `variantUtilities`: the value of `variant` for this color, overrides included, with
277
+ * its opacity unless `alpha` is given, light and dark. A shade is this palette's, not the element's `c-*` class.
278
+ */
279
+ variantUtilCss(color, prop, variant, alpha) {
280
+ const palette = this.items.get(color);
281
+ const values = palette && this.variants.colorValues(variant, color);
282
+ if (!values) return {};
283
+ const key = VAR_PROP[prop];
284
+ const shade = new RegExp(`var\\(${this.vars.c("")}(${COLOR_SHADE_REGEX.source})\\)`, "g");
285
+ const paint = (v) => {
286
+ const target = {};
287
+ const value = v[key].replace(shade, (read, s) => palette.get(s) ?? read);
288
+ this.theme[APPLY[prop]](value, target, alpha ?? v[`${key}-opacity`]);
289
+ return target;
290
+ };
291
+ const { light, dark } = values;
292
+ const css = paint(light);
293
+ if (key in dark || `${key}-opacity` in dark) css["@dark"] = paint({
294
+ ...light,
295
+ ...dark
296
+ });
297
+ return css;
298
+ }
299
+ toValue(name, alpha) {
300
+ return this.theme.colorValue(COLOR_SHADE_EX_REGEX.exec(name) ? this.vars.color(name) : name, alpha);
301
+ }
302
+ };
88
303
  // src/variants.ts
304
+ const CALIBRATED = [
305
+ "bg",
306
+ "fg",
307
+ "border",
308
+ "bg-:hover",
309
+ "fg-:hover",
310
+ "border-:hover"
311
+ ];
312
+ const ACCENT_KEY = /^(bg|border|fg)-accent(-:|$)/;
313
+ const APPLY_PROPS = [
314
+ "bg",
315
+ "border",
316
+ "text"
317
+ ].flatMap((p) => [
318
+ p,
319
+ `${p}-soft`,
320
+ `${p}-muted`,
321
+ `${p}-accent`
322
+ ]);
323
+ /** The states `apply-v-[...]` takes, by name without the colon. */
324
+ const APPLY_STATES = [
325
+ ":hover",
326
+ ":focus",
327
+ ":focus-visible",
328
+ ":focus-within",
329
+ ":active",
330
+ ":visited",
331
+ ":target",
332
+ ":checked",
333
+ ":indeterminate",
334
+ ":disabled",
335
+ ":enabled",
336
+ ":invalid",
337
+ ":valid",
338
+ ":user-invalid",
339
+ ":user-valid",
340
+ ":required",
341
+ ":optional",
342
+ ":read-only",
343
+ ":placeholder-shown",
344
+ ":autofill",
345
+ ":open"
346
+ ];
89
347
  function defaultVariant(vars) {
90
348
  return {
91
349
  "bg": vars.color("white"),
@@ -262,32 +520,57 @@ function mainVariants(vars) {
262
520
  }
263
521
  };
264
522
  }
265
- function getCssVars(vars, props) {
523
+ /** @param fill whether a state refers to the base value of each property it leaves out */
524
+ function flatten(vars, props, fill = true) {
266
525
  const { pseudos: vPseudos, ...vProps } = props;
267
526
  const cssObj = vProps;
268
527
  if (vPseudos) {
269
- const propKeys = new Set(Object.keys(vProps));
528
+ const propKeys = fill ? Object.keys(vProps) : [];
270
529
  for (const vpk in vPseudos) {
271
530
  const vp = vPseudos[vpk];
272
531
  if (vp) {
273
- const vpPropKeys = new Set(Object.keys(vp)).union(propKeys);
274
- for (const vppk of vpPropKeys) cssObj[`${vppk}-${vpk}`] = vppk in vp ? vp[vppk] : vProps[vppk];
532
+ const vpPropKeys = /* @__PURE__ */ new Set([...Object.keys(vp), ...propKeys]);
533
+ for (const vppk of vpPropKeys) cssObj[`${vppk}-${vpk}`] = vp[vppk] ?? vars.variant(vppk);
275
534
  }
276
535
  }
277
536
  }
278
- return cssVars(cssObj, (n) => vars.v(n));
537
+ return cssObj;
538
+ }
539
+ function getCssVars(vars, props) {
540
+ return cssVars(flatten(vars, props), (n) => vars.v(n));
541
+ }
542
+ /** An override value: a shade of the color, of the accent color for an `-accent` property, or the value as written. */
543
+ function shade(vars, key, value) {
544
+ if (key.includes("opacity") || !COLOR_SHADE_EX_REGEX.test(value)) return value;
545
+ return ACCENT_KEY.test(key) && /^\d/.test(value) ? vars.color.accent(value) : vars.color(value);
546
+ }
547
+ /** Merges `source` into `target` in depth; `under` keeps the values `target` has. */
548
+ function merge(target, source, under) {
549
+ for (const [key, value] of Object.entries(source)) if (value && typeof value === "object") merge(target[key] ??= {}, value, under);
550
+ else if (!under || !(key in target)) target[key] = value;
279
551
  }
280
552
  /** The light values of the built-in default variant, by CSS variable name. */
281
553
  function defaultVariantVars(vars) {
282
554
  const { dark: _, ...props } = defaultVariant(vars);
283
- return getCssVars(vars, props);
555
+ const light = getCssVars(vars, props);
556
+ for (const [name, value] of Object.entries(light)) {
557
+ const base = /^var\((--[^,()\s]+)\)$/.exec(value)?.[1];
558
+ if (base && base in light) light[name] = `var(${base}, ${light[base]})`;
559
+ }
560
+ return light;
284
561
  }
285
562
  var VariantsGenerator = class extends VarsGeneratorBase {
286
563
  scope;
564
+ mode;
287
565
  items;
288
- constructor(ctx, scope = "universal") {
566
+ /** Palettes given by CSS variables that also take their overrides from variables. */
567
+ aliases = /* @__PURE__ */ new Map();
568
+ /** Whether a color is applied anywhere; the overrides of the others are left out. Set by `colors.only`. */
569
+ hasColor = (_color) => true;
570
+ constructor(ctx, scope = "universal", mode = "vars") {
289
571
  super(ctx);
290
572
  this.scope = scope;
573
+ this.mode = mode;
291
574
  this.items = new Map([["default", defaultVariant(this.vars)], ...Object.entries(mainVariants(this.vars))]);
292
575
  }
293
576
  cssVars(name) {
@@ -296,10 +579,169 @@ var VariantsGenerator = class extends VarsGeneratorBase {
296
579
  console.error(`Variant "${name}" not found.`);
297
580
  return {};
298
581
  }
299
- const { dark: vDark, ...vProps } = variant;
300
- const cssVars = getCssVars(this.vars, vProps);
301
- if (vDark) cssVars["@dark"] = getCssVars(this.vars, vDark);
302
- return cssVars;
582
+ const { values: { light, dark }, statics, entries, keys } = this.resolved(name);
583
+ const compound = {};
584
+ if (this.mode !== "vars") for (const [color, own] of statics) {
585
+ const entry = entries.get(color);
586
+ for (const accent of [false, true]) {
587
+ const block = {};
588
+ const blockDark = {};
589
+ for (const k of Object.keys({
590
+ ...own.light,
591
+ ...own.dark
592
+ })) {
593
+ if (ACCENT_KEY.test(k) !== accent) continue;
594
+ if (k in own.light) block[k] = entry.light[k];
595
+ if (k in entry.dark) blockDark[k] = entry.dark[k];
596
+ else if (k in own.light && k in dark) blockDark[k] = dark[k];
597
+ }
598
+ if (Object.keys(block).length || Object.keys(blockDark).length) compound[`&.c-${accent ? "accent-" : ""}${color}`] = {
599
+ ...cssVars(block, (n) => this.vars.v(n)),
600
+ ...Object.keys(blockDark).length ? { "@dark": cssVars(blockDark, (n) => this.vars.v(n)) } : {}
601
+ };
602
+ }
603
+ }
604
+ for (const k of keys) {
605
+ const cv = cssEsc(this.vars.cv(`${name}-${k}`));
606
+ light[k] = `var(${cv}, ${light[k]})`;
607
+ if (k in dark) dark[k] = `var(${cv}, ${dark[k]})`;
608
+ }
609
+ const css = cssVars(light, (n) => this.vars.v(n));
610
+ if (variant.dark || Object.keys(dark).length) css["@dark"] = cssVars(dark, (n) => this.vars.v(n));
611
+ return Object.assign(css, compound);
612
+ }
613
+ /**
614
+ * Sets values of a variant for some colors, over those they have.
615
+ * @param under keeps the values set before, so that a preset or a calibration never replaces an explicit override
616
+ */
617
+ override(name, colors, override, under = false) {
618
+ const variant = this.items.get(name);
619
+ if (!variant) {
620
+ console.error(`"${name}" not found.`);
621
+ return;
622
+ }
623
+ const value = typeof override === "function" ? override(this.ctx) : override;
624
+ const byColor = {};
625
+ for (const [key, own] of Object.entries(variant.colors ?? {})) for (const color of key.split(",")) merge(byColor[color.trim()] ??= {}, own, false);
626
+ for (const color of [colors].flat().flatMap((c) => c.split(","))) merge(byColor[color.trim()] ??= {}, value, under);
627
+ this.items.set(name, {
628
+ ...variant,
629
+ colors: byColor
630
+ });
631
+ }
632
+ /** The colors that overrides name. */
633
+ overrideColors() {
634
+ return [...new Set([...this.items.values()].flatMap((v) => Object.keys(v.colors ?? {}).flatMap((k) => k.split(",").map((c) => c.trim()))))];
635
+ }
636
+ /** Makes the palette `color` take its overrides from `{vars}-{variant}-{prop}` variables, or with no `value` stops that. */
637
+ alias(color, value) {
638
+ if (value) this.aliases.set(color, value);
639
+ else this.aliases.delete(color);
640
+ }
641
+ /**
642
+ * The override variables a `c-*` class sets: the values of `color`, and `initial` for the others, so that a color
643
+ * inside another one is not overridden by it. In dark mode it resets a light override where the variant has a dark
644
+ * value of its own. With `accent`, those of the `-accent` properties, for `c-accent-*`.
645
+ */
646
+ colorVars(color, accent = false) {
647
+ const light = {};
648
+ const dark = {};
649
+ for (const name of this.items.keys()) {
650
+ const { values, varEntries, keys } = this.resolved(name);
651
+ const entry = varEntries.get(color);
652
+ for (const k of keys) {
653
+ if (ACCENT_KEY.test(k) !== accent) continue;
654
+ const cv = cssEsc(this.vars.cv(`${name}-${k}`));
655
+ light[cv] = entry?.light[k] ?? "initial";
656
+ if (entry && k in entry.dark) dark[cv] = entry.dark[k];
657
+ else if (entry && k in entry.light && k in values.dark) dark[cv] = "initial";
658
+ }
659
+ }
660
+ return Object.keys(dark).length ? {
661
+ ...light,
662
+ "@dark": dark
663
+ } : light;
664
+ }
665
+ values(name) {
666
+ const { dark = {}, colors: _, ...props } = this.items.get(name);
667
+ return {
668
+ light: flatten(this.vars, props),
669
+ dark: flatten(this.vars, dark)
670
+ };
671
+ }
672
+ /**
673
+ * The values of a variant, and its overrides by color: `statics` as configured, `entries` with what a palette given
674
+ * by variables forwards. `keys` get an override variable, which the colors of `varEntries` set.
675
+ */
676
+ resolved(name) {
677
+ const values = this.values(name);
678
+ const statics = /* @__PURE__ */ new Map();
679
+ for (const [key, { dark = {}, ...light }] of Object.entries(this.items.get(name).colors ?? {})) for (const color of key.split(",").map((c) => c.trim()).filter(this.hasColor)) {
680
+ const flat = statics.get(color) ?? {
681
+ light: {},
682
+ dark: {}
683
+ };
684
+ for (const [k, v] of Object.entries(flatten(this.vars, light, false))) flat.light[k] = shade(this.vars, k, v);
685
+ for (const [k, v] of Object.entries(flatten(this.vars, dark, false))) flat.dark[k] = shade(this.vars, k, v);
686
+ statics.set(color, flat);
687
+ }
688
+ const forwarded = /* @__PURE__ */ new Map();
689
+ for (const [color, { variants, props = "calibrated" }] of this.aliases) {
690
+ if (variants && !variants.includes(name)) continue;
691
+ forwarded.set(color, props === "all" ? Object.keys({
692
+ ...values.light,
693
+ ...values.dark
694
+ }) : props === "calibrated" ? CALIBRATED : props);
695
+ }
696
+ const staticKeys = [...statics.values()].flatMap((f) => [...Object.keys(f.light), ...Object.keys(f.dark)]);
697
+ const forwardedKeys = [...forwarded.values()].flat();
698
+ const def = this.values("default");
699
+ for (const k of [...staticKeys, ...forwardedKeys]) {
700
+ if (k in values.light) continue;
701
+ values.light[k] = def.light[k] ?? this.vars.variant(k.replace(/-:.*$/, ""));
702
+ if (k in def.dark && !(k in values.dark)) values.dark[k] = def.dark[k];
703
+ }
704
+ const entries = new Map(statics);
705
+ const aliasEntries = /* @__PURE__ */ new Map();
706
+ for (const [color, keys] of forwarded) {
707
+ const { vars } = this.aliases.get(color);
708
+ const own = statics.get(color);
709
+ const entry = {
710
+ light: {},
711
+ dark: {}
712
+ };
713
+ for (const k of keys) {
714
+ const runtime = `var(${vars}-${name}-${k.replace(/:/g, "")}`;
715
+ entry.light[k] = `${runtime}${own?.light[k] ? `, ${own.light[k]}` : ""})`;
716
+ if (own?.dark[k]) entry.dark[k] = `${runtime}, ${own.dark[k]})`;
717
+ else if (k in values.dark) entry.dark[k] = `${runtime})`;
718
+ }
719
+ aliasEntries.set(color, entry);
720
+ entries.set(color, {
721
+ light: {
722
+ ...own?.light,
723
+ ...entry.light
724
+ },
725
+ dark: {
726
+ ...own?.dark,
727
+ ...entry.dark
728
+ }
729
+ });
730
+ }
731
+ if (this.mode === "compound") return {
732
+ values,
733
+ statics,
734
+ entries,
735
+ varEntries: aliasEntries,
736
+ keys: new Set(forwardedKeys)
737
+ };
738
+ return {
739
+ values,
740
+ statics,
741
+ entries,
742
+ varEntries: entries,
743
+ keys: /* @__PURE__ */ new Set([...staticKeys, ...forwardedKeys])
744
+ };
303
745
  }
304
746
  /** Every variable a variant can set, as `initial`: on a child it stops the parent's variant from inheriting. */
305
747
  resetVars() {
@@ -309,7 +751,7 @@ var VariantsGenerator = class extends VarsGeneratorBase {
309
751
  for (const v of Object.keys({
310
752
  ...light,
311
753
  ...dark
312
- })) reset[v] = "initial";
754
+ })) if (v.startsWith("--")) reset[v] = "initial";
313
755
  }
314
756
  return reset;
315
757
  }
@@ -317,6 +759,43 @@ var VariantsGenerator = class extends VarsGeneratorBase {
317
759
  return this.withFallback(this.vApplyProp(v, prop, {}, void 0, alpha));
318
760
  }
319
761
  /**
762
+ * The CSS of `apply-v`: what `btn` gets, `bg`, `border` and `text` with `:hover`. `list` names props and states
763
+ * instead, such as `bg,text,focus-visible`: the props are applied at rest and in each state. Without props it is
764
+ * all three, without states `:hover`. A name it does not know gives `undefined`.
765
+ */
766
+ applyCss(list) {
767
+ const tokens = list ? list.split(",").map((t) => t.trim()) : [];
768
+ const props = tokens.filter((t) => APPLY_PROPS.includes(t));
769
+ const states = tokens.map((t) => `:${t}`).filter((s) => APPLY_STATES.includes(s));
770
+ if (props.length + states.length !== tokens.length) return;
771
+ const v = props.length ? props : true;
772
+ return this.withFallback(this.apply(v, Object.fromEntries((states.length ? states : [":hover"]).map((s) => [s, v]))));
773
+ }
774
+ /**
775
+ * The values of a variant on an element of `color`, its overrides applied as the `c-*` and `v-*` classes would
776
+ * resolve them. `dark` holds the values that change in dark mode.
777
+ */
778
+ colorValues(name, color) {
779
+ if (!this.items.has(name)) {
780
+ console.error(`Variant "${name}" not found.`);
781
+ return;
782
+ }
783
+ const { values, entries } = this.resolved(name);
784
+ const entry = entries.get(color);
785
+ const or = (value, base) => {
786
+ const read = /^var\((--[^,()\s]+)\)$/.exec(value)?.[1];
787
+ return read && !read.startsWith(this.vars.c("")) ? `var(${read}, ${base})` : value;
788
+ };
789
+ const light = { ...values.light };
790
+ const dark = { ...values.dark };
791
+ for (const [k, v] of Object.entries(entry?.light ?? {})) light[k] = or(v, values.light[k]);
792
+ for (const [k, v] of Object.entries(entry?.dark ?? {})) dark[k] = or(v, values.dark[k] ?? values.light[k]);
793
+ return {
794
+ light,
795
+ dark
796
+ };
797
+ }
798
+ /**
320
799
  * The two switches a fallback picks its light or dark value with: `var(on-light, <light>) var(on-dark, <dark>)`.
321
800
  * The one of the current mode is not set, so its fallback is used, and the other one is empty.
322
801
  * They inherit, so a pseudo-element is in the mode of its element, which a dark selector nested in its rule cannot tell.
@@ -378,21 +857,14 @@ var VariantsGenerator = class extends VarsGeneratorBase {
378
857
  return target;
379
858
  }
380
859
  vApplyProp(mProp, sProp, target = {}, pseudo, alpha) {
381
- const mvp = mProp === "text" ? "fg" : mProp;
860
+ const mvp = VAR_PROP[mProp];
382
861
  const vp = sProp && sProp !== "default" ? `${mvp}-${sProp}` : mvp;
383
862
  const aVar = pseudo ? `${mvp}-opacity-${pseudo}` : `${mvp}-opacity`;
384
- const vVal = this.vars.variant(pseudo ? `${vp}-${pseudo}` : vp);
863
+ const key = pseudo ? `${vp}-${pseudo}` : vp;
864
+ const vVal = this.vars.variant(key, !pseudo || key in this.values("default").light ? void 0 : this.vars.variant(vp));
385
865
  const one = this.scope === "universal" ? "1" : void 0;
386
866
  const vAlpha = this.vars.variant(aVar, pseudo ? this.vars.variant(`${mvp}-opacity`, one) : one);
387
- switch (mProp) {
388
- case "bg":
389
- this.theme.applyBackgroundColor(vVal, target, vAlpha);
390
- break;
391
- case "border":
392
- this.theme.applyBorderColor(vVal, target, vAlpha);
393
- break;
394
- case "text": this.theme.applyTextColor(vVal, target, vAlpha);
395
- }
867
+ this.theme[APPLY[mProp]](vVal, target, vAlpha);
396
868
  if (alpha) {
397
869
  target[this.vars.v(aVar)] = `${alpha} !important`;
398
870
  if (this.scope === "element") target[":where(&) > *"] = { [this.vars.v(aVar)]: "initial" };
@@ -411,6 +883,7 @@ function varsProvider(prefix) {
411
883
  const c = (v) => vName(`c-${v}`);
412
884
  const v = (v) => vName(`v-${v}`);
413
885
  const s = (v) => vName(`s-${v}`);
886
+ const cv = (v) => vName(`cv-${v}`);
414
887
  function color(name, fallback) {
415
888
  return varFunc(c(name), fallback);
416
889
  }
@@ -420,6 +893,7 @@ function varsProvider(prefix) {
420
893
  c,
421
894
  v,
422
895
  s,
896
+ cv,
423
897
  color,
424
898
  variant(name, fallback) {
425
899
  if (typeof fallback === "object") fallback = defaultVariantVars(vars)[cssEsc(v(name))];
@@ -434,136 +908,11 @@ function varsProvider(prefix) {
434
908
  function cssVars(obj, keyMap, vars = {}) {
435
909
  for (const p in obj) {
436
910
  const val = obj[p];
437
- if (typeof val === "string") vars[cssEsc(keyMap(p))] = val;
911
+ if (typeof val === "string" || typeof val === "number") vars[cssEsc(keyMap(p))] = String(val);
438
912
  else cssVars(val, (cp) => `${keyMap(p)}-${cp}`, vars);
439
913
  }
440
914
  return vars;
441
915
  }
442
- // src/colors.ts
443
- const COLOR_SHADES = [
444
- "50",
445
- "100",
446
- "200",
447
- "300",
448
- "400",
449
- "500",
450
- "600",
451
- "700",
452
- "800",
453
- "900",
454
- "950"
455
- ];
456
- const COLOR_SHADE_REGEX = /50|950|[1-9]00/;
457
- const COLOR_SHADE_EX_REGEX = new RegExp(`^((?:(default|accent)-)?(${COLOR_SHADE_REGEX.source})|black|white)$`);
458
- const COLOR_PALETTE_REGEX = new RegExp(`^(.+)-(${COLOR_SHADE_REGEX.source})$`);
459
- const FLAT_COLORS = {
460
- black: "#000",
461
- white: "#fff"
462
- };
463
- function getColorMap(colors, debug) {
464
- const cMap = {
465
- palette: /* @__PURE__ */ new Map(),
466
- custom: /* @__PURE__ */ new Map()
467
- };
468
- for (const key in colors) {
469
- const value = colors[key];
470
- const mRes = COLOR_PALETTE_REGEX.exec(key);
471
- if (mRes) {
472
- const cName = mRes[1];
473
- if (cName === "c") continue;
474
- if (!cMap.palette.has(cName)) cMap.palette.set(cName, /* @__PURE__ */ new Map());
475
- cMap.palette.get(cName).set(mRes[2], value);
476
- } else cMap.custom.set(key, value);
477
- }
478
- for (const [cName, shades] of cMap.palette) if (shades.size !== 11) {
479
- if (debug) console.warn(`Color palette "${cName}" does not have all 11 shades (50, 100, ..., 950).`);
480
- cMap.palette.delete(cName);
481
- for (const [sKey, sVal] of shades) cMap.custom.set(`${cName}-${sKey}`, sVal);
482
- }
483
- return cMap;
484
- }
485
- var ColorsGenerator = class extends VarsGeneratorBase {
486
- colors;
487
- items;
488
- constructor(ctx, options = {}) {
489
- super(ctx);
490
- this.colors = getColorMap(this.theme.colors(), options.debug);
491
- this.items = this.colors.palette;
492
- for (const name of ["default", "accent"]) {
493
- const value = options[name];
494
- if (value == null) continue;
495
- const palette = this.resolve(value);
496
- if (!palette) throw new Error(`WindiUI: the \`${name}\` option does not give a palette.`);
497
- this.items.set(name, palette);
498
- }
499
- }
500
- /** The 11 shades of a palette given by name, by CSS variables or as the shades themselves. */
501
- resolve(value) {
502
- if (typeof value === "string") {
503
- const palette = this.items.get(value);
504
- if (!palette) console.error(`Color "${value}" not found.`);
505
- return palette && new Map(palette);
506
- }
507
- if ("vars" in value) return new Map(COLOR_SHADES.map((s) => [s, `var(${value.vars}-${s})`]));
508
- const missing = COLOR_SHADES.filter((s) => !value[s]);
509
- if (missing.length) {
510
- console.error(`A color needs all 11 shades; ${missing.join(", ")} ${missing.length > 1 ? "are" : "is"} missing.`);
511
- return;
512
- }
513
- return new Map(COLOR_SHADES.map((s) => [s, this.theme.toColor?.(value[s]) ?? value[s]]));
514
- }
515
- addColor(name, value) {
516
- if (this.items.has(name)) {
517
- console.error(`"${name}" already exists.`);
518
- return;
519
- }
520
- this.setColor(name, value);
521
- }
522
- /** Adds a palette, or replaces the one of that name. */
523
- setColor(name, value) {
524
- const palette = this.resolve(value);
525
- if (palette) this.items.set(name, palette);
526
- }
527
- names() {
528
- const cNames = super.names();
529
- cNames.push(...cNames.map((c) => `accent-${c}`));
530
- return cNames;
531
- }
532
- cssVars(name) {
533
- let palette = this.items.get(name);
534
- let accentVars = false;
535
- if (!palette) {
536
- if (name.startsWith("accent-")) {
537
- name = name.replace(/^accent-/, "");
538
- palette = this.items.get(name);
539
- if (palette) accentVars = true;
540
- else {
541
- console.error(`Color "${name}" not found.`);
542
- return {};
543
- }
544
- } else {
545
- console.error(`Color "${name}" not found.`);
546
- return {};
547
- }
548
- }
549
- return cssVars(Object.fromEntries(palette.entries()), (n) => this.vars.c(accentVars ? `accent-${n}` : n));
550
- }
551
- rootVars() {
552
- const cRoot = {};
553
- for (const mn in FLAT_COLORS) cRoot[this.vars.c(mn)] = this.colors.custom.get(mn) ?? FLAT_COLORS[mn];
554
- const dc = super.rootVars();
555
- Object.assign(cRoot, dc);
556
- const addPalette = (suffix, palette) => {
557
- for (const [shade, color] of palette) cRoot[this.vars.c(`${suffix}-${shade}`)] = color;
558
- };
559
- addPalette("default", this.items.get("default") ?? /* @__PURE__ */ new Map());
560
- addPalette("accent", this.items.get("accent") ?? /* @__PURE__ */ new Map());
561
- return cRoot;
562
- }
563
- toValue(name, alpha) {
564
- return this.theme.colorValue(COLOR_SHADE_EX_REGEX.exec(name) ? this.vars.color(name) : name, alpha);
565
- }
566
- };
567
916
  // src/size.ts
568
917
  function sizes(themeProvider) {
569
918
  return {
@@ -621,20 +970,40 @@ function sizes(themeProvider) {
621
970
  }
622
971
  var SizesGenerator = class extends VarsGeneratorBase {
623
972
  items;
973
+ /** Whether `md` names the `default` size while no size of that name exists. Removing `md` ends it. */
974
+ md = true;
624
975
  constructor(ctx) {
625
976
  super(ctx);
626
977
  this.items = new Map(Object.entries(sizes(this.theme)));
627
978
  }
979
+ alias(name) {
980
+ return name === "md" && this.md && !this.items.has("md");
981
+ }
982
+ names() {
983
+ const names = super.names();
984
+ return this.alias("md") ? names.flatMap((n) => n === "default" ? [n, "md"] : [n]) : names;
985
+ }
986
+ update(name, builder) {
987
+ if (this.alias(name)) this.items.set(name, structuredClone(this.items.get("default")));
988
+ super.update(name, builder);
989
+ }
990
+ remove(name) {
991
+ const alias = this.alias(name);
992
+ if (name === "md") this.md = false;
993
+ if (!alias) super.remove(name);
994
+ }
628
995
  cssVars(name) {
629
- const size = this.items.get(name);
996
+ const size = this.items.get(this.alias(name) ? "default" : name);
630
997
  if (!size) {
631
998
  console.error(`Size "${name}" not found.`);
632
999
  return {};
633
1000
  }
634
- const rSize = { ...size };
635
- const sText = size.text;
1001
+ const { spacing, text: sText, ...own } = size;
1002
+ const rSize = {
1003
+ spacing,
1004
+ text: Array.isArray(sText) ? sText[0] : sText
1005
+ };
636
1006
  if (Array.isArray(sText)) {
637
- rSize.text = sText[0];
638
1007
  const rVal = sText[1];
639
1008
  if (typeof rVal === "string") rSize["line-height"] = rVal;
640
1009
  else if (typeof rVal === "object" && rVal !== null) {
@@ -643,6 +1012,7 @@ var SizesGenerator = class extends VarsGeneratorBase {
643
1012
  if (rVal.fontWeight) rSize["font-weight"] = rVal.fontWeight;
644
1013
  }
645
1014
  }
1015
+ Object.assign(rSize, own);
646
1016
  return cssVars(rSize, (n) => this.vars.s(n));
647
1017
  }
648
1018
  };
@@ -669,7 +1039,7 @@ var ComponentsGenerator = class extends BuilderBase {
669
1039
  // src/builder.ts
670
1040
  function createBuilder(colors, variants, sizes, components) {
671
1041
  return {
672
- colorsNames: () => colors.names(),
1042
+ colorsNames: (all) => colors.names(all),
673
1043
  variantsNames: () => variants.names(),
674
1044
  sizesNames: () => sizes.names(),
675
1045
  componentsNames: () => components.names(),
@@ -693,6 +1063,18 @@ function createBuilder(colors, variants, sizes, components) {
693
1063
  colors.setColor(name, value);
694
1064
  return this;
695
1065
  },
1066
+ setVariant(name, builder) {
1067
+ variants.set(name, builder);
1068
+ return this;
1069
+ },
1070
+ setSize(name, builder) {
1071
+ sizes.set(name, builder);
1072
+ return this;
1073
+ },
1074
+ setComponent(name, builder) {
1075
+ components.set(name, builder);
1076
+ return this;
1077
+ },
696
1078
  updateColor(name, builder) {
697
1079
  colors.update(name, builder);
698
1080
  return this;
@@ -724,6 +1106,10 @@ function createBuilder(colors, variants, sizes, components) {
724
1106
  removeComponent(name) {
725
1107
  components.remove(name);
726
1108
  return this;
1109
+ },
1110
+ overrideVariant(variant, colors, override) {
1111
+ variants.override(variant, colors, override);
1112
+ return this;
727
1113
  }
728
1114
  };
729
1115
  }
@@ -731,6 +1117,13 @@ function createBuilder(colors, variants, sizes, components) {
731
1117
  function create(config) {
732
1118
  const rConfig = resolveConfig(config);
733
1119
  const vars = varsProvider(rConfig.prefix);
1120
+ const reported = /* @__PURE__ */ new Set();
1121
+ /** Warns once per instance, for each source, about the names that are not palettes. */
1122
+ const notPalettes = (source, names) => {
1123
+ if (!names.length || reported.has(source)) return;
1124
+ reported.add(source);
1125
+ console.warn(`WindiUI: ${source} names ${names.map((c) => `"${c}"`).join(", ")}, which ${names.length > 1 ? "are not palettes" : "is not a palette"}.`);
1126
+ };
734
1127
  return {
735
1128
  vars,
736
1129
  config: rConfig,
@@ -739,18 +1132,21 @@ function create(config) {
739
1132
  vars,
740
1133
  theme
741
1134
  };
742
- const colors = new ColorsGenerator(ctx, rConfig);
743
- const variants = new VariantsGenerator(ctx, rConfig.variantScope);
1135
+ const variants = new VariantsGenerator(ctx, rConfig.variantScope, rConfig.overrideMode);
1136
+ const colors = new ColorsGenerator(ctx, rConfig, variants);
744
1137
  const sizes = new SizesGenerator(ctx);
745
1138
  const components = new ComponentsGenerator({
746
1139
  ...ctx,
747
1140
  colors,
748
1141
  variants
749
1142
  });
750
- if (rConfig.build) {
751
- const builder = createBuilder(colors, variants, sizes, components);
752
- rConfig.build(builder);
753
- }
1143
+ const builder = createBuilder(colors, variants, sizes, components);
1144
+ for (const preset of rConfig.presets) preset.build(builder);
1145
+ rConfig.build?.(builder);
1146
+ for (const [variant, overrides] of Object.entries(rConfig.overrides ?? {})) for (const [color, override] of Object.entries(overrides)) variants.override(variant, color, override);
1147
+ const palettes = colors.names(true);
1148
+ notPalettes("`colors.only`", colors.unknownOnly());
1149
+ notPalettes("an override", variants.overrideColors().filter((c) => !palettes.includes(c) && c !== "default" && c !== "accent"));
754
1150
  return {
755
1151
  colors,
756
1152
  variants,
@@ -762,4 +1158,5 @@ function create(config) {
762
1158
  }
763
1159
  exports.create = create;
764
1160
  exports.defineConfig = defineConfig;
1161
+ exports.definePreset = require_nuxt_ui.definePreset;
765
1162
  exports.varsProvider = varsProvider;