Skip to content

Commit 6a69521

Browse files
authored
feat(framework): Allow the registration of custom themes (#1109)
1 parent cf90f82 commit 6a69521

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

docs/Public Module Imports.md

+23
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,26 @@ import { getFirstDayOfWeek } from "@ui5/webcomponents-base/dist/config/FormatSet
318318
```
319319

320320
For more details, please check [Configuration](Configuration.md)
321+
322+
### 6. Assets registration
323+
324+
In order to register a **custom theme**:
325+
326+
```js
327+
import { registerThemeProperties } from "@ui5/webcomponents-base/dist/AssetRegistry.js"
328+
```
329+
330+
and then call the method above to register CSS Variables for each theme/package pair.
331+
332+
You can pass the parameters directly, as an object, or as a URL:
333+
1) Pass the CSS Vars as a string directly.
334+
335+
`registerThemeProperties("my-package", "my_theme", ":root{--var1: red;}");`
336+
337+
2) Pass the CSS Vars as an object directly. The object must have a "_" property, pointing to a string with the CSS Vars.
338+
339+
`registerThemeProperties("my-package", "my_theme", {"_": ":root{--var1: red;}"});`
340+
341+
3) Pass a URL to a JSON file, containing the CSS Vars in its "_" property. Will be fetched on demand, not upon registration.
342+
343+
`registerThemeProperties("my-package", "my_theme", "http://url/to/my/theme.json");`

packages/base/bundle.esm.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { registerThemeProperties } from "./dist/AssetRegistry.js";
2+
13
import "./dist/features/calendar/Buddhist.js";
24
import "./dist/features/calendar/Islamic.js";
35
import "./dist/features/calendar/Japanese.js";
@@ -14,7 +16,7 @@ import "./dist/test-resources/elements/Child.js";
1416
import "./dist/test-resources/elements/DensityAware.js";
1517
import "./dist/test-resources/elements/GenericExt.js";
1618

17-
// Test themes
19+
// Test themes - CSS Vars for the sap_fiori_3, sap_fiori_3_dark, sap_belize and sap_belize_hcb themes
1820
import "./dist/test-resources/assets/Themes.js";
1921

2022
// used in test pages
@@ -23,6 +25,9 @@ window.RenderScheduler = RenderScheduler;
2325
import { isIE } from "./dist/Device.js";
2426
window.isIE = isIE; // attached to the window object for testing purposes
2527

28+
// used for tests - to register a custom theme
29+
window.registerThemeProperties = registerThemeProperties;
30+
2631
// Note: keep in sync with rollup.config value for IIFE
2732
import { getAnimationMode } from "./dist/config/AnimationMode.js";
2833
import { getLanguage } from "./dist/config/Language.js";

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

+23-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@ import { fetchJsonOnce } from "../util/FetchHelper.js";
33
const themeURLs = new Map();
44
const themeStyles = new Map();
55
const registeredPackages = new Set();
6-
const SUPPORTED_THEMES = ["sap_fiori_3", "sap_fiori_3_dark", "sap_belize", "sap_belize_hcb"];
6+
const registeredThemes = new Set();
77

8+
/**
9+
* Used to provide CSS Vars for a specific theme for a specific package.
10+
* The CSS Vars can be passed directly as a string (containing them), as an object with a "_" property(containing them in the "_" property), or as a URL.
11+
* This URL must point to a JSON file, containing a "_" property.
12+
*
13+
* Example usage:
14+
* 1) Pass the CSS Vars as a string directly.
15+
* registerThemeProperties("my-package", "my_theme", ":root{--var1: red;}");
16+
* 2) Pass the CSS Vars as an object directly
17+
* registerThemeProperties("my-package", "my_theme", {"_": ":root{--var1: red;}"});
18+
* 3) Pass a URL to a JSON file, containing the CSS Vars in its "_" property. Will be fetched on demand, not upon registration.
19+
* registerThemeProperties("my-package", "my_theme", "http://url/to/my/theme.json");
20+
*
21+
* @public
22+
* @param packageName - the NPM package for which CSS Vars are registered
23+
* @param themeName - the theme which the CSS Vars implement
24+
* @param style - can be one of three options: a string, an object with a "_" property or a URL to a JSON file with a "_" property
25+
*/
826
const registerThemeProperties = (packageName, themeName, style) => {
927
if (style._) {
1028
// JSON object like ({"_": ":root"})
@@ -17,6 +35,7 @@ const registerThemeProperties = (packageName, themeName, style) => {
1735
themeURLs.set(`${packageName}_${themeName}`, style);
1836
}
1937
registeredPackages.add(packageName);
38+
registeredThemes.add(themeName);
2039
};
2140

2241
const getThemeProperties = async (packageName, themeName) => {
@@ -25,8 +44,9 @@ const getThemeProperties = async (packageName, themeName) => {
2544
return style;
2645
}
2746

28-
if (!SUPPORTED_THEMES.includes(themeName)) {
29-
console.warn(`You have requested non-existing theme - falling back to sap_fiori_3. The supported themes are: ${SUPPORTED_THEMES.join(", ")}.`); /* eslint-disable-line */
47+
if (!registeredThemes.has(themeName)) {
48+
const regThemesStr = [...registeredThemes.values()].join(", ");
49+
console.warn(`You have requested a non-registered theme - falling back to sap_fiori_3. Registered themes are: ${regThemesStr}`); /* eslint-disable-line */
3050
return themeStyles.get(`${packageName}_sap_fiori_3`);
3151
}
3252

packages/base/test/elements/Generic.js

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Generic extends UI5Element {
6868
return `:host {
6969
display: inline-block;
7070
border: 1px solid black;
71+
color: var(--var1);
7172
}`;
7273
}
7374

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const assert = require("chai").assert;
2+
3+
describe("Custom themes can be registered", () => {
4+
browser.url("http://localhost:9191/test-resources/pages/AllTestElements.html");
5+
6+
it("Tests that theme parameters are changed on theme change", () => {
7+
const newTheme = 'my_custom_theme';
8+
9+
const res = browser.executeAsync( async (newTheme, done) => {
10+
const var1 = "--var1: #555555";
11+
12+
window.registerThemeProperties("@ui5/webcomponents-base-test", newTheme, `:root{ ${var1}; }`);
13+
14+
const config = window['sap-ui-webcomponents-bundle'].configuration;
15+
await config.setTheme(newTheme);
16+
17+
const style = document.querySelector(`style[data-ui5-theme-properties="@ui5/webcomponents-base-test"]`);
18+
const varsFound = style && style.textContent.includes(var1);
19+
return done(varsFound);
20+
}, newTheme);
21+
22+
assert.strictEqual(res, true, "Theme parameters changed");
23+
});
24+
25+
});

0 commit comments

Comments
 (0)