@atlaskit/tokens 1.22.0 → 1.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/custom-themes/package.json +17 -0
  3. package/dist/cjs/constants.js +3 -1
  4. package/dist/cjs/custom-theme.js +45 -79
  5. package/dist/cjs/enable-global-theme.js +70 -0
  6. package/dist/cjs/entry-points/custom-themes.js +13 -0
  7. package/dist/cjs/get-theme-html-attrs.js +3 -5
  8. package/dist/cjs/get-token-value.js +1 -1
  9. package/dist/cjs/get-token.js +1 -1
  10. package/dist/cjs/index.js +7 -0
  11. package/dist/cjs/load-custom-theme-styles.js +45 -0
  12. package/dist/cjs/set-global-theme.js +50 -94
  13. package/dist/cjs/theme-config.js +6 -1
  14. package/dist/cjs/utils/color-mode-listeners.js +66 -0
  15. package/dist/cjs/utils/configure-page.js +34 -0
  16. package/dist/cjs/utils/get-theme-preferences.js +0 -5
  17. package/dist/es2019/constants.js +1 -0
  18. package/dist/es2019/custom-theme.js +4 -4
  19. package/dist/es2019/enable-global-theme.js +53 -0
  20. package/dist/es2019/entry-points/custom-themes.js +1 -0
  21. package/dist/es2019/get-theme-html-attrs.js +3 -5
  22. package/dist/es2019/get-token-value.js +1 -1
  23. package/dist/es2019/get-token.js +1 -1
  24. package/dist/es2019/index.js +1 -0
  25. package/dist/es2019/load-custom-theme-styles.js +37 -0
  26. package/dist/es2019/set-global-theme.js +34 -65
  27. package/dist/es2019/theme-config.js +7 -1
  28. package/dist/es2019/utils/color-mode-listeners.js +48 -0
  29. package/dist/es2019/utils/configure-page.js +22 -0
  30. package/dist/es2019/utils/get-theme-preferences.js +0 -5
  31. package/dist/esm/constants.js +1 -0
  32. package/dist/esm/custom-theme.js +45 -78
  33. package/dist/esm/enable-global-theme.js +63 -0
  34. package/dist/esm/entry-points/custom-themes.js +1 -0
  35. package/dist/esm/get-theme-html-attrs.js +3 -5
  36. package/dist/esm/get-token-value.js +1 -1
  37. package/dist/esm/get-token.js +1 -1
  38. package/dist/esm/index.js +1 -0
  39. package/dist/esm/load-custom-theme-styles.js +39 -0
  40. package/dist/esm/set-global-theme.js +49 -93
  41. package/dist/esm/theme-config.js +7 -1
  42. package/dist/esm/utils/color-mode-listeners.js +58 -0
  43. package/dist/esm/utils/configure-page.js +28 -0
  44. package/dist/esm/utils/get-theme-preferences.js +0 -5
  45. package/dist/types/constants.d.ts +1 -0
  46. package/dist/types/custom-theme.d.ts +3 -3
  47. package/dist/types/enable-global-theme.d.ts +25 -0
  48. package/dist/types/entry-points/custom-themes.d.ts +1 -0
  49. package/dist/types/get-global-theme.d.ts +1 -1
  50. package/dist/types/index.d.ts +1 -0
  51. package/dist/types/load-custom-theme-styles.d.ts +17 -0
  52. package/dist/types/set-global-theme.d.ts +1 -4
  53. package/dist/types/theme-config.d.ts +8 -1
  54. package/dist/types/theme-mutation-observer.d.ts +1 -1
  55. package/dist/types/use-theme-observer.d.ts +1 -1
  56. package/dist/types/utils/color-mode-listeners.d.ts +13 -0
  57. package/dist/types/utils/configure-page.d.ts +6 -0
  58. package/dist/types-ts4.5/constants.d.ts +1 -0
  59. package/dist/types-ts4.5/custom-theme.d.ts +3 -3
  60. package/dist/types-ts4.5/enable-global-theme.d.ts +25 -0
  61. package/dist/types-ts4.5/entry-points/custom-themes.d.ts +1 -0
  62. package/dist/types-ts4.5/get-global-theme.d.ts +1 -1
  63. package/dist/types-ts4.5/index.d.ts +1 -0
  64. package/dist/types-ts4.5/load-custom-theme-styles.d.ts +17 -0
  65. package/dist/types-ts4.5/set-global-theme.d.ts +1 -4
  66. package/dist/types-ts4.5/theme-config.d.ts +8 -1
  67. package/dist/types-ts4.5/theme-mutation-observer.d.ts +1 -1
  68. package/dist/types-ts4.5/use-theme-observer.d.ts +1 -1
  69. package/dist/types-ts4.5/utils/color-mode-listeners.d.ts +13 -0
  70. package/dist/types-ts4.5/utils/configure-page.d.ts +6 -0
  71. package/package.json +2 -1
  72. package/report.api.md +17 -3
  73. package/tmp/api-report-tmp.d.ts +6 -3
