@hanzogui/theme 2.0.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 +21 -0
- package/README.md +1 -0
- package/dist/cjs/_mutateTheme.cjs +136 -0
- package/dist/cjs/_mutateTheme.native.js +158 -0
- package/dist/cjs/_mutateTheme.native.js.map +1 -0
- package/dist/cjs/addTheme.cjs +33 -0
- package/dist/cjs/addTheme.native.js +36 -0
- package/dist/cjs/addTheme.native.js.map +1 -0
- package/dist/cjs/index.cjs +30 -0
- package/dist/cjs/index.native.js +33 -0
- package/dist/cjs/index.native.js.map +1 -0
- package/dist/cjs/replaceTheme.cjs +37 -0
- package/dist/cjs/replaceTheme.native.js +42 -0
- package/dist/cjs/replaceTheme.native.js.map +1 -0
- package/dist/cjs/updateTheme.cjs +37 -0
- package/dist/cjs/updateTheme.native.js +41 -0
- package/dist/cjs/updateTheme.native.js.map +1 -0
- package/dist/esm/_mutateTheme.mjs +112 -0
- package/dist/esm/_mutateTheme.mjs.map +1 -0
- package/dist/esm/_mutateTheme.native.js +131 -0
- package/dist/esm/_mutateTheme.native.js.map +1 -0
- package/dist/esm/addTheme.mjs +10 -0
- package/dist/esm/addTheme.mjs.map +1 -0
- package/dist/esm/addTheme.native.js +10 -0
- package/dist/esm/addTheme.native.js.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.mjs +6 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/index.native.js +6 -0
- package/dist/esm/index.native.js.map +1 -0
- package/dist/esm/replaceTheme.mjs +14 -0
- package/dist/esm/replaceTheme.mjs.map +1 -0
- package/dist/esm/replaceTheme.native.js +16 -0
- package/dist/esm/replaceTheme.native.js.map +1 -0
- package/dist/esm/updateTheme.mjs +14 -0
- package/dist/esm/updateTheme.mjs.map +1 -0
- package/dist/esm/updateTheme.native.js +15 -0
- package/dist/esm/updateTheme.native.js.map +1 -0
- package/package.json +48 -0
- package/src/_mutateTheme.ts +197 -0
- package/src/addTheme.ts +11 -0
- package/src/index.ts +5 -0
- package/src/replaceTheme.ts +14 -0
- package/src/updateTheme.ts +13 -0
- package/types/_mutateTheme.d.ts +27 -0
- package/types/_mutateTheme.d.ts.map +1 -0
- package/types/addTheme.d.ts +11 -0
- package/types/addTheme.d.ts.map +1 -0
- package/types/index.d.ts +6 -0
- package/types/index.d.ts.map +1 -0
- package/types/replaceTheme.d.ts +10 -0
- package/types/replaceTheme.d.ts.map +1 -0
- package/types/updateTheme.d.ts +10 -0
- package/types/updateTheme.d.ts.map +1 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { isServer } from "@hanzogui/constants";
|
|
2
|
+
import { startTransition } from "@hanzogui/start-transition";
|
|
3
|
+
import { ensureThemeVariable, forceUpdateThemes, getConfig, getThemeCSSRules, mutatedAutoVariables, proxyThemeToParents, simpleHash, updateConfig } from "@hanzogui/web";
|
|
4
|
+
function mutateThemes({
|
|
5
|
+
themes,
|
|
6
|
+
batch,
|
|
7
|
+
insertCSS = !0,
|
|
8
|
+
...props
|
|
9
|
+
}) {
|
|
10
|
+
const allThemesProxied = {},
|
|
11
|
+
allThemesRaw = {};
|
|
12
|
+
for (const {
|
|
13
|
+
name,
|
|
14
|
+
theme
|
|
15
|
+
} of themes) {
|
|
16
|
+
const res = _mutateTheme({
|
|
17
|
+
...props,
|
|
18
|
+
name,
|
|
19
|
+
theme,
|
|
20
|
+
// we'll do one update at the end
|
|
21
|
+
avoidUpdate: !0,
|
|
22
|
+
// always add which also replaces but doesnt fail first time
|
|
23
|
+
mutationType: "add"
|
|
24
|
+
});
|
|
25
|
+
res && (allThemesProxied[name] = res.theme, allThemesRaw[name] = res.themeRaw);
|
|
26
|
+
}
|
|
27
|
+
const cssRules = insertCSS ? insertThemeCSS(allThemesRaw, batch) : [];
|
|
28
|
+
return startTransition(() => {
|
|
29
|
+
for (const themeName in allThemesProxied) {
|
|
30
|
+
const theme = allThemesProxied[themeName];
|
|
31
|
+
updateThemeConfig(themeName, theme);
|
|
32
|
+
}
|
|
33
|
+
updateThemeStates();
|
|
34
|
+
}), {
|
|
35
|
+
themes: allThemesProxied,
|
|
36
|
+
themesRaw: allThemesRaw,
|
|
37
|
+
cssRules
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function _mutateTheme(props) {
|
|
41
|
+
if (isServer) {
|
|
42
|
+
process.env.NODE_ENV === "development" && console.warn("Theme mutation is not supported on server side");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const config = getConfig(),
|
|
46
|
+
{
|
|
47
|
+
name: themeName,
|
|
48
|
+
theme: themeIn,
|
|
49
|
+
insertCSS,
|
|
50
|
+
mutationType
|
|
51
|
+
} = props;
|
|
52
|
+
if (process.env.NODE_ENV === "development") {
|
|
53
|
+
if (!config) throw new Error("No config");
|
|
54
|
+
const theme2 = config.themes[props.name];
|
|
55
|
+
if (mutationType !== "add" && !theme2) throw new Error(`${mutationType === "replace" ? "Replace" : "Update"} theme failed! Theme ${props.name} does not exist`);
|
|
56
|
+
}
|
|
57
|
+
const theme = {
|
|
58
|
+
...(mutationType === "update" ? config.themes[themeName] ?? {} : {}),
|
|
59
|
+
...themeIn
|
|
60
|
+
};
|
|
61
|
+
for (const key in theme) ensureThemeVariable(theme, key);
|
|
62
|
+
const themeProxied = proxyThemeToParents(themeName, theme),
|
|
63
|
+
response = {
|
|
64
|
+
themeRaw: theme,
|
|
65
|
+
theme: themeProxied,
|
|
66
|
+
cssRules: []
|
|
67
|
+
};
|
|
68
|
+
return props.avoidUpdate || (insertCSS && (response.cssRules = insertThemeCSS({
|
|
69
|
+
[themeName]: theme
|
|
70
|
+
})), updateThemeConfig(themeName, themeProxied), updateThemeStates()), response;
|
|
71
|
+
}
|
|
72
|
+
function updateThemeConfig(themeName, theme) {
|
|
73
|
+
const config = getConfig();
|
|
74
|
+
config.themes[themeName] = theme, updateConfig("themes", config.themes);
|
|
75
|
+
}
|
|
76
|
+
function updateThemeStates() {
|
|
77
|
+
forceUpdateThemes();
|
|
78
|
+
}
|
|
79
|
+
function insertThemeCSS(themes, batch = !1) {
|
|
80
|
+
const config = getConfig();
|
|
81
|
+
let cssRules = [];
|
|
82
|
+
for (const themeName in themes) {
|
|
83
|
+
const theme = themes[themeName],
|
|
84
|
+
rules = getThemeCSSRules({
|
|
85
|
+
config,
|
|
86
|
+
themeName,
|
|
87
|
+
names: [themeName],
|
|
88
|
+
hasDarkLight: !0,
|
|
89
|
+
theme,
|
|
90
|
+
// Use mutated variable creator which starts from high index to avoid conflicts
|
|
91
|
+
useMutatedVariables: !0
|
|
92
|
+
});
|
|
93
|
+
cssRules = [...cssRules, ...rules], batch || updateStyle(`t_theme_style_${themeName}`, rules);
|
|
94
|
+
}
|
|
95
|
+
if (mutatedAutoVariables.length > 0) {
|
|
96
|
+
const autoVarCSS = `:root{${mutatedAutoVariables.map(v => `--${v.name}:${v.val}`).join(";")}}`;
|
|
97
|
+
updateStyle("t_mutate_vars", [autoVarCSS]);
|
|
98
|
+
}
|
|
99
|
+
if (batch) {
|
|
100
|
+
const id = typeof batch == "string" ? batch : simpleHash(Object.keys(themes).join(""));
|
|
101
|
+
updateStyle(`t_theme_style_${id}`, cssRules);
|
|
102
|
+
}
|
|
103
|
+
return cssRules;
|
|
104
|
+
}
|
|
105
|
+
function updateStyle(id, rules) {
|
|
106
|
+
const existing = document.querySelector(`#${id}`),
|
|
107
|
+
style = document.createElement("style");
|
|
108
|
+
style.id = id, style.appendChild(document.createTextNode(rules.join(`
|
|
109
|
+
`))), document.head.appendChild(style), existing && existing.parentElement?.removeChild(existing);
|
|
110
|
+
}
|
|
111
|
+
export { _mutateTheme, mutateThemes };
|
|
112
|
+
//# sourceMappingURL=_mutateTheme.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isServer","startTransition","ensureThemeVariable","forceUpdateThemes","getConfig","getThemeCSSRules","mutatedAutoVariables","proxyThemeToParents","simpleHash","updateConfig","mutateThemes","themes","batch","insertCSS","props","allThemesProxied","allThemesRaw","name","theme","res","_mutateTheme","avoidUpdate","mutationType","themeRaw","cssRules","insertThemeCSS","themeName","updateThemeConfig","updateThemeStates","themesRaw","process","env","NODE_ENV","console","warn","config","themeIn","Error","theme2","key","themeProxied","response","rules","names","hasDarkLight","useMutatedVariables","updateStyle","length","autoVarCSS","map","v","val","join","id","Object","keys","existing","document","querySelector","style","createElement","appendChild","createTextNode","head","parentElement","removeChild"],"sources":["../../src/_mutateTheme.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,QAAA,QAAgB;AACzB,SAASC,eAAA,QAAuB;AAEhC,SACEC,mBAAA,EACAC,iBAAA,EACAC,SAAA,EACAC,gBAAA,EACAC,oBAAA,EACAC,mBAAA,EACAC,UAAA,EACAC,YAAA,QACK;AAmBA,SAASC,aAAa;EAC3BC,MAAA;EACAC,KAAA;EACAC,SAAA,GAAY;EACZ,GAAGC;AACL,GAKG;EACD,MAAMC,gBAAA,GAAgD,CAAC;IACjDC,YAAA,GAA4C,CAAC;EACnD,WAAW;IAAEC,IAAA;IAAMC;EAAM,KAAKP,MAAA,EAAQ;IACpC,MAAMQ,GAAA,GAAMC,YAAA,CAAa;MACvB,GAAGN,KAAA;MACHG,IAAA;MACAC,KAAA;MAAA;MAEAG,WAAA,EAAa;MAAA;MAEbC,YAAA,EAAc;IAChB,CAAC;IACGH,GAAA,KACFJ,gBAAA,CAAiBE,IAAI,IAAIE,GAAA,CAAID,KAAA,EAC7BF,YAAA,CAAaC,IAAI,IAAIE,GAAA,CAAII,QAAA;EAE7B;EAEA,MAAMC,QAAA,GAAWX,SAAA,GAAYY,cAAA,CAAeT,YAAA,EAAcJ,KAAK,IAAI,EAAC;EAEpE,OAAAX,eAAA,CAAgB,MAAM;IACpB,WAAWyB,SAAA,IAAaX,gBAAA,EAAkB;MACxC,MAAMG,KAAA,GAAQH,gBAAA,CAAiBW,SAAS;MACxCC,iBAAA,CAAkBD,SAA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AK,OAAOzC,KAAA,IAAS,WAAWA,KAAA,GAAQJ,UAAA,CAAW8C,MAAA,CAAOC,IAAA,CAAK5C,MAAM,EAAEyC,IAAA,CAAK,EAAE,CAAC;IACrFN,WAAA,CAAY,iBAAiBO,EAAE,IAAI7B,QAAQ;EAC7C;EAEA,OAAOA,QAAA;AACT;AAEA,SAASsB,YAAYO,EAAA,EAAYX,KAAA,EAAiB;EAChD,MAAMc,QAAA,GAAWC,QAAA,CAASC,aAAA,CAAc,IAAIL,EAAE,EAAE;IAC1CM,KAAA,GAAQF,QAAA,CAASG,aAAA,CAAc,OAAO;EAC5CD,KAAA,CAAMN,EAAA,GAAKA,EAAA,EACXM,KAAA,CAAME,WAAA,CAAYJ,QAAA,CAASK,cAAA,CAAepB,KAAA,CAAMU,IAAA,CAAK;AAAA,CAAI,CAAC,CAAC,GAC3DK,QAAA,CAASM,IAAA,CAAKF,WAAA,CAAYF,KAAK,GAC3BH,QAAA,IACFA,QAAA,CAASQ,aAAA,EAAeC,WAAA,CAAYT,QAAQ;AAEhD","ignoreList":[]}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { isServer } from "@hanzogui/constants";
|
|
2
|
+
import { startTransition } from "@hanzogui/start-transition";
|
|
3
|
+
import { ensureThemeVariable, forceUpdateThemes, getConfig, getThemeCSSRules, mutatedAutoVariables, proxyThemeToParents, simpleHash, updateConfig } from "@hanzogui/web";
|
|
4
|
+
function mutateThemes(param) {
|
|
5
|
+
var {
|
|
6
|
+
themes,
|
|
7
|
+
batch,
|
|
8
|
+
insertCSS = !0,
|
|
9
|
+
...props
|
|
10
|
+
} = param,
|
|
11
|
+
allThemesProxied = {},
|
|
12
|
+
allThemesRaw = {},
|
|
13
|
+
_iteratorNormalCompletion = !0,
|
|
14
|
+
_didIteratorError = !1,
|
|
15
|
+
_iteratorError = void 0;
|
|
16
|
+
try {
|
|
17
|
+
for (var _iterator = themes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
|
|
18
|
+
var {
|
|
19
|
+
name,
|
|
20
|
+
theme
|
|
21
|
+
} = _step.value,
|
|
22
|
+
res = _mutateTheme({
|
|
23
|
+
...props,
|
|
24
|
+
name,
|
|
25
|
+
theme,
|
|
26
|
+
// we'll do one update at the end
|
|
27
|
+
avoidUpdate: !0,
|
|
28
|
+
// always add which also replaces but doesnt fail first time
|
|
29
|
+
mutationType: "add"
|
|
30
|
+
});
|
|
31
|
+
res && (allThemesProxied[name] = res.theme, allThemesRaw[name] = res.themeRaw);
|
|
32
|
+
}
|
|
33
|
+
} catch (err) {
|
|
34
|
+
_didIteratorError = !0, _iteratorError = err;
|
|
35
|
+
} finally {
|
|
36
|
+
try {
|
|
37
|
+
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
|
|
38
|
+
} finally {
|
|
39
|
+
if (_didIteratorError) throw _iteratorError;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
var cssRules = insertCSS ? insertThemeCSS(allThemesRaw, batch) : [];
|
|
43
|
+
return startTransition(function () {
|
|
44
|
+
for (var themeName in allThemesProxied) {
|
|
45
|
+
var theme2 = allThemesProxied[themeName];
|
|
46
|
+
updateThemeConfig(themeName, theme2);
|
|
47
|
+
}
|
|
48
|
+
updateThemeStates();
|
|
49
|
+
}), {
|
|
50
|
+
themes: allThemesProxied,
|
|
51
|
+
themesRaw: allThemesRaw,
|
|
52
|
+
cssRules
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function _mutateTheme(props) {
|
|
56
|
+
if (isServer) {
|
|
57
|
+
process.env.NODE_ENV === "development" && console.warn("Theme mutation is not supported on server side");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
var config = getConfig(),
|
|
61
|
+
{
|
|
62
|
+
name: themeName,
|
|
63
|
+
theme: themeIn,
|
|
64
|
+
insertCSS,
|
|
65
|
+
mutationType
|
|
66
|
+
} = props;
|
|
67
|
+
if (process.env.NODE_ENV === "development") {
|
|
68
|
+
if (!config) throw new Error("No config");
|
|
69
|
+
var theme = config.themes[props.name];
|
|
70
|
+
if (mutationType !== "add" && !theme) throw new Error(`${mutationType === "replace" ? "Replace" : "Update"} theme failed! Theme ${props.name} does not exist`);
|
|
71
|
+
}
|
|
72
|
+
var _config_themes_themeName,
|
|
73
|
+
theme1 = {
|
|
74
|
+
...(mutationType === "update" ? (_config_themes_themeName = config.themes[themeName]) !== null && _config_themes_themeName !== void 0 ? _config_themes_themeName : {} : {}),
|
|
75
|
+
...themeIn
|
|
76
|
+
};
|
|
77
|
+
for (var key in theme1) ensureThemeVariable(theme1, key);
|
|
78
|
+
var themeProxied = proxyThemeToParents(themeName, theme1),
|
|
79
|
+
response = {
|
|
80
|
+
themeRaw: theme1,
|
|
81
|
+
theme: themeProxied,
|
|
82
|
+
cssRules: []
|
|
83
|
+
};
|
|
84
|
+
return props.avoidUpdate || (insertCSS && (response.cssRules = insertThemeCSS({
|
|
85
|
+
[themeName]: theme1
|
|
86
|
+
})), updateThemeConfig(themeName, themeProxied), updateThemeStates()), response;
|
|
87
|
+
}
|
|
88
|
+
function updateThemeConfig(themeName, theme) {
|
|
89
|
+
var config = getConfig();
|
|
90
|
+
config.themes[themeName] = theme, updateConfig("themes", config.themes);
|
|
91
|
+
}
|
|
92
|
+
function updateThemeStates() {
|
|
93
|
+
forceUpdateThemes();
|
|
94
|
+
}
|
|
95
|
+
function insertThemeCSS(themes) {
|
|
96
|
+
var batch = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !1;
|
|
97
|
+
return [];
|
|
98
|
+
var config, cssRules;
|
|
99
|
+
for (var themeName in themes) {
|
|
100
|
+
var theme = themes[themeName],
|
|
101
|
+
rules = getThemeCSSRules({
|
|
102
|
+
config,
|
|
103
|
+
themeName,
|
|
104
|
+
names: [themeName],
|
|
105
|
+
hasDarkLight: !0,
|
|
106
|
+
theme,
|
|
107
|
+
// Use mutated variable creator which starts from high index to avoid conflicts
|
|
108
|
+
useMutatedVariables: !0
|
|
109
|
+
});
|
|
110
|
+
cssRules = [...cssRules, ...rules], batch || updateStyle(`t_theme_style_${themeName}`, rules);
|
|
111
|
+
}
|
|
112
|
+
if (mutatedAutoVariables.length > 0) {
|
|
113
|
+
var autoVarCSS;
|
|
114
|
+
updateStyle("t_mutate_vars", [autoVarCSS]);
|
|
115
|
+
}
|
|
116
|
+
if (batch) {
|
|
117
|
+
var id;
|
|
118
|
+
updateStyle(`t_theme_style_${id}`, cssRules);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function updateStyle(id, rules) {
|
|
122
|
+
var existing = document.querySelector(`#${id}`),
|
|
123
|
+
style = document.createElement("style");
|
|
124
|
+
if (style.id = id, style.appendChild(document.createTextNode(rules.join(`
|
|
125
|
+
`))), document.head.appendChild(style), existing) {
|
|
126
|
+
var _existing_parentElement;
|
|
127
|
+
(_existing_parentElement = existing.parentElement) === null || _existing_parentElement === void 0 || _existing_parentElement.removeChild(existing);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
export { _mutateTheme, mutateThemes };
|
|
131
|
+
//# sourceMappingURL=_mutateTheme.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isServer","startTransition","ensureThemeVariable","forceUpdateThemes","getConfig","getThemeCSSRules","mutatedAutoVariables","proxyThemeToParents","simpleHash","updateConfig","mutateThemes","param","themes","batch","insertCSS","props","allThemesProxied","allThemesRaw","_iteratorNormalCompletion","_didIteratorError","_iteratorError","_iterator","Symbol","iterator","_step","next","done","name","theme","value","res","_mutateTheme","avoidUpdate","mutationType","themeRaw","err","return","cssRules","insertThemeCSS","themeName","theme2","updateThemeConfig","updateThemeStates","themesRaw","process","env","NODE_ENV","console","warn","config","themeIn","Error","_config_themes_themeName","theme1","key","themeProxied","response","arguments","length","rules","names","hasDarkLight","useMutatedVariables","updateStyle","autoVarCSS","id","existing","document","querySelector","style","createElement","appendChild","createTextNode","join"],"sources":["../../src/_mutateTheme.ts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,CAAAK,cAAA,CAAAb,KAAA,CAAAc,IAAA","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_mutateTheme","addTheme","props","insertCSS","mutationType"],"sources":["../../src/addTheme.ts"],"sourcesContent":[null],"mappings":"AAEA,SAASA,YAAA,QAAoB;AAEtB,SAASC,SAASC,KAAA,EAItB;EACD,OAAOF,YAAA,CAAa;IAAE,GAAGE,KAAA;IAAOC,SAAA,EAAW;IAAMC,YAAA,EAAc;EAAM,CAAC;AACxE","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_mutateTheme","addTheme","props"],"sources":["../../src/addTheme.ts"],"sourcesContent":[null],"mappings":"AAEA,SAASA,YAAA,QAAoB;AAEtB,SAASC,SAASC,KAAA,EAItB;EACD,OAAOF,YAAA,CAAa;IACtB,GAAAE,KAAA","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["mutateThemes"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,SAASA,YAAA,QAAoB","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["mutateThemes"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,SAASA,YAAA,QAAoB","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["mutateThemes"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,SAASA,YAAA,QAAoB","ignoreList":[]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { _mutateTheme } from "./_mutateTheme.mjs";
|
|
2
|
+
function replaceTheme({
|
|
3
|
+
name,
|
|
4
|
+
theme
|
|
5
|
+
}) {
|
|
6
|
+
return _mutateTheme({
|
|
7
|
+
name,
|
|
8
|
+
theme,
|
|
9
|
+
insertCSS: !0,
|
|
10
|
+
mutationType: "replace"
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export { replaceTheme };
|
|
14
|
+
//# sourceMappingURL=replaceTheme.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_mutateTheme","replaceTheme","name","theme","insertCSS","mutationType"],"sources":["../../src/replaceTheme.ts"],"sourcesContent":[null],"mappings":"AAEA,SAASA,YAAA,QAAoB;AAEtB,SAASC,aAAa;EAC3BC,IAAA;EACAC;AACF,GAGG;EAED,OADaH,YAAA,CAAa;IAAEE,IAAA;IAAMC,KAAA;IAAOC,SAAA,EAAW;IAAMC,YAAA,EAAc;EAAU,CAAC;AAErF","ignoreList":[]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { _mutateTheme } from "./_mutateTheme.native.js";
|
|
2
|
+
function replaceTheme(param) {
|
|
3
|
+
var {
|
|
4
|
+
name,
|
|
5
|
+
theme
|
|
6
|
+
} = param,
|
|
7
|
+
next = _mutateTheme({
|
|
8
|
+
name,
|
|
9
|
+
theme,
|
|
10
|
+
insertCSS: !0,
|
|
11
|
+
mutationType: "replace"
|
|
12
|
+
});
|
|
13
|
+
return next;
|
|
14
|
+
}
|
|
15
|
+
export { replaceTheme };
|
|
16
|
+
//# sourceMappingURL=replaceTheme.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_mutateTheme","replaceTheme","param","name","theme","next","insertCSS","mutationType"],"sources":["../../src/replaceTheme.ts"],"sourcesContent":[null],"mappings":"AAEA,SAASA,YAAA,QAAoB;AAEtB,SAASC,aAAaC,KAAA;EAC3B;MAAAC,IAAA;MAAAC;IAAA,IAAAF,KAAA;IAAAG,IAAA,GAAAL,YAAA;MACAG,IAAA;MAICC,KAAA;MAEDE,SADa;MAEfC,YAAA","ignoreList":[]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { _mutateTheme } from "./_mutateTheme.mjs";
|
|
2
|
+
function updateTheme({
|
|
3
|
+
name,
|
|
4
|
+
theme
|
|
5
|
+
}) {
|
|
6
|
+
return _mutateTheme({
|
|
7
|
+
name,
|
|
8
|
+
theme,
|
|
9
|
+
insertCSS: !0,
|
|
10
|
+
mutationType: "update"
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export { updateTheme };
|
|
14
|
+
//# sourceMappingURL=updateTheme.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_mutateTheme","updateTheme","name","theme","insertCSS","mutationType"],"sources":["../../src/updateTheme.ts"],"sourcesContent":[null],"mappings":"AAEA,SAASA,YAAA,QAAoB;AAEtB,SAASC,YAAY;EAC1BC,IAAA;EACAC;AACF,GAGG;EACD,OAAOH,YAAA,CAAa;IAAEE,IAAA;IAAMC,KAAA;IAAOC,SAAA,EAAW;IAAMC,YAAA,EAAc;EAAS,CAAC;AAC9E","ignoreList":[]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { _mutateTheme } from "./_mutateTheme.native.js";
|
|
2
|
+
function updateTheme(param) {
|
|
3
|
+
var {
|
|
4
|
+
name,
|
|
5
|
+
theme
|
|
6
|
+
} = param;
|
|
7
|
+
return _mutateTheme({
|
|
8
|
+
name,
|
|
9
|
+
theme,
|
|
10
|
+
insertCSS: !0,
|
|
11
|
+
mutationType: "update"
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export { updateTheme };
|
|
15
|
+
//# sourceMappingURL=updateTheme.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_mutateTheme","updateTheme","param","name","theme","insertCSS"],"sources":["../../src/updateTheme.ts"],"sourcesContent":[null],"mappings":"AAEA,SAASA,YAAA,QAAoB;AAEtB,SAASC,YAAYC,KAAA;EAC1B;IAAAC,IAAA;IAAAC;EAAA,IAAAF,KAAA;EACA,OAAAF,YAAA;IAICG,IAAA;IACDC,KAAO;IACTC,SAAA","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hanzogui/theme",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"source": "src/index.ts",
|
|
5
|
+
"files": [
|
|
6
|
+
"src",
|
|
7
|
+
"types",
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"main": "dist/cjs",
|
|
13
|
+
"module": "dist/esm",
|
|
14
|
+
"types": "./types/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
"./package.json": "./package.json",
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./types/index.d.ts",
|
|
19
|
+
"react-native": "./dist/esm/index.native.js",
|
|
20
|
+
"browser": "./dist/esm/index.mjs",
|
|
21
|
+
"module": "./dist/esm/index.mjs",
|
|
22
|
+
"import": "./dist/esm/index.mjs",
|
|
23
|
+
"require": "./dist/cjs/index.cjs",
|
|
24
|
+
"default": "./dist/esm/index.mjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "hanzo-gui-build",
|
|
32
|
+
"watch": "hanzo-gui-build --watch",
|
|
33
|
+
"clean": "hanzo-gui-build clean",
|
|
34
|
+
"clean:build": "hanzo-gui-build clean:build"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@hanzogui/constants": "workspace:*",
|
|
38
|
+
"@hanzogui/start-transition": "workspace:*",
|
|
39
|
+
"@hanzogui/web": "workspace:*"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@hanzogui/build": "workspace:*",
|
|
43
|
+
"react": ">=19"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"react": ">=19"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { isServer } from '@hanzogui/constants'
|
|
2
|
+
import { startTransition } from '@hanzogui/start-transition'
|
|
3
|
+
import type { ThemeDefinition, ThemeParsed } from '@hanzogui/web'
|
|
4
|
+
import {
|
|
5
|
+
ensureThemeVariable,
|
|
6
|
+
forceUpdateThemes,
|
|
7
|
+
getConfig,
|
|
8
|
+
getThemeCSSRules,
|
|
9
|
+
mutatedAutoVariables,
|
|
10
|
+
proxyThemeToParents,
|
|
11
|
+
simpleHash,
|
|
12
|
+
updateConfig,
|
|
13
|
+
} from '@hanzogui/web'
|
|
14
|
+
|
|
15
|
+
type MutateThemeOptions = {
|
|
16
|
+
mutationType: 'replace' | 'update' | 'add'
|
|
17
|
+
insertCSS?: boolean
|
|
18
|
+
avoidUpdate?: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type PartialTheme = Partial<Record<keyof ThemeDefinition, any>>
|
|
22
|
+
|
|
23
|
+
export type MutateOneThemeProps = {
|
|
24
|
+
name: string
|
|
25
|
+
theme: PartialTheme
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// need to name the batch in case the theme amount changes so it removes properly
|
|
29
|
+
type Batch = boolean | string
|
|
30
|
+
|
|
31
|
+
// more advanced helper used only internally in studio for now
|
|
32
|
+
export function mutateThemes({
|
|
33
|
+
themes,
|
|
34
|
+
batch,
|
|
35
|
+
insertCSS = true,
|
|
36
|
+
...props
|
|
37
|
+
}: Omit<MutateThemeOptions, 'mutationType'> & {
|
|
38
|
+
themes: MutateOneThemeProps[] // if using batch, know that if you later on do addTheme/etc it will break things
|
|
39
|
+
// batch is only useful if you know youre only ever going to change these themes using batch
|
|
40
|
+
// aka only in studio as a preview mode
|
|
41
|
+
batch?: Batch
|
|
42
|
+
}) {
|
|
43
|
+
const allThemesProxied: Record<string, ThemeParsed> = {}
|
|
44
|
+
const allThemesRaw: Record<string, ThemeParsed> = {}
|
|
45
|
+
for (const { name, theme } of themes) {
|
|
46
|
+
const res = _mutateTheme({
|
|
47
|
+
...props,
|
|
48
|
+
name,
|
|
49
|
+
theme,
|
|
50
|
+
// we'll do one update at the end
|
|
51
|
+
avoidUpdate: true,
|
|
52
|
+
// always add which also replaces but doesnt fail first time
|
|
53
|
+
mutationType: 'add',
|
|
54
|
+
})
|
|
55
|
+
if (res) {
|
|
56
|
+
allThemesProxied[name] = res.theme
|
|
57
|
+
allThemesRaw[name] = res.themeRaw
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const cssRules = insertCSS ? insertThemeCSS(allThemesRaw, batch) : []
|
|
62
|
+
|
|
63
|
+
startTransition(() => {
|
|
64
|
+
for (const themeName in allThemesProxied) {
|
|
65
|
+
const theme = allThemesProxied[themeName]
|
|
66
|
+
updateThemeConfig(themeName, theme)
|
|
67
|
+
}
|
|
68
|
+
updateThemeStates()
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
themes: allThemesProxied,
|
|
73
|
+
themesRaw: allThemesRaw,
|
|
74
|
+
cssRules,
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function _mutateTheme(props: MutateThemeOptions & MutateOneThemeProps) {
|
|
79
|
+
if (isServer) {
|
|
80
|
+
if (process.env.NODE_ENV === 'development') {
|
|
81
|
+
console.warn('Theme mutation is not supported on server side')
|
|
82
|
+
}
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
const config = getConfig()
|
|
86
|
+
const { name: themeName, theme: themeIn, insertCSS, mutationType } = props
|
|
87
|
+
|
|
88
|
+
if (process.env.NODE_ENV === 'development') {
|
|
89
|
+
if (!config) {
|
|
90
|
+
throw new Error('No config')
|
|
91
|
+
}
|
|
92
|
+
const theme = config.themes[props.name]
|
|
93
|
+
|
|
94
|
+
if (mutationType !== 'add' && !theme) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`${mutationType === 'replace' ? 'Replace' : 'Update'} theme failed! Theme ${
|
|
97
|
+
props.name
|
|
98
|
+
} does not exist`
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const theme = {
|
|
104
|
+
...(mutationType === 'update' ? (config.themes[themeName] ?? {}) : {}),
|
|
105
|
+
...themeIn,
|
|
106
|
+
} as ThemeParsed
|
|
107
|
+
|
|
108
|
+
for (const key in theme) {
|
|
109
|
+
ensureThemeVariable(theme, key)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const themeProxied = proxyThemeToParents(themeName, theme)
|
|
113
|
+
|
|
114
|
+
const response = {
|
|
115
|
+
themeRaw: theme,
|
|
116
|
+
theme: themeProxied,
|
|
117
|
+
cssRules: [] as string[],
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (props.avoidUpdate) {
|
|
121
|
+
return response
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (insertCSS) {
|
|
125
|
+
response.cssRules = insertThemeCSS({
|
|
126
|
+
[themeName]: theme,
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
updateThemeConfig(themeName, themeProxied)
|
|
131
|
+
updateThemeStates()
|
|
132
|
+
|
|
133
|
+
return response
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function updateThemeConfig(themeName: string, theme: ThemeParsed) {
|
|
137
|
+
const config = getConfig()
|
|
138
|
+
config.themes[themeName] = theme
|
|
139
|
+
updateConfig('themes', config.themes)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function updateThemeStates() {
|
|
143
|
+
forceUpdateThemes()
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function insertThemeCSS(themes: Record<string, PartialTheme>, batch: Batch = false) {
|
|
147
|
+
if (process.env.HANZO_GUI_TARGET !== 'web') {
|
|
148
|
+
return []
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const config = getConfig()
|
|
152
|
+
let cssRules: string[] = []
|
|
153
|
+
|
|
154
|
+
for (const themeName in themes) {
|
|
155
|
+
const theme = themes[themeName]
|
|
156
|
+
|
|
157
|
+
const rules = getThemeCSSRules({
|
|
158
|
+
config,
|
|
159
|
+
themeName,
|
|
160
|
+
names: [themeName],
|
|
161
|
+
hasDarkLight: true,
|
|
162
|
+
theme,
|
|
163
|
+
// Use mutated variable creator which starts from high index to avoid conflicts
|
|
164
|
+
useMutatedVariables: true,
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
cssRules = [...cssRules, ...rules]
|
|
168
|
+
|
|
169
|
+
if (!batch) {
|
|
170
|
+
updateStyle(`t_theme_style_${themeName}`, rules)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Output ALL mutated auto variables (since updateStyle replaces the element)
|
|
175
|
+
if (mutatedAutoVariables.length > 0) {
|
|
176
|
+
const autoVarCSS = `:root{${mutatedAutoVariables.map((v) => `--${v.name}:${v.val}`).join(';')}}`
|
|
177
|
+
updateStyle(`t_mutate_vars`, [autoVarCSS])
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (batch) {
|
|
181
|
+
const id = typeof batch == 'string' ? batch : simpleHash(Object.keys(themes).join(''))
|
|
182
|
+
updateStyle(`t_theme_style_${id}`, cssRules)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return cssRules
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function updateStyle(id: string, rules: string[]) {
|
|
189
|
+
const existing = document.querySelector(`#${id}`)
|
|
190
|
+
const style = document.createElement('style')
|
|
191
|
+
style.id = id
|
|
192
|
+
style.appendChild(document.createTextNode(rules.join('\n')))
|
|
193
|
+
document.head.appendChild(style)
|
|
194
|
+
if (existing) {
|
|
195
|
+
existing.parentElement?.removeChild(existing)
|
|
196
|
+
}
|
|
197
|
+
}
|
package/src/addTheme.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ThemeDefinition, ThemeParsed } from '@hanzogui/web'
|
|
2
|
+
|
|
3
|
+
import { _mutateTheme } from './_mutateTheme'
|
|
4
|
+
|
|
5
|
+
export function addTheme(props: {
|
|
6
|
+
name: string
|
|
7
|
+
theme: Partial<Record<keyof ThemeDefinition, any>>
|
|
8
|
+
insertCSS?: boolean
|
|
9
|
+
}) {
|
|
10
|
+
return _mutateTheme({ ...props, insertCSS: true, mutationType: 'add' })
|
|
11
|
+
}
|