Skip to content

Commit 6d477c6

Browse files
author
Simon Milfred
committed
fix: better handling of theme classes to only apply what they need to apply
1 parent e168f64 commit 6d477c6

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

.playground/app.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const themeConfiguration = ref(null);
2323
const testSubset = getThemeConfiguration('default', {
2424
[/colors/i]: /^on[A-Z]/,
2525
});
26-
console.log(testSubset);
27-
console.log(getThemeConfigurations());
26+
console.log('subset', testSubset);
27+
console.log('getThemeConfigurations', getThemeConfigurations());
2828
</script>
2929

3030
<style>

components/ThemeConfiguration/ThemeConfiguration.vue

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,27 @@ import {
1717
1818
const props = defineProps({
1919
config: [String, Object],
20+
21+
// Add specific media queries with their own theme configuration.
2022
media: Object,
23+
24+
// If true, theme classes will be generated for each theme
25+
// configuration, which can be used to apply different themes
26+
// to different parts of the page. If an array is provided,
27+
// only theme classes for the specified themes will be generated.
2128
useThemeClasses: [Boolean, Array],
29+
30+
// If each theme class should be a "hard reset" to the current
31+
// theme + the theme class, instead of being able to cascade on
32+
// top of each other. This is useful to prevent unexpected
33+
// results when using multiple theme classes together, but also
34+
// causes more CSS to be generated.
35+
mergeThemeClassesWithBaseConfig: {
36+
type: Boolean,
37+
default: false,
38+
},
39+
40+
// Apply the generated CSS in a CSS layer.
2241
cssLayer: String,
2342
});
2443
@@ -53,18 +72,24 @@ const compConfig = computed(() => {
5372
return clone;
5473
});
5574
56-
const classBaseConfig = computed(() => {
57-
if (props.useThemeClasses && Array.isArray(props.useThemeClasses)) {
58-
return deepMergeExisting(
59-
props.useThemeClasses.reduce((obj, key) => {
60-
Object.assign(obj, availableConfigs[key]);
61-
}, {}),
62-
compConfig.value
63-
);
64-
}
65-
66-
return compConfig.value;
67-
});
75+
// // CAUSING ISSUES, REMOVED – REMOVE PROPERLY LATER
76+
// const classBaseConfig = computed(() => {
77+
// if (props.useThemeClasses) {
78+
// const themeClassArray = Array.isArray(props.useThemeClasses)
79+
// ? props.useThemeClasses
80+
// : Object.keys(availableConfigs);
81+
// return deepMergeExisting(
82+
// themeClassArray.reduce((obj, key) => {
83+
// if (availableConfigs?.[key]) {
84+
// Object.assign(obj, availableConfigs[key]);
85+
// }
86+
// return obj;
87+
// }, {}),
88+
// defaultConfig
89+
// );
90+
// }
91+
// return compConfig.value;
92+
// });
6893
6994
/* Compile css text */
7095
const cssText = computed(() => {
@@ -75,15 +100,19 @@ const cssText = computed(() => {
75100
if (props.config === key) continue;
76101
if (!props.config && key === 'default') continue;
77102
if (
78-
Array.isArray(props.useThemeClasses) &&
79-
!props.useThemeClasses.includes(key)
80-
)
81-
continue;
103+
Array.isArray(props.useThemeClasses)
104+
) {
105+
if (!props.useThemeClasses.includes(key)) {
106+
continue;
107+
}
108+
}
82109
83110
rules.push(
84111
makeCssText(
85112
`.u-theme-${key}`,
86-
deepMergeExisting({ ...classBaseConfig.value }, value)
113+
props.mergeThemeClassesWithBaseConfig
114+
? deepMergeExisting(cloneDeep(compConfig.value), value)
115+
: value
87116
)
88117
);
89118
}
@@ -805,6 +834,9 @@ function deepMergeExisting(target, source) {
805834
}
806835
}
807836
}
837+
838+
// Also return the target in the end for easier chaining
839+
return target;
808840
}
809841
810842
watch(compConfig, (value) => (observedData.value = value), {

0 commit comments

Comments
 (0)