Skip to content

Commit 3173fb9

Browse files
authoredApr 10, 2020
feat(framework): register theme variables via CSS file (#1451)
1 parent f4d2547 commit 3173fb9

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed
 

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { fetchJsonOnce } from "../util/FetchHelper.js";
1+
import { fetchJsonOnce, fetchTextOnce } from "../util/FetchHelper.js";
22
import { DEFAULT_THEME } from "../generated/AssetParameters.js";
3+
import getFileExtension from "../util/getFileExtension.js";
34

45
const themeURLs = new Map();
56
const themeStyles = new Map();
@@ -16,13 +17,15 @@ const registeredThemes = new Set();
1617
* registerThemeProperties("my-package", "my_theme", ":root{--var1: red;}");
1718
* 2) Pass the CSS Vars as an object directly
1819
* registerThemeProperties("my-package", "my_theme", {"_": ":root{--var1: red;}"});
19-
* 3) Pass a URL to a JSON file, containing the CSS Vars in its "_" property. Will be fetched on demand, not upon registration.
20+
* 3) Pass a URL to a CSS file, containing the CSS Vars. Will be fetched on demand, not upon registration.
21+
* registerThemeProperties("my-package", "my_theme", "http://url/to/my/theme.css");
22+
* 4) Pass a URL to a JSON file, containing the CSS Vars in its "_" property. Will be fetched on demand, not upon registration.
2023
* registerThemeProperties("my-package", "my_theme", "http://url/to/my/theme.json");
2124
*
2225
* @public
2326
* @param packageName - the NPM package for which CSS Vars are registered
2427
* @param themeName - the theme which the CSS Vars implement
25-
* @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
28+
* @param style - can be one of four options: a string, an object with a "_" property, URL to a CSS file, or URL to a JSON file with a "_" property
2629
*/
2730
const registerThemeProperties = (packageName, themeName, style) => {
2831
if (style._) {
@@ -52,8 +55,10 @@ const getThemeProperties = async (packageName, themeName) => {
5255
}
5356

5457
const data = await fetchThemeProperties(packageName, themeName);
55-
themeStyles.set(`${packageName}_${themeName}`, data._);
56-
return data._;
58+
const themeProps = data._ || data;
59+
60+
themeStyles.set(`${packageName}_${themeName}`, themeProps);
61+
return themeProps;
5762
};
5863

5964
const fetchThemeProperties = async (packageName, themeName) => {
@@ -62,7 +67,8 @@ const fetchThemeProperties = async (packageName, themeName) => {
6267
if (!url) {
6368
throw new Error(`You have to import the ${packageName}/dist/Assets.js module to switch to additional themes`);
6469
}
65-
return fetchJsonOnce(url);
70+
71+
return getFileExtension(url) === ".css" ? fetchTextOnce(url) : fetchJsonOnce(url);
6672
};
6773

6874
const getRegisteredPackages = () => {

0 commit comments

Comments
 (0)
Please sign in to comment.