@atlaskit/tokens 1.22.1 → 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 (67) hide show
  1. package/CHANGELOG.md +9 -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-token-value.js +1 -1
  8. package/dist/cjs/get-token.js +1 -1
  9. package/dist/cjs/index.js +7 -0
  10. package/dist/cjs/load-custom-theme-styles.js +45 -0
  11. package/dist/cjs/set-global-theme.js +50 -94
  12. package/dist/cjs/theme-config.js +5 -0
  13. package/dist/cjs/utils/color-mode-listeners.js +66 -0
  14. package/dist/cjs/utils/configure-page.js +34 -0
  15. package/dist/es2019/constants.js +1 -0
  16. package/dist/es2019/custom-theme.js +4 -4
  17. package/dist/es2019/enable-global-theme.js +53 -0
  18. package/dist/es2019/entry-points/custom-themes.js +1 -0
  19. package/dist/es2019/get-token-value.js +1 -1
  20. package/dist/es2019/get-token.js +1 -1
  21. package/dist/es2019/index.js +1 -0
  22. package/dist/es2019/load-custom-theme-styles.js +37 -0
  23. package/dist/es2019/set-global-theme.js +34 -65
  24. package/dist/es2019/theme-config.js +6 -0
  25. package/dist/es2019/utils/color-mode-listeners.js +48 -0
  26. package/dist/es2019/utils/configure-page.js +22 -0
  27. package/dist/esm/constants.js +1 -0
  28. package/dist/esm/custom-theme.js +45 -78
  29. package/dist/esm/enable-global-theme.js +63 -0
  30. package/dist/esm/entry-points/custom-themes.js +1 -0
  31. package/dist/esm/get-token-value.js +1 -1
  32. package/dist/esm/get-token.js +1 -1
  33. package/dist/esm/index.js +1 -0
  34. package/dist/esm/load-custom-theme-styles.js +39 -0
  35. package/dist/esm/set-global-theme.js +49 -93
  36. package/dist/esm/theme-config.js +6 -0
  37. package/dist/esm/utils/color-mode-listeners.js +58 -0
  38. package/dist/esm/utils/configure-page.js +28 -0
  39. package/dist/types/constants.d.ts +1 -0
  40. package/dist/types/custom-theme.d.ts +3 -3
  41. package/dist/types/enable-global-theme.d.ts +25 -0
  42. package/dist/types/entry-points/custom-themes.d.ts +1 -0
  43. package/dist/types/get-global-theme.d.ts +1 -1
  44. package/dist/types/index.d.ts +1 -0
  45. package/dist/types/load-custom-theme-styles.d.ts +17 -0
  46. package/dist/types/set-global-theme.d.ts +1 -4
  47. package/dist/types/theme-config.d.ts +7 -0
  48. package/dist/types/theme-mutation-observer.d.ts +1 -1
  49. package/dist/types/use-theme-observer.d.ts +1 -1
  50. package/dist/types/utils/color-mode-listeners.d.ts +13 -0
  51. package/dist/types/utils/configure-page.d.ts +6 -0
  52. package/dist/types-ts4.5/constants.d.ts +1 -0
  53. package/dist/types-ts4.5/custom-theme.d.ts +3 -3
  54. package/dist/types-ts4.5/enable-global-theme.d.ts +25 -0
  55. package/dist/types-ts4.5/entry-points/custom-themes.d.ts +1 -0
  56. package/dist/types-ts4.5/get-global-theme.d.ts +1 -1
  57. package/dist/types-ts4.5/index.d.ts +1 -0
  58. package/dist/types-ts4.5/load-custom-theme-styles.d.ts +17 -0
  59. package/dist/types-ts4.5/set-global-theme.d.ts +1 -4
  60. package/dist/types-ts4.5/theme-config.d.ts +7 -0
  61. package/dist/types-ts4.5/theme-mutation-observer.d.ts +1 -1
  62. package/dist/types-ts4.5/use-theme-observer.d.ts +1 -1
  63. package/dist/types-ts4.5/utils/color-mode-listeners.d.ts +13 -0
  64. package/dist/types-ts4.5/utils/configure-page.d.ts +6 -0
  65. package/package.json +2 -1
  66. package/report.api.md +16 -2
  67. package/tmp/api-report-tmp.d.ts +5 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @atlaskit/tokens
2
2
 
3
+ ## 1.23.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`5a967ac2397`](https://bitbucket.org/atlassian/atlassian-frontend/commits/5a967ac2397) - Adds two new synchronous theme configuration utilities:
8
+
9
+ - `enableGlobalTheme` is a synchronous version of `setGlobalTheme` that sets data attributes on the page and takes a themeLoader
10
+ - `UNSAFE_loadCustomThemeStyles` provides a synchronous way to generate and apply custom theme styles to the page. It can be accessed from a new entrypoint, `@atlaskit/tokens/custom-themes`
11
+
3
12
  ## 1.22.1
