Skip to content

Commit 5486d5e

Browse files
committed
refactor: call appyTheme after boot, when custom theme is used
1 parent 288fd1a commit 5486d5e

3 files changed

Lines changed: 38 additions & 5 deletions

File tree

packages/base/src/Boot.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import EventProvider from "./EventProvider.js";
33
import insertFontFace from "./FontFace.js";
44
import insertSystemCSSVars from "./SystemCSSVars.js";
55
import insertScrollbarStyles from "./ScrollbarStyles.js";
6-
import { getTheme } from "./config/Theme.js";
6+
import { getTheme, getBaseTheme } from "./config/Theme.js";
77
import applyTheme from "./theming/applyTheme.js";
88
import { registerCurrentRuntime } from "./Runtimes.js";
99
import { getFeature } from "./FeaturesRegistry.js";
@@ -88,8 +88,15 @@ const boot = async (): Promise<void> => {
8888
* @param { string } theme
8989
*/
9090
const onThemeRegistered = (theme: string) => {
91-
if (booted && theme === getTheme()) { // getTheme should only be called if "booted" is true
92-
applyTheme(getTheme());
91+
if (!booted) {
92+
return;
93+
}
94+
95+
const currentTheme = getTheme();
96+
const currentBaseTheme = getBaseTheme();
97+
98+
if (theme === currentTheme || theme === currentBaseTheme) {
99+
applyTheme(currentTheme);
93100
}
94101
};
95102

packages/base/src/config/Theme.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { boot, isBooted } from "../Boot.js";
77
import { attachConfigurationReset } from "./ConfigurationReset.js";
88

99
let curTheme: string | undefined;
10+
let curBaseTheme: string | undefined = undefined;
1011

1112
attachConfigurationReset(() => {
1213
curTheme = undefined;
@@ -91,11 +92,31 @@ const isLegacyThemeFamilyAsync = async () => {
9192

9293
const isKnownTheme = (theme: string) => SUPPORTED_THEMES.includes(theme);
9394

95+
/**
96+
* Returns the base theme of external theme.
97+
* @private
98+
* @returns {string | undefined} the base theme name
99+
*/
100+
const getBaseTheme = (): string | undefined => {
101+
return curBaseTheme;
102+
};
103+
104+
/**
105+
* Sets the base theme of the current external theme.
106+
* @param { string | undefined } theme the name of the new base theme
107+
* @private
108+
*/
109+
const setBaseTheme = (theme: string | undefined): void => {
110+
curBaseTheme = theme;
111+
};
112+
94113
export {
95114
getTheme,
96115
setTheme,
97116
isTheme,
98117
isLegacyThemeFamily,
99118
isLegacyThemeFamilyAsync,
100119
getDefaultTheme,
120+
getBaseTheme,
121+
setBaseTheme,
101122
};

packages/base/src/theming/applyTheme.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import getThemeDesignerTheme from "./getThemeDesignerTheme.js";
44
import { fireThemeLoaded } from "./ThemeLoaded.js";
55
import { getFeature } from "../FeaturesRegistry.js";
66
import { attachCustomThemeStylesToHead, getThemeRoot } from "../config/ThemeRoot.js";
7+
import { setBaseTheme } from "../config/Theme.js";
78
import type OpenUI5Support from "../features/OpenUI5Support.js";
89
import { DEFAULT_THEME } from "../generated/AssetParameters.js";
910
import { getCurrentRuntimeIndex } from "../Runtimes.js";
@@ -87,9 +88,13 @@ const applyTheme = async (theme: string) => {
8788
}
8889

8990
// Always load component packages properties. For non-registered themes, try with the base theme, if any
90-
const packagesTheme = isThemeRegistered(theme) ? theme : extTheme && extTheme.baseThemeName;
91-
await loadComponentPackages(packagesTheme || DEFAULT_THEME, extTheme && extTheme.themeName === theme ? theme : undefined);
91+
const externalThemeName = extTheme && extTheme.themeName === theme ? theme : undefined;
92+
const baseThemeName = extTheme && extTheme.baseThemeName;
93+
const effectiveThemeName = isThemeRegistered(theme) ? theme : (baseThemeName || DEFAULT_THEME);
9294

95+
await loadComponentPackages(effectiveThemeName, externalThemeName);
96+
97+
setBaseTheme(baseThemeName);
9398
fireThemeLoaded(theme);
9499
};
95100

0 commit comments

Comments
 (0)