|
1 |
| -import { getThemeProperties, getRegisteredPackages } from "../asset-registries/Themes.js"; |
2 |
| -import { getExternalThemePresent } from "./ExternalThemePresent.js"; |
| 1 | +import { getThemeProperties, getRegisteredPackages, isThemeRegistered } from "../asset-registries/Themes.js"; |
3 | 2 | import createThemePropertiesStyleTag from "./createThemePropertiesStyleTag.js";
|
| 3 | +import getThemeDesignerTheme from "./getThemeDesignerTheme.js"; |
| 4 | +import { ponyfillNeeded, runPonyfill } from "./CSSVarsPonyfill.js"; |
| 5 | +import { getFeature } from "../FeaturesRegistry.js"; |
4 | 6 |
|
5 |
| -const applyTheme = async theme => { |
6 |
| - let cssText = ""; |
| 7 | +const BASE_THEME_PACKAGE = "@ui5/webcomponents-theme-base"; |
7 | 8 |
|
| 9 | +const isThemeBaseRegistered = () => { |
8 | 10 | const registeredPackages = getRegisteredPackages();
|
9 |
| - if (getExternalThemePresent()) { |
10 |
| - registeredPackages.delete("@ui5/webcomponents-theme-base"); |
| 11 | + return registeredPackages.has(BASE_THEME_PACKAGE); |
| 12 | +}; |
| 13 | + |
| 14 | +const loadThemeBase = async theme => { |
| 15 | + if (!isThemeBaseRegistered()) { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + const cssText = await getThemeProperties(BASE_THEME_PACKAGE, theme); |
| 20 | + createThemePropertiesStyleTag(cssText, BASE_THEME_PACKAGE); |
| 21 | +}; |
| 22 | + |
| 23 | +const deleteThemeBase = () => { |
| 24 | + const styleElement = document.head.querySelector(`style[data-ui5-theme-properties="${BASE_THEME_PACKAGE}"]`); |
| 25 | + if (styleElement) { |
| 26 | + styleElement.parentElement.removeChild(styleElement); |
11 | 27 | }
|
| 28 | +}; |
12 | 29 |
|
| 30 | +const loadComponentPackages = async theme => { |
| 31 | + const registeredPackages = getRegisteredPackages(); |
13 | 32 | registeredPackages.forEach(async packageName => {
|
14 |
| - cssText = await getThemeProperties(packageName, theme); |
| 33 | + if (packageName === BASE_THEME_PACKAGE) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const cssText = await getThemeProperties(packageName, theme); |
15 | 38 | createThemePropertiesStyleTag(cssText, packageName);
|
16 | 39 | });
|
17 | 40 | };
|
18 | 41 |
|
| 42 | +const detectExternalTheme = () => { |
| 43 | + // If theme designer theme is detected, use this |
| 44 | + const extTheme = getThemeDesignerTheme(); |
| 45 | + if (extTheme) { |
| 46 | + return extTheme; |
| 47 | + } |
| 48 | + |
| 49 | + // If OpenUI5Support is enabled, try to find out if it loaded variables |
| 50 | + const OpenUI5Support = getFeature("OpenUI5Support"); |
| 51 | + if (OpenUI5Support) { |
| 52 | + const varsLoaded = OpenUI5Support.cssVariablesLoaded(); |
| 53 | + if (varsLoaded) { |
| 54 | + return { |
| 55 | + themeName: OpenUI5Support.getConfigurationSettingsObject().theme, // just themeName, baseThemeName is only relevant for custom themes |
| 56 | + }; |
| 57 | + } |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +const applyTheme = async theme => { |
| 62 | + const extTheme = detectExternalTheme(); |
| 63 | + |
| 64 | + // Only load theme_base properties if there is no externally loaded theme, or there is, but it is not being loaded |
| 65 | + if (!extTheme || theme !== extTheme.themeName) { |
| 66 | + await loadThemeBase(theme); |
| 67 | + } else { |
| 68 | + deleteThemeBase(); |
| 69 | + } |
| 70 | + |
| 71 | + // Always load component packages properties. For non-registered themes, try with the base theme, if any |
| 72 | + const packagesTheme = isThemeRegistered(theme) ? theme : extTheme && extTheme.baseThemeName; |
| 73 | + await loadComponentPackages(packagesTheme); |
| 74 | + |
| 75 | + // When changing the theme, run the ponyfill immediately |
| 76 | + if (ponyfillNeeded()) { |
| 77 | + runPonyfill(); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
19 | 81 | export default applyTheme;
|
0 commit comments