4
13
 
5
14
  ### Patch Changes
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@atlaskit/tokens/custom-themes",
3
+ "main": "../dist/cjs/entry-points/custom-themes.js",
4
+ "module": "../dist/esm/entry-points/custom-themes.js",
5
+ "module:es2019": "../dist/es2019/entry-points/custom-themes.js",
6
+ "sideEffects": [
7
+ "**/*.css"
8
+ ],
9
+ "types": "../dist/types/entry-points/custom-themes.d.ts",
10
+ "typesVersions": {
11
+ ">=4.5 <4.9": {
12
+ "*": [
13
+ "../dist/types-ts4.5/entry-points/custom-themes.d.ts"
14
+ ]
15
+ }
16
+ }
17
+ }
@@ -3,13 +3,15 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.TOKEN_NOT_FOUND_CSS_VAR = exports.THEME_DATA_ATTRIBUTE = exports.CUSTOM_THEME_ATTRIBUTE = exports.CURRENT_SURFACE_CSS_VAR = exports.CSS_VAR_FULL = exports.CSS_PREFIX = exports.COLOR_MODE_ATTRIBUTE = void 0;
6
+ exports.TOKEN_NOT_FOUND_CSS_VAR = exports.THEME_DATA_ATTRIBUTE = exports.DEFAULT_THEME = exports.CUSTOM_THEME_ATTRIBUTE = exports.CURRENT_SURFACE_CSS_VAR = exports.CSS_VAR_FULL = exports.CSS_PREFIX = exports.COLOR_MODE_ATTRIBUTE = void 0;
7
7
  var THEME_DATA_ATTRIBUTE = 'data-theme';
8
8
  exports.THEME_DATA_ATTRIBUTE = THEME_DATA_ATTRIBUTE;
9
9
  var COLOR_MODE_ATTRIBUTE = 'data-color-mode';
10
10
  exports.COLOR_MODE_ATTRIBUTE = COLOR_MODE_ATTRIBUTE;
11
11
  var CUSTOM_THEME_ATTRIBUTE = 'data-custom-theme';
12
12
  exports.CUSTOM_THEME_ATTRIBUTE = CUSTOM_THEME_ATTRIBUTE;
13
+ var DEFAULT_THEME = 'light';
14
+ exports.DEFAULT_THEME = DEFAULT_THEME;
13
15
  var CSS_PREFIX = 'ds';
14
16
  exports.CSS_PREFIX = CSS_PREFIX;
15
17
  var CSS_VAR_FULL = ['opacity', 'font', 'space', 'border'];
@@ -1,14 +1,11 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
3
  Object.defineProperty(exports, "__esModule", {
5
4
  value: true
6
5
  });
7
6
  exports.CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD = void 0;
8
7
  exports.getCustomThemeStyles = getCustomThemeStyles;
9
8
  exports.loadAndAppendCustomThemeCss = loadAndAppendCustomThemeCss;
10
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
- var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
12
9
  var _constants = require("./constants");
13
10
  var _themeConfig = require("./theme-config");
14
11
  var _customThemeLoadingUtils = require("./utils/custom-theme-loading-utils");
@@ -29,84 +26,53 @@ var CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD = 10;
29
26
  * @param {string} themeState.colorMode Determines which color theme is applied
30
27
  * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
31
28
  *
32
- * @returns A Promise of an object array, containing theme IDs, data-attributes to attach to the theme, and the theme CSS.
29
+ * @returns An object array, containing theme IDs, data-attributes to attach to the theme, and the theme CSS.
33
30
  * If an error is encountered while loading themes, the themes array will be empty.
34
31
  */
35
32
  exports.CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD = CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD;
