UNPKG

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