@@ -1,5 +1,3 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
- import _regeneratorRuntime from "@babel/runtime/regenerator";
3
1
  import { COLOR_MODE_ATTRIBUTE, CUSTOM_THEME_ATTRIBUTE } from './constants';
4
2
  import { themeStateDefaults } from './theme-config';
5
3
  import { limitSizeOfCustomStyleElements, reduceTokenMap } from './utils/custom-theme-loading-utils';
@@ -20,83 +18,52 @@ export var CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD = 10;
20
18
  * @param {string} themeState.colorMode Determines which color theme is applied
21
19
  * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
22
20
  *
23
- * @returns A Promise of an object array, containing theme IDs, data-attributes to attach to the theme, and the theme CSS.
21
+ * @returns An object array, containing theme IDs, data-attributes to attach to the theme, and the theme CSS.
24
22
  * If an error is encountered while loading themes, the themes array will be empty.
25
23
  */
26
- export function getCustomThemeStyles(_x) {
27
- return _getCustomThemeStyles.apply(this, arguments);
28
- }
29
- function _getCustomThemeStyles() {
30
- _getCustomThemeStyles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(themeState) {
31
- var _themeState$UNSAFE_th;
32
- var brandColor, mode, optionString, uniqueId, themeRamp, themes, tokenMaps;
33
- return _regeneratorRuntime.wrap(function _callee$(_context) {
34
- while (1) switch (_context.prev = _context.next) {
35
- case 0:
36
- brandColor = themeState === null || themeState === void 0 ? void 0 : (_themeState$UNSAFE_th = themeState.UNSAFE_themeOptions) === null || _themeState$UNSAFE_th === void 0 ? void 0 : _themeState$UNSAFE_th.brandColor;
37
- mode = (themeState === null || themeState === void 0 ? void 0 : themeState.colorMode) || themeStateDefaults['colorMode'];
38
- optionString = JSON.stringify(themeState === null || themeState === void 0 ? void 0 : themeState.UNSAFE_themeOptions);
39
- uniqueId = hash(optionString);
40
- themeRamp = generateColors(brandColor).ramp; // outputs object to generate to CSS from
41
- themes = [];
42
- tokenMaps = generateTokenMapWithContrastCheck(brandColor, mode, themeRamp);
43
- if ((mode === 'light' || mode === 'auto') && tokenMaps.light) {
44
- // Light mode theming
45
- themes.push({
46
- id: 'light',
47
- attrs: {
48
- 'data-theme': 'light',
49
- 'data-custom-theme': uniqueId
50
- },
51
- css: "\nhtml[".concat(CUSTOM_THEME_ATTRIBUTE, "=\"").concat(uniqueId, "\"][").concat(COLOR_MODE_ATTRIBUTE, "=\"light\"][data-theme~=\"light:light\"] {\n /* Branded tokens */\n ").concat(reduceTokenMap(tokenMaps.light, themeRamp), "\n}")
52
- });
53
- }
54
- if ((mode === 'dark' || mode === 'auto') && tokenMaps.dark) {
55
- // Dark mode theming
56
- themes.push({
57
- id: 'dark',
58
- attrs: {
59
- 'data-theme': 'dark',
60
- 'data-custom-theme': uniqueId
61
- },
62
- css: "\nhtml[".concat(CUSTOM_THEME_ATTRIBUTE, "=\"").concat(uniqueId, "\"][").concat(COLOR_MODE_ATTRIBUTE, "=\"dark\"][data-theme~=\"dark:dark\"] {\n /* Branded tokens */\n ").concat(reduceTokenMap(tokenMaps.dark, themeRamp), "\n}")
63
- });
64
- }
65
- return _context.abrupt("return", themes);
66
- case 10:
67
- case "end":
68
- return _context.stop();
69
- }
70
- }, _callee);
71
- }));
72
- return _getCustomThemeStyles.apply(this, arguments);
73
- }
74
- export function loadAndAppendCustomThemeCss(_x2) {
75
- return _loadAndAppendCustomThemeCss.apply(this, arguments);
24
+ export function getCustomThemeStyles(themeState) {
25
+ var _themeState$UNSAFE_th;
26
+ var brandColor = themeState === null || themeState === void 0 ? void 0 : (_themeState$UNSAFE_th = themeState.UNSAFE_themeOptions) === null || _themeState$UNSAFE_th === void 0 ? void 0 : _themeState$UNSAFE_th.brandColor;
27
+ var mode = (themeState === null || themeState === void 0 ? void 0 : themeState.colorMode) || themeStateDefaults['colorMode'];
28
+ var optionString = JSON.stringify(themeState === null || themeState === void 0 ? void 0 : themeState.UNSAFE_themeOptions);
29
+ var uniqueId = hash(optionString);
30
+ var themeRamp = generateColors(brandColor).ramp;
31
+
32
+ // outputs object to generate to CSS from
33
+ var themes = [];
34
+ var tokenMaps = generateTokenMapWithContrastCheck(brandColor, mode, themeRamp);
35
+ if ((mode === 'light' || mode === 'auto') && tokenMaps.light) {
36
+ // Light mode theming
37
+ themes.push({
38
+ id: 'light',
39
+ attrs: {
40
+ 'data-theme': 'light',
41
+ 'data-custom-theme': uniqueId
42
+ },
43
+ css: "\nhtml[".concat(CUSTOM_THEME_ATTRIBUTE, "=\"").concat(uniqueId, "\"][").concat(COLOR_MODE_ATTRIBUTE, "=\"light\"][data-theme~=\"light:light\"] {\n /* Branded tokens */\n ").concat(reduceTokenMap(tokenMaps.light, themeRamp), "\n}")
44
+ });
45
+ }
46
+ if ((mode === 'dark' || mode === 'auto') && tokenMaps.dark) {
47
+ // Dark mode theming
48
+ themes.push({
49
+ id: 'dark',
50
+ attrs: {
51
+ 'data-theme': 'dark',
52
+ 'data-custom-theme': uniqueId
53
+ },
54
+ css: "\nhtml[".concat(CUSTOM_THEME_ATTRIBUTE, "=\"").concat(uniqueId, "\"][").concat(COLOR_MODE_ATTRIBUTE, "=\"dark\"][data-theme~=\"dark:dark\"] {\n /* Branded tokens */\n ").concat(reduceTokenMap(tokenMaps.dark, themeRamp), "\n}")
55
+ });
56
+ }
57
+ return themes;
76
58
  }
77
- function _loadAndAppendCustomThemeCss() {
78
- _loadAndAppendCustomThemeCss = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(themeState) {
79
- var themes;
80
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
81
- while (1) switch (_context2.prev = _context2.next) {
82
- case 0:
83
- _context2.next = 2;
84
- return getCustomThemeStyles(themeState);
85
- case 2:
86
- themes = _context2.sent;
87
- limitSizeOfCustomStyleElements(CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD);
88
- themes.map(function (theme) {
89
- var styleTag = document.createElement('style');
90
- document.head.appendChild(styleTag);
91
- styleTag.dataset.theme = theme.attrs['data-theme'];
92
- styleTag.dataset.customTheme = theme.attrs['data-custom-theme'];
93
- styleTag.textContent = theme.css;
94
- });
95
- case 5:
96
- case "end":
97
- return _context2.stop();
98
- }
99
- }, _callee2);
100
- }));
101
- return _loadAndAppendCustomThemeCss.apply(this, arguments);
59
+ export function loadAndAppendCustomThemeCss(themeState) {
60
+ var themes = getCustomThemeStyles(themeState);
61
+ limitSizeOfCustomStyleElements(CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD);
62
+ themes.map(function (theme) {
63
+ var styleTag = document.createElement('style');
64
+ document.head.appendChild(styleTag);
65
+ styleTag.dataset.theme = theme.attrs['data-theme'];
66
+ styleTag.dataset.customTheme = theme.attrs['data-custom-theme'];
67
+ styleTag.textContent = theme.css;
68
+ });
102
69
  }