36
- function getCustomThemeStyles(_x) {
37
- return _getCustomThemeStyles.apply(this, arguments);
38
- }
39
- function _getCustomThemeStyles() {
40
- _getCustomThemeStyles = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(themeState) {
41
- var _themeState$UNSAFE_th;
42
- var brandColor, mode, optionString, uniqueId, themeRamp, themes, tokenMaps;
43
- return _regenerator.default.wrap(function _callee$(_context) {
44
- while (1) switch (_context.prev = _context.next) {
45
- case 0:
46
- 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;
47
- mode = (themeState === null || themeState === void 0 ? void 0 : themeState.colorMode) || _themeConfig.themeStateDefaults['colorMode'];
48
- optionString = JSON.stringify(themeState === null || themeState === void 0 ? void 0 : themeState.UNSAFE_themeOptions);
49
- uniqueId = (0, _hash.hash)(optionString);
50
- themeRamp = (0, _generateCustomColorRamp.generateColors)(brandColor).ramp; // outputs object to generate to CSS from
51
- themes = [];
52
- tokenMaps = (0, _generateCustomColorRamp.generateTokenMapWithContrastCheck)(brandColor, mode, themeRamp);
53
- if ((mode === 'light' || mode === 'auto') && tokenMaps.light) {
54
- // Light mode theming
55
- themes.push({
56
- id: 'light',
57
- attrs: {
58
- 'data-theme': 'light',
59
- 'data-custom-theme': uniqueId
60
- },
61
- css: "\nhtml[".concat(_constants.CUSTOM_THEME_ATTRIBUTE, "=\"").concat(uniqueId, "\"][").concat(_constants.COLOR_MODE_ATTRIBUTE, "=\"light\"][data-theme~=\"light:light\"] {\n /* Branded tokens */\n ").concat((0, _customThemeLoadingUtils.reduceTokenMap)(tokenMaps.light, themeRamp), "\n}")
62
- });
63
- }
64
- if ((mode === 'dark' || mode === 'auto') && tokenMaps.dark) {
65
- // Dark mode theming
66
- themes.push({
67
- id: 'dark',
68
- attrs: {
69
- 'data-theme': 'dark',
70
- 'data-custom-theme': uniqueId
71
- },
72
- css: "\nhtml[".concat(_constants.CUSTOM_THEME_ATTRIBUTE, "=\"").concat(uniqueId, "\"][").concat(_constants.COLOR_MODE_ATTRIBUTE, "=\"dark\"][data-theme~=\"dark:dark\"] {\n /* Branded tokens */\n ").concat((0, _customThemeLoadingUtils.reduceTokenMap)(tokenMaps.dark, themeRamp), "\n}")
73
- });
74
- }
75
- return _context.abrupt("return", themes);
76
- case 10:
77
- case "end":
78
- return _context.stop();
79
- }
80
- }, _callee);
81
- }));
82
- return _getCustomThemeStyles.apply(this, arguments);
83
- }
84
- function loadAndAppendCustomThemeCss(_x2) {
85
- return _loadAndAppendCustomThemeCss.apply(this, arguments);
33
+ function getCustomThemeStyles(themeState) {
34
+ var _themeState$UNSAFE_th;
35
+ 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;
36
+ var mode = (themeState === null || themeState === void 0 ? void 0 : themeState.colorMode) || _themeConfig.themeStateDefaults['colorMode'];
37
+ var optionString = JSON.stringify(themeState === null || themeState === void 0 ? void 0 : themeState.UNSAFE_themeOptions);
38
+ var uniqueId = (0, _hash.hash)(optionString);
39
+ var themeRamp = (0, _generateCustomColorRamp.generateColors)(brandColor).ramp;
40
+
41
+ // outputs object to generate to CSS from
42
+ var themes = [];
43
+ var tokenMaps = (0, _generateCustomColorRamp.generateTokenMapWithContrastCheck)(brandColor, mode, themeRamp);
44
+ if ((mode === 'light' || mode === 'auto') && tokenMaps.light) {
45
+ // Light mode theming
46
+ themes.push({
47
+ id: 'light',
48
+ attrs: {
49
+ 'data-theme': 'light',
50
+ 'data-custom-theme': uniqueId
51
+ },
52
+ css: "\nhtml[".concat(_constants.CUSTOM_THEME_ATTRIBUTE, "=\"").concat(uniqueId, "\"][").concat(_constants.COLOR_MODE_ATTRIBUTE, "=\"light\"][data-theme~=\"light:light\"] {\n /* Branded tokens */\n ").concat((0, _customThemeLoadingUtils.reduceTokenMap)(tokenMaps.light, themeRamp), "\n}")
53
+ });
54
+ }
55
+ if ((mode === 'dark' || mode === 'auto') && tokenMaps.dark) {
56
+ // Dark mode theming
57
+ themes.push({
58
+ id: 'dark',
59
+ attrs: {
60
+ 'data-theme': 'dark',
61
+ 'data-custom-theme': uniqueId
62
+ },
63
+ css: "\nhtml[".concat(_constants.CUSTOM_THEME_ATTRIBUTE, "=\"").concat(uniqueId, "\"][").concat(_constants.COLOR_MODE_ATTRIBUTE, "=\"dark\"][data-theme~=\"dark:dark\"] {\n /* Branded tokens */\n ").concat((0, _customThemeLoadingUtils.reduceTokenMap)(tokenMaps.dark, themeRamp), "\n}")
64
+ });
65
+ }
66
+ return themes;
86
67
  }
87
- function _loadAndAppendCustomThemeCss() {
88
- _loadAndAppendCustomThemeCss = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(themeState) {
89
- var themes;
90
- return _regenerator.default.wrap(function _callee2$(_context2) {
91
- while (1) switch (_context2.prev = _context2.next) {
92
- case 0:
93
- _context2.next = 2;
94
- return getCustomThemeStyles(themeState);
95
- case 2:
96
- themes = _context2.sent;
97
- (0, _customThemeLoadingUtils.limitSizeOfCustomStyleElements)(CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD);
98
- themes.map(function (theme) {
99
- var styleTag = document.createElement('style');
100
- document.head.appendChild(styleTag);
101
- styleTag.dataset.theme = theme.attrs['data-theme'];
102
- styleTag.dataset.customTheme = theme.attrs['data-custom-theme'];
103
- styleTag.textContent = theme.css;
104
- });
105
- case 5:
106
- case "end":
107
- return _context2.stop();
108
- }
109
- }, _callee2);
110
- }));
111
- return _loadAndAppendCustomThemeCss.apply(this, arguments);
68
+ function loadAndAppendCustomThemeCss(themeState) {
69
+ var themes = getCustomThemeStyles(themeState);
70
+ (0, _customThemeLoadingUtils.limitSizeOfCustomStyleElements)(CUSTOM_STYLE_ELEMENTS_SIZE_THRESHOLD);
71
+ themes.map(function (theme) {
72
+ var styleTag = document.createElement('style');
73
+ document.head.appendChild(styleTag);
74
+ styleTag.dataset.theme = theme.attrs['data-theme'];
75
+ styleTag.dataset.customTheme = theme.attrs['data-custom-theme'];
76
+ styleTag.textContent = theme.css;
77
+ });
112
78
  }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _themeConfig = require("./theme-config");
