Skip to content

Commit 4a672ea

Browse files
ilhan007NHristov-sap
authored andcommitted
fix(framework): apply theme after every theme props registration (#6718)
There are use-cases when the framework boots, but the theme has not been registered and applied. And, currently the framework won't apply a theme after boot finishes. With this PR the framework will apply theme properties always when theme registration occurs. when the theme properties are registered before boot, an event will be fired, notifying for the theme registration, but as the boot did not finish, the event will be disregarded and the theme will be applied as usual. when the theme properties are being registered after boot, an event will be fired, notifying for the theme registration and the registered styles will be applied. Fixes: #6666
1 parent 26af82d commit 4a672ea

File tree

7 files changed

+50
-24
lines changed

7 files changed

+50
-24
lines changed

packages/base/src/Boot.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getFeature } from "./FeaturesRegistry.js";
99
import type OpenUI5Support from "./features/OpenUI5Support.js";
1010
import type F6Navigation from "./features/F6Navigation.js";
1111
import { PromiseResolve } from "./types.js";
12+
import { attachThemeRegistered } from "./theming/ThemeRegistered.js";
1213

1314
let booted = false;
1415
let bootPromise: Promise<void>;
@@ -40,6 +41,8 @@ const boot = async (): Promise<void> => {
4041
return;
4142
}
4243

44+
attachThemeRegistered(onThemeRegistered);
45+
4346
registerCurrentRuntime();
4447

4548
const openUI5Support = getFeature<typeof OpenUI5Support>("OpenUI5Support");
@@ -70,6 +73,19 @@ const boot = async (): Promise<void> => {
7073
return bootPromise;
7174
};
7275

76+
/**
77+
* Callback, executed after theme properties registration
78+
* to apply the newly registered theme.
79+
* @private
80+
* @param { string } theme
81+
*/
82+
const onThemeRegistered = (theme: string) => {
83+
const currentTheme = getTheme();
84+
if (booted && theme === currentTheme) {
85+
applyTheme(currentTheme);
86+
}
87+
};
88+
7389
export {
7490
boot,
7591
attachBoot,

packages/base/src/asset-registries/Themes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DEFAULT_THEME } from "../generated/AssetParameters.js";
22
import { StyleData, StyleDataCSP } from "../types.js";
3+
import { fireThemeRegistered } from "../theming/ThemeRegistered.js";
34

45
type ThemeData = {_: StyleDataCSP } | StyleDataCSP | string;
56
type ThemeLoader = (themeName: string) => Promise<ThemeData>;
@@ -13,6 +14,7 @@ const registerThemePropertiesLoader = (packageName: string, themeName: string, l
1314
loaders.set(`${packageName}/${themeName}`, loader);
1415
registeredPackages.add(packageName);
1516
registeredThemes.add(themeName);
17+
fireThemeRegistered(themeName);
1618
};
1719

1820
const getThemeProperties = async (packageName: string, themeName: string) => {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import EventProvider from "../EventProvider.js";
2+
3+
type ThemeRegisteredCallback = (theme: string) => void;
4+
5+
const eventProvider = new EventProvider<string, void>();
6+
const THEME_REGISTERED = "themeRegistered";
7+
8+
const attachThemeRegistered = (listener: ThemeRegisteredCallback) => {
9+
eventProvider.attachEvent(THEME_REGISTERED, listener);
10+
};
11+
12+
const fireThemeRegistered = (theme: string) => {
13+
return eventProvider.fireEvent(THEME_REGISTERED, theme);
14+
};
15+
16+
export {
17+
attachThemeRegistered,
18+
fireThemeRegistered,
19+
};
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
11
import { attachBoot } from "../../dist/Boot.js";
22
import { boot } from "../../dist/Boot.js";
3+
import { registerThemePropertiesLoader } from "../../dist/AssetRegistry.js";
34

4-
// Call attachBoot early
5+
// attachBoot (no longer triggers "boot")
56
attachBoot(() => {
67
console.log("Listener1: after framework booted!")
78
})
89

9-
attachBoot(() => {
10-
console.log("Listener2: after framework booted!")
11-
})
12-
13-
attachBoot(() => {
14-
console.log("Listener3: after framework booted!")
15-
})
16-
import { registerThemePropertiesLoader } from "../../dist/AssetRegistry.js";
10+
// boot the framework
11+
boot();
1712

1813
const testAssets = {
19-
registerThemePropsAndBoot: () => {
14+
// registerThemePropertiesLoader after boot (and after attachBoot ), will call applyTheme
15+
registerThemeProps: async () => {
2016
registerThemePropertiesLoader("@ui5/webcomponents-theming", "sap_fiori_3", () => {
2117
return {
22-
content: `:root{ --var1: red; }`,
18+
content: `:root{ --customCol: #fff; --customBg: #000; }`,
2319
packageName: "",
2420
fileName: "",
2521
};
2622
});
27-
// call "boot" multiple times as if multiple web components start upgrading
28-
console.log("Booting...");
29-
boot();
30-
boot();
31-
boot();
32-
boot();
33-
boot();
34-
boot();
3523
},
3624
}
3725
window["sap-ui-webcomponents-bundle"] = testAssets;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.content {
2-
color: var(--var1);
2+
color: var(--customCol);
3+
background-color: var(--customBg);
34
}

packages/base/test/pages/Boot.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
</head>
1010
<body>
1111

12-
<div class="content">When styles are applied, this text shoul be red.</div>
12+
<div class="content">Register theme properties to style this text - window['sap-ui-webcomponents-bundle'].registerThemeProps()</div>
1313
</body>
1414
</html>

packages/base/test/specs/Boot.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ describe("Framework boot", async () => {
55
await browser.url("test/pages/Boot.html");
66
});
77

8-
it("Tests theme loading, when attachBoot is called before theme is registered", async () => {
8+
it("Tests theme loading, when registered after 'attachBoot' and 'boot'", async () => {
99
await browser.executeAsync(done => {
10-
window['sap-ui-webcomponents-bundle'].registerThemePropsAndBoot();
10+
window['sap-ui-webcomponents-bundle'].registerThemeProps();
1111
done();
1212
});
1313

1414
const styleElement = await browser.executeAsync(done => {
1515
return done(document.querySelector("head>style[data-ui5-theme-properties]"));
1616
});
1717

18-
assert.ok(styleElement, "style[data-ui5-theme-properties] tag is successfully created");
18+
assert.ok(styleElement, "style[data-ui5-theme-properties] tag is successfully created and theme applied.");
1919
});
2020
});

0 commit comments

Comments
 (0)