UNPKG

@uifabric/styling

Version:

Styling helpers for Fluent UI React.

104 lines 4.1 kB
import { __assign } from "tslib"; import { Customizations, getWindow } from '@uifabric/utilities'; import { loadTheme as legacyLoadTheme } from '@microsoft/load-themed-styles'; import { createTheme } from '@fluentui/theme/lib/createTheme'; export { createTheme } from '@fluentui/theme/lib/createTheme'; var _theme = createTheme({}); var _onThemeChangeCallbacks = []; export var ThemeSettingName = 'theme'; export function initializeThemeInCustomizations() { var _a; var _b, _c, _d, _e; // eslint-disable-next-line @typescript-eslint/no-explicit-any var win = getWindow(); if ((_c = (_b = win) === null || _b === void 0 ? void 0 : _b.FabricConfig) === null || _c === void 0 ? void 0 : _c.legacyTheme) { // does everything the `else` clause does and more, such as invoke legacy theming loadTheme(win.FabricConfig.legacyTheme); } else if (!Customizations.getSettings([ThemeSettingName]).theme) { if ((_e = (_d = win) === null || _d === void 0 ? void 0 : _d.FabricConfig) === null || _e === void 0 ? void 0 : _e.theme) { _theme = createTheme(win.FabricConfig.theme); } // Set the default theme. Customizations.applySettings((_a = {}, _a[ThemeSettingName] = _theme, _a)); } } // Ensure theme is initialized when this package is referenced. initializeThemeInCustomizations(); /** * Gets the theme object * @param depComments - Whether to include deprecated tags as comments for deprecated slots. */ export function getTheme(depComments) { if (depComments === void 0) { depComments = false; } if (depComments === true) { _theme = createTheme({}, depComments); } return _theme; } /** * Registers a callback that gets called whenever the theme changes. * This should only be used when the component cannot automatically get theme changes through its state. * This will not register duplicate callbacks. */ export function registerOnThemeChangeCallback(callback) { if (_onThemeChangeCallbacks.indexOf(callback) === -1) { _onThemeChangeCallbacks.push(callback); } } /** * See registerOnThemeChangeCallback(). * Removes previously registered callbacks. */ export function removeOnThemeChangeCallback(callback) { var i = _onThemeChangeCallbacks.indexOf(callback); if (i === -1) { return; } _onThemeChangeCallbacks.splice(i, 1); } /** * Applies the theme, while filling in missing slots. * @param theme - Partial theme object. * @param depComments - Whether to include deprecated tags as comments for deprecated slots. */ export function loadTheme(theme, depComments) { var _a; if (depComments === void 0) { depComments = false; } _theme = createTheme(theme, depComments); // Invoke the legacy method of theming the page as well. legacyLoadTheme(__assign(__assign(__assign(__assign({}, _theme.palette), _theme.semanticColors), _theme.effects), _loadFonts(_theme))); Customizations.applySettings((_a = {}, _a[ThemeSettingName] = _theme, _a)); _onThemeChangeCallbacks.forEach(function (callback) { try { callback(_theme); } catch (e) { // don't let a bad callback break everything else } }); return _theme; } /** * Loads font variables into a JSON object. * @param theme - The theme object */ function _loadFonts(theme) { var lines = {}; for (var _i = 0, _a = Object.keys(theme.fonts); _i < _a.length; _i++) { var fontName = _a[_i]; var font = theme.fonts[fontName]; for (var _b = 0, _c = Object.keys(font); _b < _c.length; _b++) { var propName = _c[_b]; var name_1 = fontName + propName.charAt(0).toUpperCase() + propName.slice(1); var value = font[propName]; if (propName === 'fontSize' && typeof value === 'number') { // if it's a number, convert it to px by default like our theming system does value = value + 'px'; } lines[name_1] = value; } } return lines; } //# sourceMappingURL=theme.js.map