9
+ var _configurePage = _interopRequireDefault(require("./utils/configure-page"));
10
+ var _getThemePreferences = require("./utils/get-theme-preferences");
11
+ /**
12
+ * Synchronously sets the theme globally at runtime. Themes are not loaded;
13
+ * use `getThemeStyles` and other server-side utilities to generate and load them.
14
+ *
15
+ * @param {Object<string, string>} themeState The themes and color mode that should be applied.
16
+ * @param {string} themeState.colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
17
+ * @param {string} themeState.dark The color theme to be applied when the color mode resolves to 'dark'.
18
+ * @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'.
19
+ * @param {string} themeState.shape The shape theme to be applied.
20
+ * @param {string} themeState.spacing The spacing theme to be applied.
21
+ * @param {string} themeState.typography The typography theme to be applied.
22
+ * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
23
+ * @param {function} themeLoader Callback function used to override the default theme loading functionality.
24
+ *
25
+ * @returns An unbind function, that can be used to stop listening for changes to system theme.
26
+ *
27
+ * @example
28
+ * ```
29
+ * enableGlobalTheme({colorMode: 'auto', light: 'light', dark: 'dark', spacing: 'spacing'});
30
+ * ```
31
+ */
32
+ var enableGlobalTheme = function enableGlobalTheme() {
33
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
34
+ _ref$colorMode = _ref.colorMode,
35
+ colorMode = _ref$colorMode === void 0 ? _themeConfig.themeStateDefaults['colorMode'] : _ref$colorMode,
36
+ _ref$dark = _ref.dark,
37
+ dark = _ref$dark === void 0 ? _themeConfig.themeStateDefaults['dark'] : _ref$dark,
38
+ _ref$light = _ref.light,
39
+ light = _ref$light === void 0 ? _themeConfig.themeStateDefaults['light'] : _ref$light,
40
+ _ref$shape = _ref.shape,
41
+ shape = _ref$shape === void 0 ? _themeConfig.themeStateDefaults['shape'] : _ref$shape,
42
+ _ref$spacing = _ref.spacing,
43
+ spacing = _ref$spacing === void 0 ? _themeConfig.themeStateDefaults['spacing'] : _ref$spacing,
44
+ _ref$typography = _ref.typography,
45
+ typography = _ref$typography === void 0 ? _themeConfig.themeStateDefaults['typography'] : _ref$typography,
46
+ _ref$UNSAFE_themeOpti = _ref.UNSAFE_themeOptions,
47
+ UNSAFE_themeOptions = _ref$UNSAFE_themeOpti === void 0 ? _themeConfig.themeStateDefaults['UNSAFE_themeOptions'] : _ref$UNSAFE_themeOpti;
48
+ var themeLoader = arguments.length > 1 ? arguments[1] : undefined;
49
+ var themeState = {
50
+ colorMode: colorMode,
51
+ dark: dark,
52
+ light: light,
53
+ shape: shape,
54
+ spacing: spacing,
55
+ typography: typography,
56
+ UNSAFE_themeOptions: themeLoader ? undefined : UNSAFE_themeOptions
57
+ };
58
+
59
+ // Determine what to load and call theme loader
60
+ var themePreferences = (0, _getThemePreferences.getThemePreferences)(themeState);
61
+ if (themeLoader) {
62
+ themePreferences.map(function (themeId) {
63
+ return themeLoader(themeId);
64
+ });
65
+ }
66
+ var autoUnbind = (0, _configurePage.default)(themeState);
67
+ return autoUnbind;
68
+ };
69
+ var _default = enableGlobalTheme;
70
+ exports.default = _default;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ Object.defineProperty(exports, "UNSAFE_loadCustomThemeStyles", {
8
+ enumerable: true,
9
+ get: function get() {
10
+ return _loadCustomThemeStyles.default;
11
+ }
12
+ });
13
+ var _loadCustomThemeStyles = _interopRequireDefault(require("../load-custom-theme-styles"));
@@ -8,7 +8,7 @@ exports.default = void 0;
8
8
  var _warnOnce = _interopRequireDefault(require("@atlaskit/ds-lib/warn-once"));
