unocss-preset-magicolor 0.3.0 → 0.4.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/helper.js +79 -39
- package/dist/index.js +674 -361
- package/dist/types/helper.d.ts +27 -4
- package/dist/types/index-Dw-AtKzW.d.ts +22 -0
- package/dist/types/index.d.ts +7 -5
- package/dist/types/preflights/index.d.ts +7 -3
- package/dist/types/rules/color-style.d.ts +8 -3
- package/dist/types/rules/color-variable.d.ts +7 -2
- package/dist/types/rules/index.d.ts +7 -2
- package/dist/types/rules/utils/bg-gradient.d.ts +8 -3
- package/dist/types/rules/utils/border.d.ts +8 -3
- package/dist/types/rules/utils/index.d.ts +7 -6
- package/dist/types/rules/utils/mask.d.ts +8 -3
- package/dist/types/rules/utils/shadow.d.ts +8 -3
- package/dist/types/rules/utils/theme-colors.d.ts +2 -10
- package/dist/types/rules/utils/utilities.d.ts +18 -16
- package/dist/types/scanner-BQtbomgr.d.ts +14 -0
- package/dist/types/theme-colors-CYVY9Q3V.d.ts +23 -0
- package/dist/types/types-DQx1LGdq.d.ts +10 -0
- package/dist/types/types.d.ts +2 -5
- package/dist/types/typing-DneJMygO.d.ts +13 -0
- package/dist/types/typing.d.ts +2 -2
- package/dist/types/usages/index.d.ts +3 -0
- package/dist/types/usages/scanner.d.ts +2 -0
- package/dist/types/usages/token.d.ts +9 -0
- package/dist/utils-u9jMSN0V.js +202 -0
- package/package.json +7 -4
- package/dist/common-Dv0krm1V.js +0 -59
- package/dist/types/utils/common.d.ts +0 -20
- package/dist/types/utils/index.d.ts +0 -2
- package/dist/types/utils/transforms.d.ts +0 -7
package/dist/helper.js
CHANGED
|
@@ -1,40 +1,80 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return;
|
|
7
|
-
const s = a?.split(/-\d+-?/)[0];
|
|
8
|
-
if (h(s))
|
|
9
|
-
return;
|
|
10
|
-
const e = {}, t = {};
|
|
11
|
-
try {
|
|
12
|
-
if (m.valid(s)) {
|
|
13
|
-
e[`--mc-${c}-color`] = s;
|
|
14
|
-
const o = m.theme(s, { type: "hex" });
|
|
15
|
-
for (const r of C) {
|
|
16
|
-
const n = g({ type: "hex", components: [o[r]], alpha: 1 });
|
|
17
|
-
t[r] = n;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
} catch (o) {
|
|
21
|
-
console.error(`[updateMagicColor] get ${s} theme fail, please use another color.`), console.error(o);
|
|
22
|
-
}
|
|
23
|
-
const l = a.match(/.*-(\d+)/)?.[1];
|
|
24
|
-
if (l) {
|
|
25
|
-
const { originDepth: o, beforeDepth: r, afterDepth: n } = b(l);
|
|
26
|
-
t[r] && t[n] && (e[`--mc-${c}-color`] = d({
|
|
27
|
-
originDepth: o,
|
|
28
|
-
beforeDepth: r,
|
|
29
|
-
beforeComponents: t[r].components,
|
|
30
|
-
afterComponents: t[n].components
|
|
31
|
-
}));
|
|
32
|
-
}
|
|
33
|
-
const f = u(c, t);
|
|
34
|
-
Object.assign(e, f);
|
|
35
|
-
for (const o in e)
|
|
36
|
-
e[o] && i.style.setProperty(o, e[o].toString());
|
|
1
|
+
import { a as resolveColorParts, n as getThemeDepthColor, r as isInvalidColor, t as getMcThemeMetaColors } from "./utils-u9jMSN0V.js";
|
|
2
|
+
import { mc } from "magic-color";
|
|
3
|
+
//#region src/helper.ts
|
|
4
|
+
function escapeRegExp(value) {
|
|
5
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
37
6
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
7
|
+
function collectStyleVariables(name, style, usage) {
|
|
8
|
+
const colorVariableRE = new RegExp(`^--mc-${escapeRegExp(name)}(?:-(\\d+))?-color$`);
|
|
9
|
+
for (let i = 0; i < style.length; i++) {
|
|
10
|
+
const match = style.item(i).match(colorVariableRE);
|
|
11
|
+
if (!match) continue;
|
|
12
|
+
const [, depth] = match;
|
|
13
|
+
if (depth) usage.depths.add(depth);
|
|
14
|
+
else usage.hasBase = true;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function collectDefinedColorVariables(name, dom) {
|
|
18
|
+
const usage = {
|
|
19
|
+
hasBase: false,
|
|
20
|
+
depths: /* @__PURE__ */ new Set()
|
|
21
|
+
};
|
|
22
|
+
const window = dom.ownerDocument.defaultView;
|
|
23
|
+
collectStyleVariables(name, dom.style, usage);
|
|
24
|
+
if (window) collectStyleVariables(name, window.getComputedStyle(dom), usage);
|
|
25
|
+
return usage;
|
|
26
|
+
}
|
|
27
|
+
function generateColorVariable(name, color, depth) {
|
|
28
|
+
return { [`--mc-${name}${depth == null ? "" : `-${depth}`}-color`]: color };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Generate CSS color variables for a magic color.
|
|
32
|
+
* @param params params Parameter object
|
|
33
|
+
* @param params.name Color name
|
|
34
|
+
* @param params.color Color value, supports a color depth suffix like `#9c1d1e-457`
|
|
35
|
+
* @param params.hasBase Whether to generate the base color variable
|
|
36
|
+
* @param params.depths Color depths that need depth-specific variables
|
|
37
|
+
* @returns CSS variables for the requested magic color usage
|
|
38
|
+
*/
|
|
39
|
+
function getMagicColorStyle(params) {
|
|
40
|
+
const { name, color, hasBase, depths } = params;
|
|
41
|
+
const { originColor, bodyNo } = resolveColorParts(color);
|
|
42
|
+
if (isInvalidColor(originColor)) return {};
|
|
43
|
+
const themeMetaColors = getMcThemeMetaColors(originColor);
|
|
44
|
+
const css = {};
|
|
45
|
+
if (hasBase) {
|
|
46
|
+
if (bodyNo) {
|
|
47
|
+
const baseColor = getThemeDepthColor(themeMetaColors, bodyNo);
|
|
48
|
+
baseColor && Object.assign(css, generateColorVariable(name, baseColor));
|
|
49
|
+
} else if (mc.valid(originColor)) {
|
|
50
|
+
const baseColor = mc(originColor).css("oklch");
|
|
51
|
+
Object.assign(css, generateColorVariable(name, baseColor));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const depth of depths) {
|
|
55
|
+
const depthColor = getThemeDepthColor(themeMetaColors, depth);
|
|
56
|
+
if (depthColor) Object.assign(css, generateColorVariable(name, depthColor, depth));
|
|
57
|
+
}
|
|
58
|
+
return css;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Modify the value of the color variable
|
|
62
|
+
* @param params params Parameter object
|
|
63
|
+
* @param params.name Color name
|
|
64
|
+
* @param params.color Color
|
|
65
|
+
* @param params.dom params.dom Target element, modifying the entire page theme when passing `document.documentElement`
|
|
66
|
+
*/
|
|
67
|
+
function updateMagicColor(params) {
|
|
68
|
+
const { name, color, dom } = params;
|
|
69
|
+
if (!dom) return;
|
|
70
|
+
const { hasBase, depths } = collectDefinedColorVariables(name, dom);
|
|
71
|
+
const css = getMagicColorStyle({
|
|
72
|
+
name,
|
|
73
|
+
color,
|
|
74
|
+
hasBase,
|
|
75
|
+
depths
|
|
76
|
+
});
|
|
77
|
+
for (const [variable, value] of Object.entries(css)) if (value) dom.style.setProperty(variable, value.toString());
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
export { getMagicColorStyle, updateMagicColor };
|