UNPKG

4.96 kBJavaScriptView Raw
1define(["require", "exports", "tslib", "@uifabric/utilities", "@microsoft/load-themed-styles", "@fluentui/theme/lib/createTheme", "@fluentui/theme/lib/createTheme"], function (require, exports, tslib_1, utilities_1, load_themed_styles_1, createTheme_1, createTheme_2) {
2 "use strict";
3 Object.defineProperty(exports, "__esModule", { value: true });
4 exports.createTheme = createTheme_2.createTheme;
5 var _theme = createTheme_1.createTheme({});
6 var _onThemeChangeCallbacks = [];
7 exports.ThemeSettingName = 'theme';
8 function initializeThemeInCustomizations() {
9 var _a;
10 var _b, _c, _d, _e;
11 // eslint-disable-next-line @typescript-eslint/no-explicit-any
12 var win = utilities_1.getWindow();
13 if ((_c = (_b = win) === null || _b === void 0 ? void 0 : _b.FabricConfig) === null || _c === void 0 ? void 0 : _c.legacyTheme) {
14 // does everything the `else` clause does and more, such as invoke legacy theming
15 loadTheme(win.FabricConfig.legacyTheme);
16 }
17 else if (!utilities_1.Customizations.getSettings([exports.ThemeSettingName]).theme) {
18 if ((_e = (_d = win) === null || _d === void 0 ? void 0 : _d.FabricConfig) === null || _e === void 0 ? void 0 : _e.theme) {
19 _theme = createTheme_1.createTheme(win.FabricConfig.theme);
20 }
21 // Set the default theme.
22 utilities_1.Customizations.applySettings((_a = {}, _a[exports.ThemeSettingName] = _theme, _a));
23 }
24 }
25 exports.initializeThemeInCustomizations = initializeThemeInCustomizations;
26 initializeThemeInCustomizations();
27 /**
28 * Gets the theme object
29 * @param depComments - Whether to include deprecated tags as comments for deprecated slots.
30 */
31 function getTheme(depComments) {
32 if (depComments === void 0) { depComments = false; }
33 if (depComments === true) {
34 _theme = createTheme_1.createTheme({}, depComments);
35 }
36 return _theme;
37 }
38 exports.getTheme = getTheme;
39 /**
40 * Registers a callback that gets called whenever the theme changes.
41 * This should only be used when the component cannot automatically get theme changes through its state.
42 * This will not register duplicate callbacks.
43 */
44 function registerOnThemeChangeCallback(callback) {
45 if (_onThemeChangeCallbacks.indexOf(callback) === -1) {
46 _onThemeChangeCallbacks.push(callback);
47 }
48 }
49 exports.registerOnThemeChangeCallback = registerOnThemeChangeCallback;
50 /**
51 * See registerOnThemeChangeCallback().
52 * Removes previously registered callbacks.
53 */
54 function removeOnThemeChangeCallback(callback) {
55 var i = _onThemeChangeCallbacks.indexOf(callback);
56 if (i === -1) {
57 return;
58 }
59 _onThemeChangeCallbacks.splice(i, 1);
60 }
61 exports.removeOnThemeChangeCallback = removeOnThemeChangeCallback;
62 /**
63 * Applies the theme, while filling in missing slots.
64 * @param theme - Partial theme object.
65 * @param depComments - Whether to include deprecated tags as comments for deprecated slots.
66 */
67 function loadTheme(theme, depComments) {
68 var _a;
69 if (depComments === void 0) { depComments = false; }
70 _theme = createTheme_1.createTheme(theme, depComments);
71 // Invoke the legacy method of theming the page as well.
72 load_themed_styles_1.loadTheme(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, _theme.palette), _theme.semanticColors), _theme.effects), _loadFonts(_theme)));
73 utilities_1.Customizations.applySettings((_a = {}, _a[exports.ThemeSettingName] = _theme, _a));
74 _onThemeChangeCallbacks.forEach(function (callback) {
75 try {
76 callback(_theme);
77 }
78 catch (e) {
79 // don't let a bad callback break everything else
80 }
81 });
82 return _theme;
83 }
84 exports.loadTheme = loadTheme;
85 /**
86 * Loads font variables into a JSON object.
87 * @param theme - The theme object
88 */
89 function _loadFonts(theme) {
90 var lines = {};
91 for (var _i = 0, _a = Object.keys(theme.fonts); _i < _a.length; _i++) {
92 var fontName = _a[_i];
93 var font = theme.fonts[fontName];
94 for (var _b = 0, _c = Object.keys(font); _b < _c.length; _b++) {
95 var propName = _c[_b];
96 var name_1 = fontName + propName.charAt(0).toUpperCase() + propName.slice(1);
97 var value = font[propName];
98 if (propName === 'fontSize' && typeof value === 'number') {
99 // if it's a number, convert it to px by default like our theming system does
100 value = value + 'px';
101 }
102 lines[name_1] = value;
103 }
104 }
105 return lines;
106 }
107});
108//# sourceMappingURL=theme.js.map
\No newline at end of file