9
9
  var _tokenNames = _interopRequireDefault(require("./artifacts/token-names"));
10
10
  var name = "@atlaskit/tokens";
11
- var version = "1.22.1";
11
+ var version = "1.23.0";
12
12
  /**
13
13
  * Takes a dot-separated token name and and an optional fallback, and returns the current computed CSS value for the
14
14
  * resulting CSS Custom Property.
@@ -9,7 +9,7 @@ var _warnOnce = _interopRequireDefault(require("@atlaskit/ds-lib/warn-once"));
9
9
  var _tokenNames = _interopRequireDefault(require("./artifacts/token-names"));
10
10
  var _constants = require("./constants");
11
11
  var name = "@atlaskit/tokens";
12
- var version = "1.22.1";
12
+ var version = "1.23.0";
13
13
  /**
14
14
  * Takes a dot-separated token name and an optional fallback, and returns the CSS custom property for the corresponding token.
15
15
  * This should be used to implement design decisions throughout your application.
package/dist/cjs/index.js CHANGED
@@ -16,6 +16,12 @@ Object.defineProperty(exports, "ThemeMutationObserver", {
16
16
  return _themeMutationObserver.default;
17
17
  }
18
18
  });
19
+ Object.defineProperty(exports, "enableGlobalTheme", {
20
+ enumerable: true,
21
+ get: function get() {
22
+ return _enableGlobalTheme.default;
23
+ }
24
+ });
19
25
  Object.defineProperty(exports, "getGlobalTheme", {
20
26
  enumerable: true,
21
27
  get: function get() {
@@ -86,6 +92,7 @@ var _themeConfig = _interopRequireDefault(require("./theme-config"));
86
92
  var _getToken = _interopRequireDefault(require("./get-token"));
87
93
  var _getTokenValue = _interopRequireDefault(require("./get-token-value"));
88
94
  var _setGlobalTheme = _interopRequireDefault(require("./set-global-theme"));
95
+ var _enableGlobalTheme = _interopRequireDefault(require("./enable-global-theme"));
89
96
  var _getThemeStyles = _interopRequireDefault(require("./get-theme-styles"));
90
97
  var _getThemeHtmlAttrs = _interopRequireDefault(require("./get-theme-html-attrs"));
91
98
  var _getSsrAutoScript = _interopRequireDefault(require("./get-ssr-auto-script"));
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _customTheme = require("./custom-theme");
8
+ var _themeConfig = require("./theme-config");
9
+ var _colorUtils = require("./utils/color-utils");
10
+ var _customThemeLoadingUtils = require("./utils/custom-theme-loading-utils");
11
+ /**
12
+ * Synchronously generates and applies custom theme styles to the page.
13
+ *
14
+ * @param {Object<string, string>} themeState The themes and color mode that should be applied.
15
+ * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
16
+ *
17
+ * @example
18
+ * ```
19
+ * UNSAFE_loadCustomThemeStyles({
20
+ * colorMode: 'auto',
21
+ * UNSAFE_themeOptions: { brandColor: '#FF0000' }
22
+ * });
23
+ * ```
24
+ */
25
+ var UNSAFE_loadCustomThemeStyles = function UNSAFE_loadCustomThemeStyles() {
26
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
27
+ _ref$colorMode = _ref.colorMode,
28
+ colorMode = _ref$colorMode === void 0 ? _themeConfig.themeStateDefaults['colorMode'] : _ref$colorMode,
29
+ _ref$UNSAFE_themeOpti = _ref.UNSAFE_themeOptions,
30
+ UNSAFE_themeOptions = _ref$UNSAFE_themeOpti === void 0 ? _themeConfig.themeStateDefaults['UNSAFE_themeOptions'] : _ref$UNSAFE_themeOpti;
31
+ // Load custom theme styles
32
+ if (UNSAFE_themeOptions && (0, _colorUtils.isValidBrandHex)(UNSAFE_themeOptions === null || UNSAFE_themeOptions === void 0 ? void 0 : UNSAFE_themeOptions.brandColor)) {
33
+ var attrOfMissingCustomStyles = (0, _customThemeLoadingUtils.findMissingCustomStyleElements)(UNSAFE_themeOptions, colorMode);
34
+ if (attrOfMissingCustomStyles.length !== 0) {
35
+ (0, _customTheme.loadAndAppendCustomThemeCss)({
36
+ colorMode: attrOfMissingCustomStyles.length === 2 ? 'auto' :
37
+ // only load the missing custom theme styles
38
+ attrOfMissingCustomStyles[0],
39
+ UNSAFE_themeOptions: UNSAFE_themeOptions
40
+ });
41
+ }
42
+ }
43
+ };
44
+ var _default = UNSAFE_loadCustomThemeStyles;
45
+ exports.default = _default;
@@ -7,36 +7,15 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports.default = void 0;
9
9
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
10
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
12
10
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
- var _bindEventListener = require("bind-event-listener");
14
- var _noop = _interopRequireDefault(require("@atlaskit/ds-lib/noop"));
15
- var _constants = require("./constants");
16
- var _getThemeHtmlAttrs = _interopRequireDefault(require("./get-theme-html-attrs"));
17
11
  var _themeConfig = require("./theme-config");