@@ -0,0 +1,63 @@
1
+ import { themeStateDefaults } from './theme-config';
2
+ import configurePage from './utils/configure-page';
3
+ import { getThemePreferences } from './utils/get-theme-preferences';
4
+
5
+ /**
6
+ * Synchronously sets the theme globally at runtime. Themes are not loaded;
7
+ * use `getThemeStyles` and other server-side utilities to generate and load them.
8
+ *
9
+ * @param {Object<string, string>} themeState The themes and color mode that should be applied.
10
+ * @param {string} themeState.colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
11
+ * @param {string} themeState.dark The color theme to be applied when the color mode resolves to 'dark'.
12
+ * @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'.
13
+ * @param {string} themeState.shape The shape theme to be applied.
14
+ * @param {string} themeState.spacing The spacing theme to be applied.
15
+ * @param {string} themeState.typography The typography theme to be applied.
16
+ * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
17
+ * @param {function} themeLoader Callback function used to override the default theme loading functionality.
18
+ *
19
+ * @returns An unbind function, that can be used to stop listening for changes to system theme.
20
+ *
21
+ * @example
22
+ * ```
23
+ * enableGlobalTheme({colorMode: 'auto', light: 'light', dark: 'dark', spacing: 'spacing'});
24
+ * ```
25
+ */
26
+ var enableGlobalTheme = function enableGlobalTheme() {
27
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
28
+ _ref$colorMode = _ref.colorMode,
29
+ colorMode = _ref$colorMode === void 0 ? themeStateDefaults['colorMode'] : _ref$colorMode,
30
+ _ref$dark = _ref.dark,
31
+ dark = _ref$dark === void 0 ? themeStateDefaults['dark'] : _ref$dark,
32
+ _ref$light = _ref.light,
33
+ light = _ref$light === void 0 ? themeStateDefaults['light'] : _ref$light,
34
+ _ref$shape = _ref.shape,
35
+ shape = _ref$shape === void 0 ? themeStateDefaults['shape'] : _ref$shape,
36
+ _ref$spacing = _ref.spacing,
37
+ spacing = _ref$spacing === void 0 ? themeStateDefaults['spacing'] : _ref$spacing,
38
+ _ref$typography = _ref.typography,
39
+ typography = _ref$typography === void 0 ? themeStateDefaults['typography'] : _ref$typography,
40
+ _ref$UNSAFE_themeOpti = _ref.UNSAFE_themeOptions,
41
+ UNSAFE_themeOptions = _ref$UNSAFE_themeOpti === void 0 ? themeStateDefaults['UNSAFE_themeOptions'] : _ref$UNSAFE_themeOpti;
42
+ var themeLoader = arguments.length > 1 ? arguments[1] : undefined;
43
+ var themeState = {
44
+ colorMode: colorMode,
45
+ dark: dark,
46
+ light: light,
47
+ shape: shape,
48
+ spacing: spacing,
49
+ typography: typography,
50
+ UNSAFE_themeOptions: themeLoader ? undefined : UNSAFE_themeOptions
51
+ };
52
+
53
+ // Determine what to load and call theme loader
54
+ var themePreferences = getThemePreferences(themeState);
55
+ if (themeLoader) {
56
+ themePreferences.map(function (themeId) {
57
+ return themeLoader(themeId);
58
+ });
59
+ }
60
+ var autoUnbind = configurePage(themeState);
61
+ return autoUnbind;
62
+ };
63
+ export default enableGlobalTheme;
@@ -0,0 +1 @@
1
+ export { default as UNSAFE_loadCustomThemeStyles } from '../load-custom-theme-styles';
@@ -37,15 +37,13 @@ var getThemeHtmlAttrs = function getThemeHtmlAttrs() {
37
37
  typography = _ref$typography === void 0 ? themeStateDefaults['typography'] : _ref$typography,
38
38
  _ref$UNSAFE_themeOpti = _ref.UNSAFE_themeOptions,
39
39
  UNSAFE_themeOptions = _ref$UNSAFE_themeOpti === void 0 ? themeStateDefaults['UNSAFE_themeOptions'] : _ref$UNSAFE_themeOpti;
40
- var themePreferences = {
40
+ var themeAttribute = themeObjectToString({
41
41
  dark: dark,
42
42
  light: light,
43
43
  shape: shape,
44
- // Load spacing by default
45
- spacing: spacing || 'spacing',
44
+ spacing: spacing,
46
45
  typography: typography
47
- };
48
- var themeAttribute = themeObjectToString(themePreferences);
46
+ });
49
47
  var result = (_result = {}, _defineProperty(_result, THEME_DATA_ATTRIBUTE, themeAttribute), _defineProperty(_result, COLOR_MODE_ATTRIBUTE, colorMode === 'auto' ? defaultColorMode : colorMode), _result);
50
48
  if (UNSAFE_themeOptions && isValidBrandHex(UNSAFE_themeOptions.brandColor)) {
51
49
  var optionString = JSON.stringify(UNSAFE_themeOptions);
@@ -1,7 +1,7 @@
1
1
  import warnOnce from '@atlaskit/ds-lib/warn-once';
2
2
  import tokens from './artifacts/token-names';
3
3
  var name = "@atlaskit/tokens";
4
- var version = "1.22.0";
4
+ var version = "1.23.0";
5
5
  /**
6
6
  * Takes a dot-separated token name and and an optional fallback, and returns the current computed CSS value for the
7
7
  * resulting CSS Custom Property.
@@ -2,7 +2,7 @@ import warnOnce from '@atlaskit/ds-lib/warn-once';
2
2
  import tokens from './artifacts/token-names';
3
3
  import { TOKEN_NOT_FOUND_CSS_VAR } from './constants';
4
4
  var name = "@atlaskit/tokens";
5
- var version = "1.22.0";
5
+ var version = "1.23.0";
6
6
  /**
7
7
  * Takes a dot-separated token name and an optional fallback, and returns the CSS custom property for the corresponding token.
8
8
  * This should be used to implement design decisions throughout your application.
package/dist/esm/index.js CHANGED
@@ -2,6 +2,7 @@ export { default as themeConfig } from './theme-config';
2
2
  export { default as token } from './get-token';
3
3
  export { default as getTokenValue } from './get-token-value';
4
4
  export { default as setGlobalTheme } from './set-global-theme';
5
+ export { default as enableGlobalTheme } from './enable-global-theme';
5
6
  export { default as getThemeStyles } from './get-theme-styles';
6
7
  export { default as getThemeHtmlAttrs } from './get-theme-html-attrs';
7
8
  export { default as getSSRAutoScript } from './get-ssr-auto-script';
@@ -0,0 +1,39 @@
1
+ import { loadAndAppendCustomThemeCss } from './custom-theme';
2
+ import { themeStateDefaults } from './theme-config';
3
+ import { isValidBrandHex } from './utils/color-utils';
4
+ import { findMissingCustomStyleElements } from './utils/custom-theme-loading-utils';
5
+
6
+ /**
7
+ * Synchronously generates and applies custom theme styles to the page.
8
+ *
9
+ * @param {Object<string, string>} themeState The themes and color mode that should be applied.
10
+ * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
11
+ *
12
+ * @example
13
+ * ```
14
+ * UNSAFE_loadCustomThemeStyles({
15
+ * colorMode: 'auto',
16
+ * UNSAFE_themeOptions: { brandColor: '#FF0000' }
17
+ * });
18
+ * ```
19
+ */
20
+ var UNSAFE_loadCustomThemeStyles = function UNSAFE_loadCustomThemeStyles() {
21
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
22
+ _ref$colorMode = _ref.colorMode,
23
+ colorMode = _ref$colorMode === void 0 ? themeStateDefaults['colorMode'] : _ref$colorMode,
24
+ _ref$UNSAFE_themeOpti = _ref.UNSAFE_themeOptions,
25
+ UNSAFE_themeOptions = _ref$UNSAFE_themeOpti === void 0 ? themeStateDefaults['UNSAFE_themeOptions'] : _ref$UNSAFE_themeOpti;
26
+ // Load custom theme styles
27
+ if (UNSAFE_themeOptions && isValidBrandHex(UNSAFE_themeOptions === null || UNSAFE_themeOptions === void 0 ? void 0 : UNSAFE_themeOptions.brandColor)) {
28
+ var attrOfMissingCustomStyles = findMissingCustomStyleElements(UNSAFE_themeOptions, colorMode);
29
+ if (attrOfMissingCustomStyles.length !== 0) {
30
+ loadAndAppendCustomThemeCss({
31
+ colorMode: attrOfMissingCustomStyles.length === 2 ? 'auto' :
32
+ // only load the missing custom theme styles
33
+ attrOfMissingCustomStyles[0],
34
+ UNSAFE_themeOptions: UNSAFE_themeOptions
35
+ });
36
+ }
37
+ }
38
+ };
39
+ export default UNSAFE_loadCustomThemeStyles;
@@ -1,32 +1,11 @@
1
- import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
- import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
3
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
4
2
  import _regeneratorRuntime from "@babel/runtime/regenerator";
5
- import { bind } from 'bind-event-listener';
6
- import noop from '@atlaskit/ds-lib/noop';
7
- import { COLOR_MODE_ATTRIBUTE } from './constants';
8
- import getThemeHtmlAttrs from './get-theme-html-attrs';
9
3
  import { themeStateDefaults } from './theme-config';
10
4
  import { isValidBrandHex } from './utils/color-utils';
5
+ import configurePage from './utils/configure-page';
11
6
  import { findMissingCustomStyleElements } from './utils/custom-theme-loading-utils';
12
7
  import { getThemePreferences } from './utils/get-theme-preferences';
13
- import { darkModeMediaQuery, loadAndAppendThemeCss } from './utils/theme-loading';
14
-
15
- // Represents theme state once mounted to the page (auto is hidden from observers)
16
-
17
- var isMatchMediaAvailable = typeof window !== 'undefined' && 'matchMedia' in window;
18
- var darkModeMql = isMatchMediaAvailable && window.matchMedia(darkModeMediaQuery);
19
- var unbindThemeChangeListener = noop;
20
-
21
- /**
22
- * Updates the current theme when the system theme changes. Should be bound
23
- * to an event listener listening on the '(prefers-color-scheme: dark)' query
24
- * @param e The event representing a change in system theme.
25
- */
26
- var checkNativeListener = function checkNativeListener(e) {
27
- var element = document.documentElement;
28
- element.setAttribute(COLOR_MODE_ATTRIBUTE, e.matches ? 'dark' : 'light');
29
- };
8
+ import { loadAndAppendThemeCss } from './utils/theme-loading';
30
9
 
31
10
  /**
32
11
  * Sets the theme globally at runtime. This updates the `data-theme` and `data-color-mode` attributes on your page's <html> tag.
@@ -66,26 +45,31 @@ var setGlobalTheme = /*#__PURE__*/function () {
66
45
  _ref2$UNSAFE_themeOpt,
67
46
  UNSAFE_themeOptions,
68
47
  themeLoader,
48
+ themeState,
69
49
  themePreferences,
70
50
  loadingStrategy,
71
- themeAttributes,
51
+ loadingTasks,
52
+ mode,
53
+ attrOfMissingCustomStyles,
54
+ autoUnbind,
72
55
  _args3 = arguments;
73
56
  return _regeneratorRuntime.wrap(function _callee3$(_context3) {
74
57
  while (1) switch (_context3.prev = _context3.next) {
75
58
  case 0:
76
59
  _ref2 = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {}, _ref2$colorMode = _ref2.colorMode, colorMode = _ref2$colorMode === void 0 ? themeStateDefaults['colorMode'] : _ref2$colorMode, _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$UNSAFE_themeOpt = _ref2.UNSAFE_themeOptions, UNSAFE_themeOptions = _ref2$UNSAFE_themeOpt === void 0 ? themeStateDefaults['UNSAFE_themeOptions'] : _ref2$UNSAFE_themeOpt;
77
60
  themeLoader = _args3.length > 1 ? _args3[1] : undefined;
78
- themePreferences = getThemePreferences({
61
+ themeState = {
79
62
  colorMode: colorMode,
80
63
  dark: dark,
81
64
  light: light,
82
65
  shape: shape,
83
66
  spacing: spacing,
84
- typography: typography
85
- });
86
- loadingStrategy = themeLoader ? themeLoader : loadAndAppendThemeCss;
87
- _context3.next = 6;
88
- return Promise.all([].concat(_toConsumableArray(themePreferences.map( /*#__PURE__*/function () {
67
+ typography: typography,
68
+ UNSAFE_themeOptions: themeLoader ? undefined : UNSAFE_themeOptions
69
+ }; // Determine what to load and loading strategy
70
+ themePreferences = getThemePreferences(themeState);
71
+ loadingStrategy = themeLoader ? themeLoader : loadAndAppendThemeCss; // Load standard themes
72
+ loadingTasks = themePreferences.map( /*#__PURE__*/function () {
89
73
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(themeId) {
90
74
  return _regeneratorRuntime.wrap(function _callee$(_context) {
91
75
  while (1) switch (_context.prev = _context.next) {
@@ -103,71 +87,43 @@ var setGlobalTheme = /*#__PURE__*/function () {
103
87
  return function (_x) {
104
88
  return _ref3.apply(this, arguments);
105
89
  };
106
- }())), [_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
107
- var mode, attrOfMissingCustomStyles, _yield$import, loadAndAppendCustomThemeCss;
108
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
109
- while (1) switch (_context2.prev = _context2.next) {
110
- case 0:
111
- if (!(!themeLoader && UNSAFE_themeOptions && isValidBrandHex(UNSAFE_themeOptions === null || UNSAFE_themeOptions === void 0 ? void 0 : UNSAFE_themeOptions.brandColor))) {
112
- _context2.next = 11;
113
- break;
114
- }
115
- mode = colorMode || themeStateDefaults['colorMode'];
116
- attrOfMissingCustomStyles = findMissingCustomStyleElements(UNSAFE_themeOptions, mode);
117
- if (!(attrOfMissingCustomStyles.length === 0)) {
118
- _context2.next = 5;
119
- break;
90
+ }()); // Load custom themes if needed
91
+ if (!themeLoader && UNSAFE_themeOptions && isValidBrandHex(UNSAFE_themeOptions === null || UNSAFE_themeOptions === void 0 ? void 0 : UNSAFE_themeOptions.brandColor)) {
92
+ mode = colorMode || themeStateDefaults['colorMode'];
93
+ attrOfMissingCustomStyles = findMissingCustomStyleElements(UNSAFE_themeOptions, mode);
94
+ if (attrOfMissingCustomStyles.length > 0) {
95
+ // Load custom theme styles
96
+ loadingTasks.push(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
97
+ var _yield$import, loadAndAppendCustomThemeCss;
98
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
99
+ while (1) switch (_context2.prev = _context2.next) {
100
+ case 0:
101
+ _context2.next = 2;
102
+ return import( /* webpackChunkName: "@atlaskit-internal_atlassian-custom-theme" */
103
+ './custom-theme');
104
+ case 2:
105
+ _yield$import = _context2.sent;
106
+ loadAndAppendCustomThemeCss = _yield$import.loadAndAppendCustomThemeCss;
107
+ loadAndAppendCustomThemeCss({
108
+ colorMode: attrOfMissingCustomStyles.length === 2 ? 'auto' :
109
+ // only load the missing custom theme styles
110
+ attrOfMissingCustomStyles[0],
111
+ UNSAFE_themeOptions: UNSAFE_themeOptions
112
+ });
113
+ case 5:
114
+ case "end":
115
+ return _context2.stop();
120
116
  }
121
- return _context2.abrupt("return", false);
122
- case 5:
123
- _context2.next = 7;
124
- return import( /* webpackChunkName: "@atlaskit-internal_atlassian-custom-theme" */
125
- './custom-theme');
126
- case 7:
127
- _yield$import = _context2.sent;
128
- loadAndAppendCustomThemeCss = _yield$import.loadAndAppendCustomThemeCss;
129
- _context2.next = 11;
130
- return loadAndAppendCustomThemeCss({
131
- colorMode: attrOfMissingCustomStyles.length === 2 ? 'auto' :
132
- // only load the missing custom theme styles
133
- attrOfMissingCustomStyles[0],
134
- UNSAFE_themeOptions: UNSAFE_themeOptions
135
- });
136
- case 11:
137
- case "end":
138
- return _context2.stop();
139
- }
140
- }, _callee2);
141
- }))()]));
142
- case 6:
143
- if (colorMode === 'auto' && darkModeMql) {
144
- colorMode = darkModeMql.matches ? 'dark' : 'light';
145
- // Add an event listener for changes to the system theme.
146
- // If the function exists, it will not be added again.
147
- unbindThemeChangeListener = bind(darkModeMql, {
148
- type: 'change',
149
- listener: checkNativeListener
150
- });
151
- } else {
152
- unbindThemeChangeListener();
117
+ }, _callee2);
118
+ }))());
119
+ }
153
120
  }
154
- themeAttributes = getThemeHtmlAttrs({
155
- colorMode: colorMode,
156
- dark: dark,
157
- light: light,
158
- shape: shape,
159
- spacing: spacing,
160
- typography: typography,
161
- UNSAFE_themeOptions: themeLoader ? undefined : UNSAFE_themeOptions
162
- });
163
- Object.entries(themeAttributes).forEach(function (_ref5) {
164
- var _ref6 = _slicedToArray(_ref5, 2),
165
- key = _ref6[0],
166
- value = _ref6[1];
167
- document.documentElement.setAttribute(key, value);
168
- });
169
- return _context3.abrupt("return", unbindThemeChangeListener);
170
- case 10:
121
+ _context3.next = 9;
122
+ return Promise.all(loadingTasks);
123
+ case 9:
124
+ autoUnbind = configurePage(themeState);
125
+ return _context3.abrupt("return", autoUnbind);
126
+ case 11:
171
127
  case "end":
172
128
  return _context3.stop();
173
129
  }
@@ -174,8 +174,14 @@ export var themeStateDefaults = {
174
174
  dark: 'dark',
175
175
  light: 'light',
176
176
  shape: undefined,
177
- spacing: undefined,
177
+ spacing: 'spacing',
178
178
  typography: undefined,
179
179
  UNSAFE_themeOptions: undefined
180
180
  };
181
+
182
+ /**
183
+ * Represents theme state once mounted to the page
184
+ * (the page doesn't have an "auto" color mode, it's either light or dark)
185
+ */
186
+
181
187
  export default themeConfig;
@@ -0,0 +1,58 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
+ import { bind as _bind } from 'bind-event-listener';
5
+ import { COLOR_MODE_ATTRIBUTE } from '../constants';
6
+ import { darkModeMediaQuery } from './theme-loading';
7
+ var isMatchMediaAvailable = typeof window !== 'undefined' && 'matchMedia' in window;
8
+
9
+ /**
10
+ * Updates the current theme when the system theme changes. Should be bound
11
+ * to an event listener listening on the '(prefers-color-scheme: dark)' query
12
+ * @param e The event representing a change in system theme.
13
+ */
14
+ function checkNativeListener(e) {
15
+ var element = document.documentElement;
16
+ element.setAttribute(COLOR_MODE_ATTRIBUTE, e.matches ? 'dark' : 'light');
17
+ }
18
+ var darkModeMql = isMatchMediaAvailable && window.matchMedia(darkModeMediaQuery);
19
+ var ColorModeObserver = /*#__PURE__*/function () {
20
+ function ColorModeObserver() {
21
+ _classCallCheck(this, ColorModeObserver);
22
+ _defineProperty(this, "unbindThemeChangeListener", null);
23
+ }
24
+ _createClass(ColorModeObserver, [{
25
+ key: "getColorMode",
26
+ value: function getColorMode() {
27
+ if (!darkModeMql) {
28
+ return 'light';
29
+ }
30
+ return darkModeMql !== null && darkModeMql !== void 0 && darkModeMql.matches ? 'dark' : 'light';
31
+ }
32
+ }, {
33
+ key: "bind",
34
+ value: function bind() {
35
+ if (darkModeMql && this.unbindThemeChangeListener === null) {
36
+ this.unbindThemeChangeListener = _bind(darkModeMql, {
37
+ type: 'change',
38
+ listener: checkNativeListener
39
+ });
40
+ }
41
+ }
42
+ }, {
43
+ key: "unbind",
44
+ value: function unbind() {
45
+ if (this.unbindThemeChangeListener) {
46
+ this.unbindThemeChangeListener();
47
+ this.unbindThemeChangeListener = null;
48
+ }
49
+ }
50
+ }]);
51
+ return ColorModeObserver;
52
+ }();
53
+ /**
54
+ * A singleton color mode observer - binds "auto" switching logic to a single `mediaQueryList` listener
55
+ * that can be unbound by any consumer when no longer needed.
56
+ */
57
+ var SingletonColorModeObserver = new ColorModeObserver();
58
+ export default SingletonColorModeObserver;
@@ -0,0 +1,28 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import getThemeHtmlAttrs from '../get-theme-html-attrs';
3
+ import ColorModeObserver from './color-mode-listeners';
4
+
5
+ /**
6
+ * Given ThemeState, sets appropriate html attributes on the documentElement,
7
+ * adds a listener to keep colorMode updated, and returns a function to unbind.
8
+ */
9
+ export default function configurePage(themeState) {
10
+ if (themeState.colorMode === 'auto') {
11
+ // Set colorMode based on the user preference
12
+ themeState.colorMode = ColorModeObserver.getColorMode();
13
+ // Bind a listener (if one doesn't already exist) to keep colorMode updated
14
+ ColorModeObserver.bind();
15
+ } else {
16
+ ColorModeObserver.unbind();
17
+ }
18
+ var themeAttributes = getThemeHtmlAttrs(themeState);
19
+ Object.entries(themeAttributes).forEach(function (_ref) {
20
+ var _ref2 = _slicedToArray(_ref, 2),
21
+ key = _ref2[0],
22
+ value = _ref2[1];
23
+ document.documentElement.setAttribute(key, value);
24
+ });
25
+ return function () {
26
+ return ColorModeObserver.unbind;
27
+ };
28
+ }
@@ -16,10 +16,5 @@ export var getThemePreferences = function getThemePreferences(themeState) {
16
16
  if (getBooleanFF('platform.design-system-team.border-checkbox_nyoiu')) {
17
17
  themePreferences.push("".concat(themePreferences.includes('dark') ? 'dark' : 'light', "-new-input-border"));
18
18
  }
19
-
20
- // Load spacing by default
21
- if (!themePreferences.includes('spacing')) {
22
- themePreferences.push('spacing');
23
- }
24
19
  return _toConsumableArray(new Set(themePreferences));
25
20
  };
@@ -1,6 +1,7 @@
1
1
  export declare const THEME_DATA_ATTRIBUTE = "data-theme";
2
2
  export declare const COLOR_MODE_ATTRIBUTE = "data-color-mode";
3
3
  export declare const CUSTOM_THEME_ATTRIBUTE = "data-custom-theme";
4
+ export declare const DEFAULT_THEME = "light";
4
5
  export declare const CSS_PREFIX = "ds";
5
6
  export declare const CSS_VAR_FULL: string[];
6
7
  export declare const TOKEN_NOT_FOUND_CSS_VAR: string;
@@ -14,12 +14,12 @@ export declare const CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD = 10;
14
14
  * @param {string} themeState.colorMode Determines which color theme is applied
15
15
  * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
16
16
  *
17
- * @returns A Promise of an object array, containing theme IDs, data-attributes to attach to the theme, and the theme CSS.
17
+ * @returns An object array, containing theme IDs, data-attributes to attach to the theme, and the theme CSS.
18
18
  * If an error is encountered while loading themes, the themes array will be empty.
19
19
  */
20
20
  export declare function getCustomThemeStyles(themeState: Partial<ThemeState> & {
21
21
  UNSAFE_themeOptions: ThemeOptionsSchema;
22
- }): Promise<ThemeStyles[]>;
22
+ }): ThemeStyles[];
23
23
  export declare function loadAndAppendCustomThemeCss(themeState: Partial<ThemeState> & {
24
24
  UNSAFE_themeOptions: ThemeOptionsSchema;
25
- }): Promise<void>;
25
+ }): void;