UNPKG

@atlaskit/tokens

Version:

Design tokens are the single source of truth to name and store design decisions.

201 lines (200 loc) 11.8 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } import _regeneratorRuntime from "@babel/runtime/regenerator"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { fg } from '@atlaskit/platform-feature-flags'; import getGlobalTheme from './get-global-theme'; import { themeStateDefaults } from './theme-config'; import configurePage from './utils/configure-page'; import { findMissingCustomStyleElements } from './utils/custom-theme-loading-utils'; import { getThemeOverridePreferences } from './utils/get-theme-override-preferences'; import { getThemePreferences } from './utils/get-theme-preferences'; import { isValidBrandHex } from './utils/is-valid-brand-hex'; import { loadAndAppendThemeCss } from './utils/theme-loading'; /** * Sets the theme globally at runtime. This updates the `data-theme` and `data-color-mode` attributes on your page's <html> tag. * * @param {Object<string, string>} themeState The themes and color mode that should be applied. * @param {string} themeState.colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting. * @param {string} themeState.contrastMode The contrast mode theme to be applied. If set to `auto`, the theme applied will be determined by the OS setting.set to `auto`, the theme applied will be determined by the OS setting. * @param {string} themeState.dark The color theme to be applied when the color mode resolves to 'dark'. * @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'. * @param {string} themeState.motion The motion theme to be applied. * @param {string} themeState.shape The shape theme to be applied. * @param {string} themeState.spacing The spacing theme to be applied. * @param {string} themeState.typography The typography theme to be applied. * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation * @param {function} themeLoader Callback function used to override the default theme loading functionality. * * @returns A Promise of an unbind function, that can be used to stop listening for changes to system theme. * * @example * ``` * setGlobalTheme({colorMode: 'auto', light: 'light', dark: 'dark', spacing: 'spacing'}); * ``` */ var setGlobalTheme = /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() { var nextThemeState, themeLoader, _ref2, _ref2$colorMode, colorMode, _ref2$contrastMode, contrastMode, _ref2$dark, dark, _ref2$light, light, _ref2$shape, shape, _ref2$spacing, spacing, _ref2$typography, typography, _ref2$motion, motion, _ref2$UNSAFE_themeOpt, UNSAFE_themeOptions, themeState, themePreferences, loadingStrategy, loadingTasks, mode, attrOfMissingCustomStyles, themeOverridePreferences, _iterator, _step, themeId, autoUnbind, _args3 = arguments, _t; return _regeneratorRuntime.wrap(function (_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: nextThemeState = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {}; themeLoader = _args3.length > 1 ? _args3[1] : undefined; _ref2 = typeof nextThemeState === 'function' ? nextThemeState(_objectSpread(_objectSpread({}, themeStateDefaults), {}, { typography: themeStateDefaults['typography'], shape: themeStateDefaults['shape'](), motion: themeStateDefaults['motion']() }, getGlobalTheme())) : nextThemeState, _ref2$colorMode = _ref2.colorMode, colorMode = _ref2$colorMode === void 0 ? themeStateDefaults['colorMode'] : _ref2$colorMode, _ref2$contrastMode = _ref2.contrastMode, contrastMode = _ref2$contrastMode === void 0 ? themeStateDefaults['contrastMode'] : _ref2$contrastMode, _ref2$dark = _ref2.dark, dark = _ref2$dark === void 0 ? themeStateDefaults['dark'] : _ref2$dark, _ref2$light = _ref2.light, light = _ref2$light === void 0 ? themeStateDefaults['light'] : _ref2$light, _ref2$shape = _ref2.shape, shape = _ref2$shape === void 0 ? themeStateDefaults['shape']() : _ref2$shape, _ref2$spacing = _ref2.spacing, spacing = _ref2$spacing === void 0 ? themeStateDefaults['spacing'] : _ref2$spacing, _ref2$typography = _ref2.typography, typography = _ref2$typography === void 0 ? themeStateDefaults['typography'] : _ref2$typography, _ref2$motion = _ref2.motion, motion = _ref2$motion === void 0 ? themeStateDefaults['motion']() : _ref2$motion, _ref2$UNSAFE_themeOpt = _ref2.UNSAFE_themeOptions, UNSAFE_themeOptions = _ref2$UNSAFE_themeOpt === void 0 ? themeStateDefaults['UNSAFE_themeOptions'] : _ref2$UNSAFE_themeOpt; // CLEANUP: Remove. This blocks application of increased contrast themes // without the feature flag enabled. if (!fg('platform_increased-contrast-themes')) { if (light === 'light-increased-contrast') { light = 'light'; } if (dark === 'dark-increased-contrast') { dark = 'dark'; } } themeState = { colorMode: colorMode, contrastMode: contrastMode, dark: dark, light: light, shape: shape, spacing: spacing, typography: typography, motion: motion, UNSAFE_themeOptions: themeLoader ? undefined : UNSAFE_themeOptions }; // Determine what to load and loading strategy themePreferences = getThemePreferences(themeState); loadingStrategy = themeLoader ? themeLoader : loadAndAppendThemeCss; // Load standard themes loadingTasks = themePreferences.map( /*#__PURE__*/function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(themeId) { return _regeneratorRuntime.wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 1; return loadingStrategy(themeId); case 1: return _context.abrupt("return", _context.sent); case 2: case "end": return _context.stop(); } }, _callee); })); return function (_x) { return _ref3.apply(this, arguments); }; }()); // Load custom themes if needed if (!themeLoader && UNSAFE_themeOptions && isValidBrandHex(UNSAFE_themeOptions === null || UNSAFE_themeOptions === void 0 ? void 0 : UNSAFE_themeOptions.brandColor)) { mode = colorMode || themeStateDefaults['colorMode']; attrOfMissingCustomStyles = findMissingCustomStyleElements(UNSAFE_themeOptions, mode); if (attrOfMissingCustomStyles.length > 0) { // Load custom theme styles loadingTasks.push(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() { var _yield$import, loadAndAppendCustomThemeCss; return _regeneratorRuntime.wrap(function (_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 1; return import( /* webpackChunkName: "@atlaskit-internal_atlassian-custom-theme" */ './custom-theme'); case 1: _yield$import = _context2.sent; loadAndAppendCustomThemeCss = _yield$import.loadAndAppendCustomThemeCss; loadAndAppendCustomThemeCss({ colorMode: attrOfMissingCustomStyles.length === 2 ? 'auto' : // only load the missing custom theme styles attrOfMissingCustomStyles[0], UNSAFE_themeOptions: UNSAFE_themeOptions }); case 2: case "end": return _context2.stop(); } }, _callee2); }))()); } } _context3.next = 1; return Promise.all(loadingTasks); case 1: // Load override themes after standard themes themeOverridePreferences = getThemeOverridePreferences(themeState); _iterator = _createForOfIteratorHelper(themeOverridePreferences); _context3.prev = 2; _iterator.s(); case 3: if ((_step = _iterator.n()).done) { _context3.next = 5; break; } themeId = _step.value; _context3.next = 4; return loadingStrategy(themeId); case 4: _context3.next = 3; break; case 5: _context3.next = 7; break; case 6: _context3.prev = 6; _t = _context3["catch"](2); _iterator.e(_t); case 7: _context3.prev = 7; _iterator.f(); return _context3.finish(7); case 8: autoUnbind = configurePage(themeState); return _context3.abrupt("return", autoUnbind); case 9: case "end": return _context3.stop(); } }, _callee3, null, [[2, 6, 7, 8]]); })); return function setGlobalTheme() { return _ref.apply(this, arguments); }; }(); export default setGlobalTheme;