18
12
  var _colorUtils = require("./utils/color-utils");
13
+ var _configurePage = _interopRequireDefault(require("./utils/configure-page"));
19
14
  var _customThemeLoadingUtils = require("./utils/custom-theme-loading-utils");
20
15
  var _getThemePreferences = require("./utils/get-theme-preferences");
21
16
  var _themeLoading = require("./utils/theme-loading");
22
17
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
23
18
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
24
- // Represents theme state once mounted to the page (auto is hidden from observers)
25
-
26
- var isMatchMediaAvailable = typeof window !== 'undefined' && 'matchMedia' in window;
27
- var darkModeMql = isMatchMediaAvailable && window.matchMedia(_themeLoading.darkModeMediaQuery);
28
- var unbindThemeChangeListener = _noop.default;
29
-
30
- /**
31
- * Updates the current theme when the system theme changes. Should be bound
32
- * to an event listener listening on the '(prefers-color-scheme: dark)' query
33
- * @param e The event representing a change in system theme.
34
- */
35
- var checkNativeListener = function checkNativeListener(e) {
36
- var element = document.documentElement;
37
- element.setAttribute(_constants.COLOR_MODE_ATTRIBUTE, e.matches ? 'dark' : 'light');
38
- };
39
-
40
19
  /**
41
20
  * Sets the theme globally at runtime. This updates the `data-theme` and `data-color-mode` attributes on your page's <html> tag.
42
21
  *
@@ -75,26 +54,31 @@ var setGlobalTheme = /*#__PURE__*/function () {
75
54
  _ref2$UNSAFE_themeOpt,
76
55
  UNSAFE_themeOptions,
77
56
  themeLoader,
57
+ themeState,
78
58
  themePreferences,
79
59
  loadingStrategy,
80
- themeAttributes,
60
+ loadingTasks,
61
+ mode,
62
+ attrOfMissingCustomStyles,
63
+ autoUnbind,
81
64
  _args3 = arguments;
82
65
  return _regenerator.default.wrap(function _callee3$(_context3) {
83
66
  while (1) switch (_context3.prev = _context3.next) {
84
67
  case 0:
85
68
  _ref2 = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {}, _ref2$colorMode = _ref2.colorMode, colorMode = _ref2$colorMode === void 0 ? _themeConfig.themeStateDefaults['colorMode'] : _ref2$colorMode, _ref2$dark = _ref2.dark, dark = _ref2$dark === void 0 ? _themeConfig.themeStateDefaults['dark'] : _ref2$dark, _ref2$light = _ref2.light, light = _ref2$light === void 0 ? _themeConfig.themeStateDefaults['light'] : _ref2$light, _ref2$shape = _ref2.shape, shape = _ref2$shape === void 0 ? _themeConfig.themeStateDefaults['shape'] : _ref2$shape, _ref2$spacing = _ref2.spacing, spacing = _ref2$spacing === void 0 ? _themeConfig.themeStateDefaults['spacing'] : _ref2$spacing, _ref2$typography = _ref2.typography, typography = _ref2$typography === void 0 ? _themeConfig.themeStateDefaults['typography'] : _ref2$typography, _ref2$UNSAFE_themeOpt = _ref2.UNSAFE_themeOptions, UNSAFE_themeOptions = _ref2$UNSAFE_themeOpt === void 0 ? _themeConfig.themeStateDefaults['UNSAFE_themeOptions'] : _ref2$UNSAFE_themeOpt;
86
69
  themeLoader = _args3.length > 1 ? _args3[1] : undefined;
87
- themePreferences = (0, _getThemePreferences.getThemePreferences)({
70
+ themeState = {
88
71
  colorMode: colorMode,
89
72
  dark: dark,
90
73
  light: light,
91
74
  shape: shape,
92
75
  spacing: spacing,
93
- typography: typography
94
- });
95
- loadingStrategy = themeLoader ? themeLoader : _themeLoading.loadAndAppendThemeCss;
96
- _context3.next = 6;
97
- return Promise.all([].concat((0, _toConsumableArray2.default)(themePreferences.map( /*#__PURE__*/function () {
76
+ typography: typography,
77
+ UNSAFE_themeOptions: themeLoader ? undefined : UNSAFE_themeOptions
78
+ }; // Determine what to load and loading strategy
79
+ themePreferences = (0, _getThemePreferences.getThemePreferences)(themeState);
80
+ loadingStrategy = themeLoader ? themeLoader : _themeLoading.loadAndAppendThemeCss; // Load standard themes
81
+ loadingTasks = themePreferences.map( /*#__PURE__*/function () {
98
82
  var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(themeId) {
99
83
  return _regenerator.default.wrap(function _callee$(_context) {
100
84
  while (1) switch (_context.prev = _context.next) {
@@ -112,73 +96,45 @@ var setGlobalTheme = /*#__PURE__*/function () {
112
96
  return function (_x) {
113
97
  return _ref3.apply(this, arguments);
114
98
  };
115
- }())), [(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
116
- var mode, attrOfMissingCustomStyles, _yield$import, loadAndAppendCustomThemeCss;
117
- return _regenerator.default.wrap(function _callee2$(_context2) {
118
- while (1) switch (_context2.prev = _context2.next) {
119
- case 0:
120
- if (!(!themeLoader && UNSAFE_themeOptions && (0, _colorUtils.isValidBrandHex)(UNSAFE_themeOptions === null || UNSAFE_themeOptions === void 0 ? void 0 : UNSAFE_themeOptions.brandColor))) {
121
- _context2.next = 11;
122
- break;
123
- }
124
- mode = colorMode || _themeConfig.themeStateDefaults['colorMode'];
125
- attrOfMissingCustomStyles = (0, _customThemeLoadingUtils.findMissingCustomStyleElements)(UNSAFE_themeOptions, mode);
126
- if (!(attrOfMissingCustomStyles.length === 0)) {
127
- _context2.next = 5;
128
- break;
99
+ }()); // Load custom themes if needed
100
+ if (!themeLoader && UNSAFE_themeOptions && (0, _colorUtils.isValidBrandHex)(UNSAFE_themeOptions === null || UNSAFE_themeOptions === void 0 ? void 0 : UNSAFE_themeOptions.brandColor)) {
101
+ mode = colorMode || _themeConfig.themeStateDefaults['colorMode'];
102
+ attrOfMissingCustomStyles = (0, _customThemeLoadingUtils.findMissingCustomStyleElements)(UNSAFE_themeOptions, mode);
103
+ if (attrOfMissingCustomStyles.length > 0) {
104
+ // Load custom theme styles
105
+ loadingTasks.push((0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
106
+ var _yield$import, loadAndAppendCustomThemeCss;
107
+ return _regenerator.default.wrap(function _callee2$(_context2) {
108
+ while (1) switch (_context2.prev = _context2.next) {
109
+ case 0:
110
+ _context2.next = 2;
111
+ return Promise.resolve().then(function () {
112
+ return _interopRequireWildcard(require( /* webpackChunkName: "@atlaskit-internal_atlassian-custom-theme" */
113
+ './custom-theme'));
114
+ });
115
+ case 2:
116
+ _yield$import = _context2.sent;
117
+ loadAndAppendCustomThemeCss = _yield$import.loadAndAppendCustomThemeCss;
118
+ loadAndAppendCustomThemeCss({
119
+ colorMode: attrOfMissingCustomStyles.length === 2 ? 'auto' :
120
+ // only load the missing custom theme styles
121
+ attrOfMissingCustomStyles[0],
122
+ UNSAFE_themeOptions: UNSAFE_themeOptions
123
+ });
124
+ case 5:
125
+ case "end":
126
+ return _context2.stop();
129
127
  }
130
- return _context2.abrupt("return", false);
131
- case 5:
132
- _context2.next = 7;
133
- return Promise.resolve().then(function () {
134
- return _interopRequireWildcard(require( /* webpackChunkName: "@atlaskit-internal_atlassian-custom-theme" */
135
- './custom-theme'));
136
- });
137
- case 7:
138
- _yield$import = _context2.sent;
139
- loadAndAppendCustomThemeCss = _yield$import.loadAndAppendCustomThemeCss;
140
- _context2.next = 11;
141
- return loadAndAppendCustomThemeCss({
142
- colorMode: attrOfMissingCustomStyles.length === 2 ? 'auto' :
143
- // only load the missing custom theme styles
144
- attrOfMissingCustomStyles[0],
145
- UNSAFE_themeOptions: UNSAFE_themeOptions
146
- });
147
- case 11:
148
- case "end":
149
- return _context2.stop();
150
- }
151
- }, _callee2);
152
- }))()]));
153
- case 6:
154
- if (colorMode === 'auto' && darkModeMql) {
155
- colorMode = darkModeMql.matches ? 'dark' : 'light';
156
- // Add an event listener for changes to the system theme.
157
- // If the function exists, it will not be added again.
158
- unbindThemeChangeListener = (0, _bindEventListener.bind)(darkModeMql, {
159
- type: 'change',
160
- listener: checkNativeListener
161
- });
162
- } else {
163
- unbindThemeChangeListener();
128
+ }, _callee2);
129
+ }))());
130
+ }
164
131
  }
165
- themeAttributes = (0, _getThemeHtmlAttrs.default)({
166
- colorMode: colorMode,
167
- dark: dark,
168
- light: light,
169
- shape: shape,
170
- spacing: spacing,
171
- typography: typography,
172
- UNSAFE_themeOptions: themeLoader ? undefined : UNSAFE_themeOptions
173
- });
174
- Object.entries(themeAttributes).forEach(function (_ref5) {
175
- var _ref6 = (0, _slicedToArray2.default)(_ref5, 2),
176
- key = _ref6[0],
177
- value = _ref6[1];
178
- document.documentElement.setAttribute(key, value);
179
- });
180
- return _context3.abrupt("return", unbindThemeChangeListener);
181
- case 10:
132
+ _context3.next = 9;
133
+ return Promise.all(loadingTasks);
134
+ case 9:
135
+ autoUnbind = (0, _configurePage.default)(themeState);
136
+ return _context3.abrupt("return", autoUnbind);
137
+ case 11:
182
138
  case "end":
183
139
  return _context3.stop();
184
140
  }
@@ -187,6 +187,11 @@ var themeStateDefaults = {
187
187
  typography: undefined,
188
188
  UNSAFE_themeOptions: undefined
189
189
  };
190
+
191
+ /**
192
+ * Represents theme state once mounted to the page
193
+ * (the page doesn't have an "auto" color mode, it's either light or dark)
194
+ */
190
195
  exports.themeStateDefaults = themeStateDefaults;
191
196
  var _default = themeConfig;
192
197
  exports.default = _default;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
9
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+ var _bindEventListener = require("bind-event-listener");
12
+ var _constants = require("../constants");
13
+ var _themeLoading = require("./theme-loading");
14
+ var isMatchMediaAvailable = typeof window !== 'undefined' && 'matchMedia' in window;
15
+
16
+ /**
17
+ * Updates the current theme when the system theme changes. Should be bound
18
+ * to an event listener listening on the '(prefers-color-scheme: dark)' query
19
+ * @param e The event representing a change in system theme.
20
+ */
21
+ function checkNativeListener(e) {
22
+ var element = document.documentElement;
23
+ element.setAttribute(_constants.COLOR_MODE_ATTRIBUTE, e.matches ? 'dark' : 'light');
24
+ }
25
+ var darkModeMql = isMatchMediaAvailable && window.matchMedia(_themeLoading.darkModeMediaQuery);
26
+ var ColorModeObserver = /*#__PURE__*/function () {
27
+ function ColorModeObserver() {
28
+ (0, _classCallCheck2.default)(this, ColorModeObserver);
29
+ (0, _defineProperty2.default)(this, "unbindThemeChangeListener", null);
30
+ }
31
+ (0, _createClass2.default)(ColorModeObserver, [{
32
+ key: "getColorMode",
33
+ value: function getColorMode() {
34
+ if (!darkModeMql) {
35
+ return 'light';
36
+ }
37
+ return darkModeMql !== null && darkModeMql !== void 0 && darkModeMql.matches ? 'dark' : 'light';
38
+ }
39
+ }, {
40
+ key: "bind",
41
+ value: function bind() {
42
+ if (darkModeMql && this.unbindThemeChangeListener === null) {
43
+ this.unbindThemeChangeListener = (0, _bindEventListener.bind)(darkModeMql, {
44
+ type: 'change',
45
+ listener: checkNativeListener
46
+ });
47
+ }
48
+ }
49
+ }, {
50
+ key: "unbind",
51
+ value: function unbind() {
52
+ if (this.unbindThemeChangeListener) {
53
+ this.unbindThemeChangeListener();
54
+ this.unbindThemeChangeListener = null;
55
+ }
56
+ }
57
+ }]);
58
+ return ColorModeObserver;
59
+ }();
60
+ /**
61
+ * A singleton color mode observer - binds "auto" switching logic to a single `mediaQueryList` listener
62
+ * that can be unbound by any consumer when no longer needed.
63
+ */
64
+ var SingletonColorModeObserver = new ColorModeObserver();
65
+ var _default = SingletonColorModeObserver;
66
+ exports.default = _default;