@atlaskit/tokens 1.11.2 → 1.12.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 (116) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/cjs/artifacts/atlassian-dark-token-value-for-contrast-check.js +24 -0
  3. package/dist/cjs/artifacts/atlassian-light-token-value-for-contrast-check.js +24 -0
  4. package/dist/cjs/artifacts/themes/atlassian-dark-new-input-border.js +2 -2
  5. package/dist/cjs/artifacts/tokens-raw/atlassian-dark-new-input-border.js +3 -3
  6. package/dist/cjs/constants.js +3 -1
  7. package/dist/cjs/custom-theme.js +108 -0
  8. package/dist/cjs/get-token-value.js +1 -1
  9. package/dist/cjs/get-token.js +1 -1
  10. package/dist/cjs/set-global-theme.js +156 -59
  11. package/dist/cjs/tokens/atlassian-dark-new-input-border/color/border.js +1 -1
  12. package/dist/cjs/utils/color-utils.js +178 -0
  13. package/dist/cjs/utils/custom-theme-loading-utils.js +47 -0
  14. package/dist/cjs/utils/custom-theme-token-contrast-check.js +74 -0
  15. package/dist/cjs/utils/generate-custom-color-ramp.js +213 -0
  16. package/dist/cjs/utils/hash.js +17 -0
  17. package/dist/cjs/utils/hct-color-utils/color-utils.js +310 -0
  18. package/dist/cjs/utils/hct-color-utils/contrast.js +188 -0
  19. package/dist/cjs/utils/hct-color-utils/hct.js +1036 -0
  20. package/dist/cjs/utils/hct-color-utils/index.js +32 -0
  21. package/dist/cjs/utils/hct-color-utils/math-utils.js +159 -0
  22. package/dist/cjs/utils/theme-loading.js +1 -1
  23. package/dist/cjs/utils/theme-state-transformer.js +1 -1
  24. package/dist/cjs/version.json +1 -1
  25. package/dist/es2019/artifacts/atlassian-dark-token-value-for-contrast-check.js +17 -0
  26. package/dist/es2019/artifacts/atlassian-light-token-value-for-contrast-check.js +17 -0
  27. package/dist/es2019/artifacts/themes/atlassian-dark-new-input-border.js +2 -2
  28. package/dist/es2019/artifacts/tokens-raw/atlassian-dark-new-input-border.js +3 -3
  29. package/dist/es2019/constants.js +1 -0
  30. package/dist/es2019/custom-theme.js +77 -0
  31. package/dist/es2019/get-token-value.js +1 -1
  32. package/dist/es2019/get-token.js +1 -1
  33. package/dist/es2019/set-global-theme.js +67 -13
  34. package/dist/es2019/tokens/atlassian-dark-new-input-border/color/border.js +1 -1
  35. package/dist/es2019/utils/color-utils.js +154 -0
  36. package/dist/es2019/utils/custom-theme-loading-utils.js +31 -0
  37. package/dist/es2019/utils/custom-theme-token-contrast-check.js +68 -0
  38. package/dist/es2019/utils/generate-custom-color-ramp.js +187 -0
  39. package/dist/es2019/utils/hash.js +10 -0
  40. package/dist/es2019/utils/hct-color-utils/color-utils.js +286 -0
  41. package/dist/es2019/utils/hct-color-utils/contrast.js +161 -0
  42. package/dist/es2019/utils/hct-color-utils/hct.js +931 -0
  43. package/dist/es2019/utils/hct-color-utils/index.js +3 -0
  44. package/dist/es2019/utils/hct-color-utils/math-utils.js +145 -0
  45. package/dist/es2019/utils/theme-loading.js +2 -2
  46. package/dist/es2019/utils/theme-state-transformer.js +3 -1
  47. package/dist/es2019/version.json +1 -1
  48. package/dist/esm/artifacts/atlassian-dark-token-value-for-contrast-check.js +17 -0
  49. package/dist/esm/artifacts/atlassian-light-token-value-for-contrast-check.js +17 -0
  50. package/dist/esm/artifacts/themes/atlassian-dark-new-input-border.js +2 -2
  51. package/dist/esm/artifacts/tokens-raw/atlassian-dark-new-input-border.js +3 -3
  52. package/dist/esm/constants.js +1 -0
  53. package/dist/esm/custom-theme.js +98 -0
  54. package/dist/esm/get-token-value.js +1 -1
  55. package/dist/esm/get-token.js +1 -1
  56. package/dist/esm/set-global-theme.js +149 -60
  57. package/dist/esm/tokens/atlassian-dark-new-input-border/color/border.js +1 -1
  58. package/dist/esm/utils/color-utils.js +162 -0
  59. package/dist/esm/utils/custom-theme-loading-utils.js +38 -0
  60. package/dist/esm/utils/custom-theme-token-contrast-check.js +65 -0
  61. package/dist/esm/utils/generate-custom-color-ramp.js +202 -0
  62. package/dist/esm/utils/hash.js +10 -0
  63. package/dist/esm/utils/hct-color-utils/color-utils.js +285 -0
  64. package/dist/esm/utils/hct-color-utils/contrast.js +181 -0
  65. package/dist/esm/utils/hct-color-utils/hct.js +1029 -0
  66. package/dist/esm/utils/hct-color-utils/index.js +3 -0
  67. package/dist/esm/utils/hct-color-utils/math-utils.js +145 -0
  68. package/dist/esm/utils/theme-loading.js +2 -2
  69. package/dist/esm/utils/theme-state-transformer.js +1 -1
  70. package/dist/esm/version.json +1 -1
  71. package/dist/types/artifacts/atlassian-dark-token-value-for-contrast-check.d.ts +17 -0
  72. package/dist/types/artifacts/atlassian-light-token-value-for-contrast-check.d.ts +17 -0
  73. package/dist/types/artifacts/themes/atlassian-dark-new-input-border.d.ts +2 -2
  74. package/dist/types/artifacts/tokens-raw/atlassian-dark-new-input-border.d.ts +1 -1
  75. package/dist/types/constants.d.ts +1 -0
  76. package/dist/types/custom-theme.d.ts +30 -0
  77. package/dist/types/index.d.ts +1 -0
  78. package/dist/types/set-global-theme.d.ts +9 -3
  79. package/dist/types/tokens/atlassian-dark/utility/utility.d.ts +1 -1
  80. package/dist/types/tokens/atlassian-light/utility/utility.d.ts +1 -1
  81. package/dist/types/tokens/default/utility/utility.d.ts +1 -1
  82. package/dist/types/utils/color-utils.d.ts +10 -0
  83. package/dist/types/utils/custom-theme-loading-utils.d.ts +11 -0
  84. package/dist/types/utils/custom-theme-token-contrast-check.d.ts +20 -0
  85. package/dist/types/utils/generate-custom-color-ramp.d.ts +19 -0
  86. package/dist/types/utils/hash.d.ts +1 -0
  87. package/dist/types/utils/hct-color-utils/color-utils.d.ts +131 -0
  88. package/dist/types/utils/hct-color-utils/contrast.d.ts +78 -0
  89. package/dist/types/utils/hct-color-utils/hct.d.ts +137 -0
  90. package/dist/types/utils/hct-color-utils/index.d.ts +3 -0
  91. package/dist/types/utils/hct-color-utils/math-utils.d.ts +86 -0
  92. package/dist/types-ts4.5/artifacts/atlassian-dark-token-value-for-contrast-check.d.ts +17 -0
  93. package/dist/types-ts4.5/artifacts/atlassian-light-token-value-for-contrast-check.d.ts +17 -0
  94. package/dist/types-ts4.5/artifacts/themes/atlassian-dark-new-input-border.d.ts +2 -2
  95. package/dist/types-ts4.5/artifacts/tokens-raw/atlassian-dark-new-input-border.d.ts +1 -1
  96. package/dist/types-ts4.5/constants.d.ts +1 -0
  97. package/dist/types-ts4.5/custom-theme.d.ts +30 -0
  98. package/dist/types-ts4.5/index.d.ts +1 -0
  99. package/dist/types-ts4.5/set-global-theme.d.ts +9 -3
  100. package/dist/types-ts4.5/tokens/atlassian-dark/utility/utility.d.ts +1 -1
  101. package/dist/types-ts4.5/tokens/atlassian-light/utility/utility.d.ts +1 -1
  102. package/dist/types-ts4.5/tokens/default/utility/utility.d.ts +1 -1
  103. package/dist/types-ts4.5/utils/color-utils.d.ts +27 -0
  104. package/dist/types-ts4.5/utils/custom-theme-loading-utils.d.ts +11 -0
  105. package/dist/types-ts4.5/utils/custom-theme-token-contrast-check.d.ts +20 -0
  106. package/dist/types-ts4.5/utils/generate-custom-color-ramp.d.ts +19 -0
  107. package/dist/types-ts4.5/utils/hash.d.ts +1 -0
  108. package/dist/types-ts4.5/utils/hct-color-utils/color-utils.d.ts +131 -0
  109. package/dist/types-ts4.5/utils/hct-color-utils/contrast.d.ts +78 -0
  110. package/dist/types-ts4.5/utils/hct-color-utils/hct.d.ts +137 -0
  111. package/dist/types-ts4.5/utils/hct-color-utils/index.d.ts +3 -0
  112. package/dist/types-ts4.5/utils/hct-color-utils/math-utils.d.ts +86 -0
  113. package/figma/atlassian-dark-new-input-border.json +1 -1
  114. package/package.json +35 -40
  115. package/report.api.md +24 -1
  116. package/tmp/api-report-tmp.d.ts +0 -1132
