Skip to content

Commit 7f84b83

Browse files
authored
feat(framework): configure default icon collection per theme (#5031)
1 parent b06d608 commit 7f84b83

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

packages/base/hash.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1xd6xbRotmYM0O01qxhGL8Fk17E=
1+
DkQ0iqK3nl9zgm3odHFLssq0yeA=

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import getSharedResource from "../getSharedResource.js";
22
import IconCollectionsAlias from "../assets-meta/IconCollectionsAlias.js";
3-
import { isThemeFamily } from "../config/Theme.js";
3+
import { getEffectiveDefaultIconCollection } from "../config/Icons.js";
44

55
const loaders = new Map();
66
const registry = getSharedResource("SVGIcons.registry", new Map());
@@ -48,7 +48,7 @@ const _fillRegistry = bundleData => {
4848
// set
4949
const registerIcon = (name, { pathData, ltr, accData, collection, packageName } = {}) => { // eslint-disable-line
5050
if (!collection) {
51-
collection = _getDefaultCollection();
51+
collection = getEffectiveDefaultIconCollection();
5252
}
5353

5454
const key = `${collection}/${name}`;
@@ -68,7 +68,7 @@ const _parseName = name => {
6868

6969
let collection;
7070
[name, collection] = name.split("/").reverse();
71-
collection = collection || _getDefaultCollection();
71+
collection = collection || getEffectiveDefaultIconCollection();
7272

7373
// Normalize collection name.
7474
// - resolve `SAP-icons-TNT` to `tnt`.
@@ -117,10 +117,6 @@ const _getRegisteredNames = async () => {
117117
return Array.from(registry.keys());
118118
};
119119

120-
const _getDefaultCollection = () => {
121-
return isThemeFamily("sap_horizon") ? "SAP-icons-v5" : "SAP-icons";
122-
};
123-
124120
const _normalizeCollection = collectionName => {
125121
if (IconCollectionsAlias[collectionName]) {
126122
return IconCollectionsAlias[collectionName];

packages/base/src/config/Icons.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { getTheme, isThemeFamily } from "./Theme.js";
2+
3+
const IconCollectionConfiguration = new Map();
4+
5+
/**
6+
* Sets the default icon collection (v4 or v5) per theme,
7+
* which will be applied in case icon collection is not specified.
8+
*
9+
* Note: by default SAP-icons-v5 is used in SAP Horizon and SAP-icons-v4 for all the rest.
10+
* @param {String} theme
11+
* @param {String} collectionName
12+
*/
13+
const setDefaultIconCollection = (theme, collectionName) => {
14+
if (collectionName === "horizon") {
15+
collectionName = "SAP-icons-v5";
16+
}
17+
18+
IconCollectionConfiguration.set(theme, collectionName);
19+
};
20+
21+
/**
22+
* Returns the default icon collection (v4 or v5) for given theme,
23+
* that is configured.
24+
*
25+
* @param {String} theme
26+
* @returns {String}
27+
*/
28+
const getDefaultIconCollection = theme => {
29+
return IconCollectionConfiguration.get(theme);
30+
};
31+
32+
/**
33+
* Returns the effective icon collection that will be applied for icon web components
34+
* whenever namespace is not specified.
35+
* @returns {String}
36+
*/
37+
const getEffectiveDefaultIconCollection = () => {
38+
const currentTheme = getTheme();
39+
const currentThemeConfiguration = IconCollectionConfiguration.get(currentTheme);
40+
41+
if (currentThemeConfiguration) {
42+
return currentThemeConfiguration;
43+
}
44+
45+
return isThemeFamily("sap_horizon") ? "SAP-icons-v5" : "SAP-icons";
46+
};
47+
48+
export {
49+
setDefaultIconCollection,
50+
getDefaultIconCollection,
51+
getEffectiveDefaultIconCollection,
52+
};

packages/main/bundle.common.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ const testAssets = {
141141
defaultTexts,
142142
};
143143

144+
// The SAP Icons V4 icon collection is set by default in sap_fiori_3,
145+
// but it's configurable:
146+
// import { setDefaultIconCollection } from "@ui5/webcomponents-base/dist/config/Icons.js";
147+
// setDefaultIconCollection("sap_fiori_3", "SAP-icons-v5");
148+
// or
149+
// setDefaultIconCollection("sap_fiori_3", "horizon");
150+
// or for custom theme
151+
// setDefaultIconCollection("my_custom_theme", "SAP-icons-v5");
152+
144153
window["sap-ui-webcomponents-bundle"] = testAssets;
145154

146155
export default testAssets;

0 commit comments

Comments
 (0)