-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathapplyTheme.ts
More file actions
82 lines (66 loc) · 2.85 KB
/
applyTheme.ts
File metadata and controls
82 lines (66 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { getThemeProperties, getRegisteredPackages, isThemeRegistered } from "../asset-registries/Themes.js";
import { createOrUpdateStyle } from "../ManagedStyles.js";
import getThemeDesignerTheme from "./getThemeDesignerTheme.js";
import { fireThemeLoaded } from "./ThemeLoaded.js";
import { attachCustomThemeStylesToHead, getThemeRoot } from "../config/ThemeRoot.js";
import { DEFAULT_THEME } from "../generated/AssetParameters.js";
import { getCurrentRuntimeIndex } from "../Runtimes.js";
// eslint-disable-next-line
export let _lib = "ui5";
// eslint-disable-next-line
export let _package = "webcomponents-theming";
// eslint-disable-next-line
const BASE_THEME_PACKAGE = "@" + _lib + "/" + _package;
const isThemeBaseRegistered = () => {
const registeredPackages = getRegisteredPackages();
return registeredPackages.has(BASE_THEME_PACKAGE);
};
const loadThemeBase = async (theme: string) => {
if (!isThemeBaseRegistered()) {
return;
}
const cssData = await getThemeProperties(BASE_THEME_PACKAGE, theme);
if (cssData) {
createOrUpdateStyle(cssData, "data-ui5-theme-properties", BASE_THEME_PACKAGE, theme);
}
};
const loadComponentPackages = async (theme: string, externalThemeName?: string) => {
const registeredPackages = getRegisteredPackages();
const packagesStylesPromises = [...registeredPackages].map(async packageName => {
if (packageName === BASE_THEME_PACKAGE) {
return;
}
const cssData = await getThemeProperties(packageName, theme, externalThemeName);
if (cssData) {
createOrUpdateStyle(cssData, `data-ui5-component-properties-${getCurrentRuntimeIndex()}`, packageName);
}
});
return Promise.all(packagesStylesPromises);
};
const detectExternalTheme = async (theme: string) => {
if (getThemeRoot()) {
await attachCustomThemeStylesToHead(theme);
}
// If theme designer theme is detected, use this
const extTheme = getThemeDesignerTheme();
if (extTheme) {
return extTheme;
}
};
const applyTheme = async (theme: string) => {
// Detect external theme if available (e.g., from theme designer or custom theme root)
const extTheme = await detectExternalTheme(theme);
// Determine which theme to use for component packages:
// 1. If the requested theme is registered, use it directly
// 2. If external theme exists, use its base theme (e.g., "my_custom_theme" extends "sap_fiori_3")
// 3. Otherwise, fallback to the default theme
const packagesTheme = isThemeRegistered(theme) ? theme : extTheme && extTheme.baseThemeName;
const effectiveTheme = packagesTheme || DEFAULT_THEME;
// Load base theme properties
await loadThemeBase(effectiveTheme);
// Load component-specific theme properties
// Pass external theme name only if it matches the requested theme to avoid conflicts
await loadComponentPackages(effectiveTheme, extTheme && extTheme.themeName === theme ? theme : undefined);
fireThemeLoaded(theme);
};
export default applyTheme;