Theming API: update `mergeKeys` method for presets

pull/5677/head
mertsincan 2024-04-15 11:21:03 +01:00
parent 51110b585a
commit 9bdabd8530
4 changed files with 22 additions and 5 deletions

View File

@ -49,6 +49,9 @@ export default {
handler(newValue) { handler(newValue) {
if (newValue) { if (newValue) {
this._loadScopedThemeStyles(newValue); this._loadScopedThemeStyles(newValue);
this._themeChangeListener(() => this._loadScopedThemeStyles(newValue));
} else {
this._unloadScopedThemeStyles();
} }
} }
} }
@ -91,7 +94,7 @@ export default {
this._hook('onBeforeUnmount'); this._hook('onBeforeUnmount');
}, },
unmounted() { unmounted() {
this.scopedStyleEl?.value?.remove(); this._unloadScopedThemeStyles();
this._hook('onUnmounted'); this._hook('onUnmounted');
}, },
methods: { methods: {
@ -185,6 +188,9 @@ export default {
this.scopedStyleEl = scopedStyle.el; this.scopedStyleEl = scopedStyle.el;
}, },
_unloadScopedThemeStyles() {
this.scopedStyleEl?.value?.remove();
},
_themeChangeListener(callback = () => {}) { _themeChangeListener(callback = () => {}) {
Base.clearLoadedStyleNames(); Base.clearLoadedStyleNames();
ThemeService.on('theme:change', callback); ThemeService.on('theme:change', callback);

View File

@ -1,8 +1,7 @@
import Theme, { SharedUtils } from 'primevue/themes'; import Theme, { SharedUtils } from 'primevue/themes';
export default (preset1, preset2) => { export default (preset1, preset2) => {
const VARIABLE = Theme.defaults.variable; const newPreset = SharedUtils.object.mergeKeys(preset1, preset2);
const newPreset = SharedUtils.object.mergeKeysByRegex(preset1, preset2, VARIABLE.excludedKeyRegex);
Theme.setPreset(newPreset); Theme.setPreset(newPreset);

View File

@ -1,8 +1,7 @@
import Theme, { SharedUtils } from 'primevue/themes'; import Theme, { SharedUtils } from 'primevue/themes';
export default (preset) => { export default (preset) => {
const VARIABLE = Theme.defaults.variable; const newPreset = SharedUtils.object.mergeKeys(Theme.getPreset(), preset);
const newPreset = SharedUtils.object.mergeKeysByRegex(Theme.getPreset(), preset, VARIABLE.excludedKeyRegex);
Theme.setPreset(newPreset); Theme.setPreset(newPreset);

View File

@ -61,6 +61,19 @@ export default {
return mergedObj; return mergedObj;
}, },
mergeKeys(target = {}, source = {}) {
const mergedObj = { ...target };
Object.keys(source).forEach((key) => {
if (this.isObject(source[key]) && key in target && this.isObject(target[key])) {
mergedObj[key] = this.mergeKeys(target[key], source[key]);
} else {
mergedObj[key] = source[key];
}
});
return mergedObj;
},
getItemValue(obj, ...params) { getItemValue(obj, ...params) {
return this.isFunction(obj) ? obj(...params) : obj; return this.isFunction(obj) ? obj(...params) : obj;
}, },