@@ -6,8 +6,11 @@ import _regeneratorRuntime from "@babel/runtime/regenerator";
6
6
  import { bind } from 'bind-event-listener';
7
7
  import noop from '@atlaskit/ds-lib/noop';
8
8
  import { getBooleanFF } from '@atlaskit/platform-feature-flags';
9
- import { COLOR_MODE_ATTRIBUTE, THEME_DATA_ATTRIBUTE } from './constants';
9
+ import { COLOR_MODE_ATTRIBUTE, CUSTOM_THEME_ATTRIBUTE, THEME_DATA_ATTRIBUTE } from './constants';
10
10
  import { themeIdsWithOverrides } from './theme-config';
11
+ import { isValidBrandHex } from './utils/color-utils';
12
+ import { findMissingCustomStyleElements } from './utils/custom-theme-loading-utils';
13
+ import { hash } from './utils/hash';
11
14
  import { loadAndAppendThemeCss, loadThemeCss } from './utils/theme-loading';
12
15
  import { themeObjectToString } from './utils/theme-state-transformer';
13
16
 
@@ -18,13 +21,14 @@ var isMatchMediaAvailable = typeof window !== 'undefined' && 'matchMedia' in win
18
21
  var darkModeMediaQuery = '(prefers-color-scheme: dark)';
19
22
  var darkModeMql = isMatchMediaAvailable && window.matchMedia(darkModeMediaQuery);
20
23
  var unbindThemeChangeListener = noop;
21
- var themeStateDefaults = {
24
+ export var themeStateDefaults = {
22
25
  colorMode: 'auto',
23
26
  dark: 'dark',
24
27
  light: 'light',
25
28
  shape: undefined,
26
29
  spacing: undefined,
27
- typography: undefined
30
+ typography: undefined,
31
+ UNSAFE_themeOptions: undefined
28
32
  };
29
33
 
30
34
  /**
@@ -72,6 +76,7 @@ var getThemePreferences = function getThemePreferences(themeState) {
72
76
  * @param {string} themeState.shape The shape theme to be applied.
73
77
  * @param {string} themeState.spacing The spacing theme to be applied.
74
78
  * @param {string} themeState.typography The typography theme to be applied.
79
+ * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
75
80
  * @param {function} themeLoader Callback function used to override the default theme loading functionality.
76
81
  *
77
82
  * @returns A Promise of an unbind function, that can be used to stop listening for changes to system theme.
@@ -82,7 +87,7 @@ var getThemePreferences = function getThemePreferences(themeState) {
82
87
  * ```
83
88
  */
84
89
  var setGlobalTheme = /*#__PURE__*/function () {
85
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
90
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
86
91
  var _ref2,
87
92
  _ref2$colorMode,
88
93
  colorMode,
@@ -96,16 +101,18 @@ var setGlobalTheme = /*#__PURE__*/function () {
96
101
  spacing,
97
102
  _ref2$typography,
98
103
  typography,
104
+ _ref2$UNSAFE_themeOpt,
105
+ UNSAFE_themeOptions,
99
106
  themeLoader,
100
107
  themePreferences,
101
108
  loadingStrategy,
102
109
  themeAttributes,
103
- _args2 = arguments;
104
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
105
- while (1) switch (_context2.prev = _context2.next) {
110
+ _args3 = arguments;
111
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
112
+ while (1) switch (_context3.prev = _context3.next) {
106
113
  case 0:
107
- _ref2 = _args2.length > 0 && _args2[0] !== undefined ? _args2[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;
108
- themeLoader = _args2.length > 1 ? _args2[1] : undefined;
114
+ _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;
115
+ themeLoader = _args3.length > 1 ? _args3[1] : undefined;
109
116
  themePreferences = getThemePreferences({
110
117
  colorMode: colorMode,
111
118
  dark: dark,
@@ -115,8 +122,8 @@ var setGlobalTheme = /*#__PURE__*/function () {
115
122
  typography: typography
116
123
  });
117
124
  loadingStrategy = themeLoader ? themeLoader : loadAndAppendThemeCss;
118
- _context2.next = 6;
119
- return Promise.all(themePreferences.map( /*#__PURE__*/function () {
125
+ _context3.next = 6;
126
+ return Promise.all([].concat(_toConsumableArray(themePreferences.map( /*#__PURE__*/function () {
120
127
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(themeId) {
121
128
  return _regeneratorRuntime.wrap(function _callee$(_context) {
122
129
  while (1) switch (_context.prev = _context.next) {
@@ -134,7 +141,42 @@ var setGlobalTheme = /*#__PURE__*/function () {
134
141
  return function (_x) {
135
142
  return _ref3.apply(this, arguments);
136
143
  };
137
- }()));
144
+ }())), [_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
145
+ var mode, attrOfMissingCustomStyles, _yield$import, loadAndAppendCustomThemeCss;
146
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
147
+ while (1) switch (_context2.prev = _context2.next) {
148
+ case 0:
149
+ if (!(!themeLoader && UNSAFE_themeOptions && isValidBrandHex(UNSAFE_themeOptions === null || UNSAFE_themeOptions === void 0 ? void 0 : UNSAFE_themeOptions.brandColor))) {
150
+ _context2.next = 11;
151
+ break;
152
+ }
153
+ mode = colorMode || themeStateDefaults['colorMode'];
154
+ attrOfMissingCustomStyles = findMissingCustomStyleElements(UNSAFE_themeOptions, mode);
155
+ if (!(attrOfMissingCustomStyles.length === 0)) {
156
+ _context2.next = 5;
157
+ break;
158
+ }
159
+ return _context2.abrupt("return", false);
160
+ case 5:
161
+ _context2.next = 7;
162
+ return import( /* webpackChunkName: "@atlaskit-internal_atlassian-custom-theme" */
163
+ './custom-theme');
164
+ case 7:
165
+ _yield$import = _context2.sent;
166
+ loadAndAppendCustomThemeCss = _yield$import.loadAndAppendCustomThemeCss;
167
+ _context2.next = 11;
168
+ return loadAndAppendCustomThemeCss({
169
+ colorMode: attrOfMissingCustomStyles.length === 2 ? 'auto' :
170
+ // only load the missing custom theme styles
171
+ attrOfMissingCustomStyles[0],
172
+ UNSAFE_themeOptions: UNSAFE_themeOptions
173
+ });
174
+ case 11:
175
+ case "end":
176
+ return _context2.stop();
177
+ }
178
+ }, _callee2);
179
+ }))()]));
138
180
  case 6:
139
181
  if (colorMode === 'auto' && darkModeMql) {
140
182
  colorMode = darkModeMql.matches ? 'dark' : 'light';
@@ -153,20 +195,21 @@ var setGlobalTheme = /*#__PURE__*/function () {
153
195
  light: light,
154
196
  shape: shape,
155
197
  spacing: spacing,
156
- typography: typography
198
+ typography: typography,
199
+ UNSAFE_themeOptions: themeLoader ? undefined : UNSAFE_themeOptions
157
200
  });
158
- Object.entries(themeAttributes).forEach(function (_ref4) {
159
- var _ref5 = _slicedToArray(_ref4, 2),
160
- key = _ref5[0],
161
- value = _ref5[1];
201
+ Object.entries(themeAttributes).forEach(function (_ref5) {
202
+ var _ref6 = _slicedToArray(_ref5, 2),
203
+ key = _ref6[0],
204
+ value = _ref6[1];
162
205
  document.documentElement.setAttribute(key, value);
163
206
  });
164
- return _context2.abrupt("return", unbindThemeChangeListener);
207
+ return _context3.abrupt("return", unbindThemeChangeListener);
165
208
  case 10:
166
209
  case "end":
167
- return _context2.stop();
210
+ return _context3.stop();
168
211
  }
169
- }, _callee2);
212
+ }, _callee3);
170
213
  }));
171
214
  return function setGlobalTheme() {
172
215
  return _ref.apply(this, arguments);
@@ -183,15 +226,16 @@ var setGlobalTheme = /*#__PURE__*/function () {
183
226
  * @param {string} themeState.shape The shape theme to be applied.
184
227
  * @param {string} themeState.spacing The spacing theme to be applied.
185
228
  * @param {string} themeState.typography The typography theme to be applied.
229
+ * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
186
230
  *
187
231
  * @returns A Promise of an object array, containing theme IDs, data-attributes to attach to the theme, and the theme CSS.
188
- * If an error is encountered while loading themes, the themes arrav will be emptv.
232
+ * If an error is encountered while loading themes, the themes array will be empty.
189
233
  */
190
234
  export var getThemeStyles = /*#__PURE__*/function () {
191
- var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(preferences) {
235
+ var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(preferences) {
192
236
  var themePreferences, results;
193
- return _regeneratorRuntime.wrap(function _callee4$(_context4) {
194
- while (1) switch (_context4.prev = _context4.next) {
237
+ return _regeneratorRuntime.wrap(function _callee6$(_context6) {
238
+ while (1) switch (_context6.prev = _context6.next) {
195
239
  case 0:
196
240
  if (preferences === 'all') {
197
241
  themePreferences = themeIdsWithOverrides;
@@ -205,19 +249,19 @@ export var getThemeStyles = /*#__PURE__*/function () {
205
249
  typography: (preferences === null || preferences === void 0 ? void 0 : preferences.typography) || themeStateDefaults['typography']
206
250
  });
207
251
  }
208
- _context4.next = 3;
209
- return Promise.all(themePreferences.map( /*#__PURE__*/function () {
210
- var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(themeId) {
252
+ _context6.next = 3;
253
+ return Promise.all([].concat(_toConsumableArray(themePreferences.map( /*#__PURE__*/function () {
254
+ var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(themeId) {
211
255
  var css;
212
- return _regeneratorRuntime.wrap(function _callee3$(_context3) {
213
- while (1) switch (_context3.prev = _context3.next) {
256
+ return _regeneratorRuntime.wrap(function _callee4$(_context4) {
257
+ while (1) switch (_context4.prev = _context4.next) {
214
258
  case 0:
215
- _context3.prev = 0;
216
- _context3.next = 3;
259
+ _context4.prev = 0;
260
+ _context4.next = 3;
217
261
  return loadThemeCss(themeId);
218
262
  case 3:
219
- css = _context3.sent;
220
- return _context3.abrupt("return", {
263
+ css = _context4.sent;
264
+ return _context4.abrupt("return", {
221
265
  id: themeId,
222
266
  attrs: {
223
267
  'data-theme': themeId
@@ -225,32 +269,68 @@ export var getThemeStyles = /*#__PURE__*/function () {
225
269
  css: css
226
270
  });
227
271
  case 7:
228
- _context3.prev = 7;
229
- _context3.t0 = _context3["catch"](0);
230
- return _context3.abrupt("return", undefined);
272
+ _context4.prev = 7;
273
+ _context4.t0 = _context4["catch"](0);
274
+ return _context4.abrupt("return", undefined);
231
275
  case 10:
232
276
  case "end":
233
- return _context3.stop();
277
+ return _context4.stop();
234
278
  }
235
- }, _callee3, null, [[0, 7]]);
279
+ }, _callee4, null, [[0, 7]]);
236
280
  }));
237
281
  return function (_x3) {
238
- return _ref7.apply(this, arguments);
282
+ return _ref8.apply(this, arguments);
239
283
  };
240
- }()));
284
+ }())), [
285
+ // Add custom themes if they're present
286
+ _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
287
+ var _preferences$UNSAFE_t;
288
+ var _yield$import2, getCustomThemeStyles, customThemeStyles;
289
+ return _regeneratorRuntime.wrap(function _callee5$(_context5) {
290
+ while (1) switch (_context5.prev = _context5.next) {
291
+ case 0:
292
+ if (!(preferences !== 'all' && preferences !== null && preferences !== void 0 && preferences.UNSAFE_themeOptions && isValidBrandHex(preferences === null || preferences === void 0 ? void 0 : (_preferences$UNSAFE_t = preferences.UNSAFE_themeOptions) === null || _preferences$UNSAFE_t === void 0 ? void 0 : _preferences$UNSAFE_t.brandColor))) {
293
+ _context5.next = 15;
294
+ break;
295
+ }
296
+ _context5.prev = 1;
297
+ _context5.next = 4;
298
+ return import( /* webpackChunkName: "@atlaskit-internal_atlassian-custom-theme" */
299
+ './custom-theme');
300
+ case 4:
301
+ _yield$import2 = _context5.sent;
302
+ getCustomThemeStyles = _yield$import2.getCustomThemeStyles;
303
+ _context5.next = 8;
304
+ return getCustomThemeStyles({
305
+ colorMode: (preferences === null || preferences === void 0 ? void 0 : preferences.colorMode) || themeStateDefaults['colorMode'],
306
+ UNSAFE_themeOptions: preferences === null || preferences === void 0 ? void 0 : preferences.UNSAFE_themeOptions
307
+ });
308
+ case 8:
309
+ customThemeStyles = _context5.sent;
310
+ return _context5.abrupt("return", customThemeStyles);
311
+ case 12:
312
+ _context5.prev = 12;
313
+ _context5.t0 = _context5["catch"](1);
314
+ return _context5.abrupt("return", undefined);
315
+ case 15:
316
+ case "end":
317
+ return _context5.stop();
318
+ }
319
+ }, _callee5, null, [[1, 12]]);
320
+ }))()]));
241
321
  case 3:
242
- results = _context4.sent;
243
- return _context4.abrupt("return", results.filter(function (theme) {
322
+ results = _context6.sent;
323
+ return _context6.abrupt("return", results.flat().filter(function (theme) {
244
324
  return theme !== undefined;
245
325
  }));
246
326
  case 5:
247
327
  case "end":
248
- return _context4.stop();
328
+ return _context6.stop();
249
329
  }
250
- }, _callee4);
330
+ }, _callee6);
251
331
  }));
252
332
  return function getThemeStyles(_x2) {
253
- return _ref6.apply(this, arguments);
333
+ return _ref7.apply(this, arguments);
254
334
  };
255
335
  }();
256
336
 
@@ -264,24 +344,27 @@ export var getThemeStyles = /*#__PURE__*/function () {
264
344
  * @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'.
265
345
  * @param {string} themeState.spacing The spacing theme to be applied.
266
346
  * @param {string} themeState.typography The typography theme to be applied.
347
+ * @param {Object} themeState.UNSAFE_themeOptions The custom branding options to be used for custom theme generation
267
348
  *
268
349
  * @returns {Object} Object of HTML attributes to be applied to the document root
269
350
  */
270
351
  export var getThemeHtmlAttrs = function getThemeHtmlAttrs() {
271
- var _ref9;
272
- var _ref8 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
273
- _ref8$colorMode = _ref8.colorMode,
274
- colorMode = _ref8$colorMode === void 0 ? themeStateDefaults['colorMode'] : _ref8$colorMode,
275
- _ref8$dark = _ref8.dark,
276
- dark = _ref8$dark === void 0 ? themeStateDefaults['dark'] : _ref8$dark,
277
- _ref8$light = _ref8.light,
278
- light = _ref8$light === void 0 ? themeStateDefaults['light'] : _ref8$light,
279
- _ref8$shape = _ref8.shape,
280
- shape = _ref8$shape === void 0 ? themeStateDefaults['shape'] : _ref8$shape,
281
- _ref8$spacing = _ref8.spacing,
282
- spacing = _ref8$spacing === void 0 ? themeStateDefaults['spacing'] : _ref8$spacing,
283
- _ref8$typography = _ref8.typography,
284
- typography = _ref8$typography === void 0 ? themeStateDefaults['typography'] : _ref8$typography;
352
+ var _result;
353
+ var _ref10 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
354
+ _ref10$colorMode = _ref10.colorMode,
355
+ colorMode = _ref10$colorMode === void 0 ? themeStateDefaults['colorMode'] : _ref10$colorMode,
356
+ _ref10$dark = _ref10.dark,
357
+ dark = _ref10$dark === void 0 ? themeStateDefaults['dark'] : _ref10$dark,
358
+ _ref10$light = _ref10.light,
359
+ light = _ref10$light === void 0 ? themeStateDefaults['light'] : _ref10$light,
360
+ _ref10$shape = _ref10.shape,
361
+ shape = _ref10$shape === void 0 ? themeStateDefaults['shape'] : _ref10$shape,
362
+ _ref10$spacing = _ref10.spacing,
363
+ spacing = _ref10$spacing === void 0 ? themeStateDefaults['spacing'] : _ref10$spacing,
364
+ _ref10$typography = _ref10.typography,
365
+ typography = _ref10$typography === void 0 ? themeStateDefaults['typography'] : _ref10$typography,
366
+ _ref10$UNSAFE_themeOp = _ref10.UNSAFE_themeOptions,
367
+ UNSAFE_themeOptions = _ref10$UNSAFE_themeOp === void 0 ? themeStateDefaults['UNSAFE_themeOptions'] : _ref10$UNSAFE_themeOp;
285
368
  var themePreferences = {
286
369
  dark: dark,
287
370
  light: light,
@@ -301,7 +384,13 @@ export var getThemeHtmlAttrs = function getThemeHtmlAttrs() {
301
384
  };
302
385
  }
303
386
  var themeAttribute = themeObjectToString(themePreferences);
304
- return _ref9 = {}, _defineProperty(_ref9, THEME_DATA_ATTRIBUTE, themeAttribute), _defineProperty(_ref9, COLOR_MODE_ATTRIBUTE, colorMode === 'auto' ? defaultColorMode : colorMode), _ref9;
387
+ var result = (_result = {}, _defineProperty(_result, THEME_DATA_ATTRIBUTE, themeAttribute), _defineProperty(_result, COLOR_MODE_ATTRIBUTE, colorMode === 'auto' ? defaultColorMode : colorMode), _result);
388
+ if (UNSAFE_themeOptions && isValidBrandHex(UNSAFE_themeOptions.brandColor)) {
389
+ var optionString = JSON.stringify(UNSAFE_themeOptions);
390
+ var uniqueId = hash(optionString);
391
+ result[CUSTOM_THEME_ATTRIBUTE] = uniqueId;
392
+ }
393
+ return result;
305
394
  };
306
395
 
307
396
  /**
@@ -2,7 +2,7 @@ var color = {
2
2
  color: {
3
3
  border: {
4
4
  input: {
5
- value: 'DarkNeutral500'
5
+ value: 'DarkNeutral600'
6
6
  }
7
7
  }
8
8
  }
@@ -0,0 +1,162 @@
1
+ // valid hex color with 6 digits
2
+ export var isValidBrandHex = function isValidBrandHex(hex) {
3
+ return /^#[0-9A-F]{6}$/i.test(hex);
4
+ };
5
+
6
+ // valid hex color with 4, 6 or 8 digits
7
+ var isValidHex = function isValidHex(hex) {
8
+ return /^#([A-Fa-f0-9]{3,4}){1,2}$/.test(hex);
9
+ };
10
+ export function rgbToHex(r, g, b) {
11
+ return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
12
+ }
13
+ export function getAlpha(hex) {
14
+ if (hex.length === 9) {
15
+ var int = parseInt(hex.slice(7, 9), 16) / 255;
16
+ return Number(parseFloat(int.toString()).toFixed(2));
17
+ }
18
+ return 1;
19
+ }
20
+ export function hexToRgbA(hex) {
21
+ if (!isValidHex(hex)) {
22
+ throw new Error('Invalid HEX');
23
+ }
24
+ var c;
25
+ c = hex.substring(1).split('');
26
+ if (c.length === 3) {
27
+ c = [c[0], c[0], c[1], c[1], c[2], c[2]];
28
+ }
29
+ c = '0x' + c.join('');
30
+ return [c >> 16 & 255, c >> 8 & 255, c & 255, getAlpha(hex)];
31
+ }
32
+ export function hexToRgb(hex) {
33
+ if (!isValidHex(hex)) {
34
+ throw new Error('Invalid HEX');
35
+ }
36
+ var c;
37
+ c = hex.substring(1).split('');
38
+ if (c.length === 3) {
39
+ c = [c[0], c[0], c[1], c[1], c[2], c[2]];
40
+ }
41
+ c = '0x' + c.join('');
42
+ return [c >> 16 & 255, c >> 8 & 255, c & 255];
43
+ }
44
+ export function hexToHSL(hex) {
45
+ if (!isValidHex(hex)) {
46
+ throw new Error('Invalid HEX');
47
+ }
48
+ var r = 0,
49
+ g = 0,
50
+ b = 0;
51
+ if (hex.length === 4) {
52
+ r = '0x' + hex[1] + hex[1];
53
+ g = '0x' + hex[2] + hex[2];
54
+ b = '0x' + hex[3] + hex[3];
55
+ } else if (hex.length === 7) {
56
+ r = '0x' + hex[1] + hex[2];
57
+ g = '0x' + hex[3] + hex[4];
58
+ b = '0x' + hex[5] + hex[6];
59
+ }
60
+ // Then to HSL
61
+ r /= 255;
62
+ g /= 255;
63
+ b /= 255;
64
+ var cmin = Math.min(r, g, b),
65
+ cmax = Math.max(r, g, b),
66
+ delta = cmax - cmin,
67
+ h = 0,
68
+ s = 0,
69
+ l = 0;
70
+ if (delta === 0) {
71
+ h = 0;
72
+ } else if (cmax === r) {
73
+ h = (g - b) / delta % 6;
74
+ } else if (cmax === g) {
75
+ h = (b - r) / delta + 2;
76
+ } else {
77
+ h = (r - g) / delta + 4;
78
+ }
79
+ h = Math.round(h * 60);
80
+ if (h < 0) {
81
+ h += 360;
82
+ }
83
+ l = (cmax + cmin) / 2;
84
+ s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
85
+ s = +(s * 100).toFixed(1);
86
+ l = +(l * 100).toFixed(1);
87
+ return [h, s, l];
88
+ }
89
+ export function HSLToRGB(h, s, l) {
90
+ s /= 100;
91
+ l /= 100;
92
+ var k = function k(n) {
93
+ return (n + h / 30) % 12;
94
+ };
95
+ var a = s * Math.min(l, 1 - l);
96
+ var f = function f(n) {
97
+ return l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
98
+ };
99
+ return [255 * f(0), 255 * f(8), 255 * f(4)];
100
+ }
101
+ export function relativeLuminanceW3C(r, g, b) {
102
+ var RsRGB = r / 255;
103
+ var GsRGB = g / 255;
104
+ var BsRGB = b / 255;
105
+ var R = RsRGB <= 0.03928 ? RsRGB / 12.92 : Math.pow((RsRGB + 0.055) / 1.055, 2.4);
106
+ var G = GsRGB <= 0.03928 ? GsRGB / 12.92 : Math.pow((GsRGB + 0.055) / 1.055, 2.4);
107
+ var B = BsRGB <= 0.03928 ? BsRGB / 12.92 : Math.pow((BsRGB + 0.055) / 1.055, 2.4);
108
+
109
+ // For the sRGB colorspace, the relative luminance of a color is defined as:
110
+ var L = 0.2126 * R + 0.7152 * G + 0.0722 * B;
111
+ return L;
112
+ }
113
+ export function getContrastRatio(foreground, background) {
114
+ if (!isValidHex(foreground) || !isValidHex(background)) {
115
+ throw new Error('Invalid HEX');
116
+ }
117
+ var foregroundRgb = hexToRgb(foreground);
118
+ var backgroundRgb = hexToRgb(background);
119
+ var foregroundLuminance = relativeLuminanceW3C(foregroundRgb[0], foregroundRgb[1], foregroundRgb[2]);
120
+ var backgroundLuminance = relativeLuminanceW3C(backgroundRgb[0], backgroundRgb[1], backgroundRgb[2]);
121
+ // calculate the color contrast ratio
122
+ var brightest = Math.max(foregroundLuminance, backgroundLuminance);
123
+ var darkest = Math.min(foregroundLuminance, backgroundLuminance);
124
+ return (brightest + 0.05) / (darkest + 0.05);
125
+ }
126
+ export function deltaE(rgbA, rgbB) {
127
+ var labA = rgbToLab(rgbA);
128
+ var labB = rgbToLab(rgbB);
129
+ var deltaL = labA[0] - labB[0];
130
+ var deltaA = labA[1] - labB[1];
131
+ var deltaB = labA[2] - labB[2];
132
+ var c1 = Math.sqrt(labA[1] * labA[1] + labA[2] * labA[2]);
133
+ var c2 = Math.sqrt(labB[1] * labB[1] + labB[2] * labB[2]);
134
+ var deltaC = c1 - c2;
135
+ var deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC;
136
+ deltaH = deltaH < 0 ? 0 : Math.sqrt(deltaH);
137
+ var sc = 1.0 + 0.045 * c1;
138
+ var sh = 1.0 + 0.015 * c1;
139
+ var deltaLKlsl = deltaL / 1.0;
140
+ var deltaCkcsc = deltaC / sc;
141
+ var deltaHkhsh = deltaH / sh;
142
+ var i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh;
143
+ return i < 0 ? 0 : Math.sqrt(i);
144
+ }
145
+ function rgbToLab(rgb) {
146
+ var r = rgb[0] / 255,
147
+ g = rgb[1] / 255,
148
+ b = rgb[2] / 255,
149
+ x,
150
+ y,
151
+ z;
152
+ r = r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
153
+ g = g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
154
+ b = b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
155
+ x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
156
+ y = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 1.0;
157
+ z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
158
+ x = x > 0.008856 ? Math.pow(x, 1 / 3) : 7.787 * x + 16 / 116;
159
+ y = y > 0.008856 ? Math.pow(y, 1 / 3) : 7.787 * y + 16 / 116;
160
+ z = z > 0.008856 ? Math.pow(z, 1 / 3) : 7.787 * z + 16 / 116;
161
+ return [116 * y - 16, 500 * (x - y), 200 * (y - z)];
162
+ }
@@ -0,0 +1,38 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
3
+ import tokens from '../artifacts/token-names';
4
+ import { CUSTOM_THEME_ATTRIBUTE, THEME_DATA_ATTRIBUTE } from '../constants';
5
+ import { hash } from './hash';
6
+ export function findMissingCustomStyleElements(UNSAFE_themeOptions, mode) {
7
+ var optionString = JSON.stringify(UNSAFE_themeOptions);
8
+ var uniqueId = hash(optionString);
9
+ var attrOfMissingCustomStyles = [];
10
+ (mode === 'auto' ? ['light', 'dark'] : [mode]).forEach(function (themeId) {
11
+ var element = document.head.querySelector("style[".concat(CUSTOM_THEME_ATTRIBUTE, "=\"").concat(uniqueId, "\"][").concat(THEME_DATA_ATTRIBUTE, "=\"").concat(themeId, "\"]"));
12
+ if (element) {
13
+ // Append the existing custom styles to take precedence over others
14
+ document.head.appendChild(element);
15
+ } else {
16
+ attrOfMissingCustomStyles.push(themeId);
17
+ }
18
+ });
19
+ return attrOfMissingCustomStyles;
20
+ }
21
+ export function limitSizeOfCustomStyleElements(sizeThreshold) {
22
+ var styleTags = _toConsumableArray(document.head.querySelectorAll("style[".concat(CUSTOM_THEME_ATTRIBUTE, "][").concat(THEME_DATA_ATTRIBUTE, "]")));
23
+ if (styleTags.length < sizeThreshold) {
24
+ return;
25
+ }
26
+ styleTags.slice(0, styleTags.length - (sizeThreshold - 1)).forEach(function (element) {
27
+ return element.remove();
28
+ });
29
+ }
30
+ export function reduceTokenMap(tokenMap, themeRamp) {
31
+ return Object.entries(tokenMap).reduce(function (acc, _ref) {
32
+ var _ref2 = _slicedToArray(_ref, 2),
33
+ key = _ref2[0],
34
+ value = _ref2[1];
35
+ var cssVar = tokens[key];
36
+ return cssVar ? "".concat(acc, "\n ").concat(cssVar, ": ").concat(themeRamp[value], ";") : acc;
37
+ }, '');
38
+ }
@@ -0,0 +1,65 @@
1
+ import tokenValuesDark from '../artifacts/atlassian-dark-token-value-for-contrast-check';
2
+ import tokenValuesLight from '../artifacts/atlassian-light-token-value-for-contrast-check';
3
+ import { getContrastRatio } from './color-utils';
4
+ export var additionalChecks = [{
5
+ foreground: 'color.text.brand',
6
+ backgroundLight: 'elevation.surface.sunken',
7
+ backgroundDark: 'elevation.surface.overlay',
8
+ desiredContrast: 4.5,
9
+ updatedTokens: [
10
+ // In light mode: darken the following tokens by one base token
11
+ // In dark mode: lighten the following tokens by one base token
12
+ 'color.text.brand', 'color.text.selected', 'color.link', 'color.link.pressed', 'color.icon.brand', 'color.icon.selected']
13
+ }, {
14
+ foreground: 'color.text.selected',
15
+ backgroundLight: 'color.background.selected',
16
+ backgroundDark: 'color.background.selected',
17
+ desiredContrast: 4.5,
18
+ // In light mode: darken the following tokens by one base token
19
+ // In dark mode: lighten the following tokens by one base token
20
+ updatedTokens: ['color.text.selected', 'color.icon.selected']
21
+ }, {
22
+ foreground: 'color.border.brand',
23
+ backgroundLight: 'elevation.surface.sunken',
24
+ backgroundDark: 'elevation.surface.overlay',
25
+ desiredContrast: 3,
26
+ // In light mode: darken the following tokens by one base token
27
+ // In dark mode: lighten the following tokens by one base toke
28
+ updatedTokens: ['color.border.brand', 'color.border.selected']
29
+ }, {
30
+ foreground: 'color.chart.brand',
31
+ backgroundLight: 'elevation.surface.sunken',
32
+ backgroundDark: 'elevation.surface.overlay',
33
+ desiredContrast: 3,
34
+ // In light mode: darken the following tokens by one base token
35
+ // In dark mode: lighten the following tokens by one base token
36
+ updatedTokens: ['color.chart.brand', 'color.chart.brand.hovered']
37
+ }];
38
+ var getColorFromTokenRaw = function getColorFromTokenRaw(tokenName, mode) {
39
+ return mode === 'light' ? tokenValuesLight[tokenName] : tokenValuesDark[tokenName];
40
+ };
41
+ export var additionalContrastChecker = function additionalContrastChecker(_ref) {
42
+ var customThemeTokenMap = _ref.customThemeTokenMap,
43
+ mode = _ref.mode,
44
+ themeRamp = _ref.themeRamp;
45
+ var updatedCustomThemeTokenMap = {};
46
+ var brandTokens = Object.keys(customThemeTokenMap);
47
+ additionalChecks.forEach(function (pairing) {
48
+ var backgroundLight = pairing.backgroundLight,
49
+ backgroundDark = pairing.backgroundDark,
50
+ foreground = pairing.foreground,
51
+ desiredContrast = pairing.desiredContrast,
52
+ updatedTokens = pairing.updatedTokens;
53
+ var background = mode === 'light' ? backgroundLight : backgroundDark;
54
+ var foregroundColor = brandTokens.includes(foreground) ? themeRamp[customThemeTokenMap[foreground]] : getColorFromTokenRaw(foreground, mode);
55
+ var backgroundColor = brandTokens.includes(background) ? themeRamp[customThemeTokenMap[background]] : getColorFromTokenRaw(background, mode);
56
+ var contrast = getContrastRatio(foregroundColor, backgroundColor);
57
+ if (contrast <= desiredContrast) {
58
+ updatedTokens.forEach(function (token) {
59
+ var rampValue = customThemeTokenMap[token];
60
+ updatedCustomThemeTokenMap[token] = mode === 'light' ? rampValue + 1 : rampValue - 1;
61
+ });
62
+ }
63
+ });
64
+ return updatedCustomThemeTokenMap;
65
+ };