@atlaskit/tokens 0.13.5 → 1.1.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 (196) hide show
  1. package/CHANGELOG.md +305 -0
  2. package/README.md +7 -16
  3. package/dist/cjs/artifacts/palettes-raw/legacy-palette.js +0 -1
  4. package/dist/cjs/artifacts/palettes-raw/palette.js +0 -1
  5. package/dist/cjs/artifacts/palettes-raw/spacing-scale.js +0 -1
  6. package/dist/cjs/artifacts/palettes-raw/typography-palette.js +0 -1
  7. package/dist/cjs/artifacts/replacement-mapping.js +3 -611
  8. package/dist/cjs/artifacts/theme-import-map.js +56 -0
  9. package/dist/cjs/artifacts/themes/atlassian-dark.js +13 -0
  10. package/dist/cjs/artifacts/themes/atlassian-legacy-dark.js +13 -0
  11. package/dist/cjs/artifacts/themes/atlassian-legacy-light.js +13 -0
  12. package/dist/cjs/artifacts/themes/atlassian-light.js +13 -0
  13. package/dist/cjs/artifacts/themes/atlassian-spacing.js +13 -0
  14. package/dist/cjs/artifacts/themes/atlassian-typography.js +13 -0
  15. package/dist/cjs/artifacts/token-default-values.js +2 -170
  16. package/dist/cjs/artifacts/token-names.js +2 -170
  17. package/dist/cjs/artifacts/tokens-raw/atlassian-dark.js +1354 -4261
  18. package/dist/cjs/artifacts/tokens-raw/atlassian-legacy-dark.js +1352 -4259
  19. package/dist/cjs/artifacts/tokens-raw/atlassian-legacy-light.js +1396 -4283
  20. package/dist/cjs/artifacts/tokens-raw/atlassian-light.js +1365 -4252
  21. package/dist/cjs/artifacts/tokens-raw/atlassian-spacing.js +1 -1582
  22. package/dist/cjs/artifacts/tokens-raw/atlassian-typography.js +0 -1
  23. package/dist/cjs/artifacts/typescript/atlassian-dark-token-names.js +0 -1
  24. package/dist/cjs/artifacts/typescript/atlassian-light-token-names.js +0 -1
  25. package/dist/cjs/babel-plugin/index.js +0 -2
  26. package/dist/cjs/babel-plugin/plugin.js +17 -43
  27. package/dist/cjs/entry-points/babel-plugin.js +0 -2
  28. package/dist/cjs/entry-points/palettes-raw.js +0 -2
  29. package/dist/cjs/entry-points/rename-mapping.js +0 -2
  30. package/dist/cjs/entry-points/token-ids.js +0 -1
  31. package/dist/cjs/entry-points/token-names.js +0 -2
  32. package/dist/cjs/entry-points/tokens-raw.js +0 -5
  33. package/dist/cjs/get-global-theme.js +30 -0
  34. package/dist/cjs/get-token-value.js +1 -10
  35. package/dist/cjs/get-token.js +3 -13
  36. package/dist/cjs/index.js +46 -10
  37. package/dist/cjs/palettes/legacy-palette.js +1 -1
  38. package/dist/cjs/palettes/spacing-scale.js +1 -1
  39. package/dist/cjs/palettes/typography-palette.js +0 -5
  40. package/dist/cjs/set-global-theme.js +265 -39
  41. package/dist/cjs/theme-config.js +8 -23
  42. package/dist/cjs/{theme-change-observer.js → theme-mutation-observer.js} +5 -57
  43. package/dist/cjs/tokens/atlassian-dark/utility/utility.js +1 -4
  44. package/dist/cjs/tokens/atlassian-legacy-dark/utility/utility.js +1 -4
  45. package/dist/cjs/tokens/atlassian-legacy-light/utility/utility.js +1 -4
  46. package/dist/cjs/tokens/atlassian-light/utility/utility.js +1 -4
  47. package/dist/cjs/tokens/atlassian-spacing/spacing.js +0 -228
  48. package/dist/cjs/tokens/default/spacing/spacing.js +0 -621
  49. package/dist/cjs/tokens/default/utility/utility.js +1 -9
  50. package/dist/cjs/use-theme-observer.js +41 -0
  51. package/dist/cjs/utils/color-detection.js +23 -43
  52. package/dist/cjs/utils/theme-loading.js +69 -0
  53. package/dist/cjs/utils/theme-state-transformer.js +73 -0
  54. package/dist/cjs/utils/token-ids.js +4 -12
  55. package/dist/cjs/version.json +1 -1
  56. package/dist/es2019/artifacts/replacement-mapping.js +3 -610
  57. package/dist/es2019/artifacts/theme-import-map.js +27 -0
  58. package/dist/es2019/artifacts/themes/atlassian-dark.js +296 -0
  59. package/dist/es2019/artifacts/themes/atlassian-legacy-dark.js +296 -0
  60. package/dist/es2019/artifacts/themes/atlassian-legacy-light.js +296 -0
  61. package/dist/es2019/artifacts/themes/atlassian-light.js +296 -0
  62. package/dist/es2019/artifacts/themes/atlassian-spacing.js +23 -0
  63. package/{css/atlassian-typography.css → dist/es2019/artifacts/themes/atlassian-typography.js} +4 -2
  64. package/dist/es2019/artifacts/token-default-values.js +2 -169
  65. package/dist/es2019/artifacts/token-names.js +2 -169
  66. package/dist/es2019/artifacts/tokens-raw/atlassian-dark.js +1354 -4260
  67. package/dist/es2019/artifacts/tokens-raw/atlassian-legacy-dark.js +1349 -4255
  68. package/dist/es2019/artifacts/tokens-raw/atlassian-legacy-light.js +1397 -4283
  69. package/dist/es2019/artifacts/tokens-raw/atlassian-light.js +1360 -4246
  70. package/dist/es2019/artifacts/tokens-raw/atlassian-spacing.js +1 -1581
  71. package/dist/es2019/babel-plugin/plugin.js +17 -37
  72. package/dist/es2019/get-global-theme.js +20 -0
  73. package/dist/es2019/get-token-value.js +1 -6
  74. package/dist/es2019/get-token.js +3 -8
  75. package/dist/es2019/index.js +5 -2
  76. package/dist/es2019/palettes/legacy-palette.js +1 -0
  77. package/dist/es2019/palettes/spacing-scale.js +1 -0
  78. package/dist/es2019/palettes/typography-palette.js +2 -1
  79. package/dist/es2019/set-global-theme.js +162 -32
  80. package/dist/es2019/theme-config.js +5 -21
  81. package/dist/es2019/{theme-change-observer.js → theme-mutation-observer.js} +4 -37
  82. package/dist/es2019/tokens/atlassian-dark/utility/utility.js +1 -4
  83. package/dist/es2019/tokens/atlassian-legacy-dark/utility/utility.js +1 -4
  84. package/dist/es2019/tokens/atlassian-legacy-light/utility/utility.js +1 -4
  85. package/dist/es2019/tokens/atlassian-light/utility/utility.js +1 -4
  86. package/dist/es2019/tokens/atlassian-spacing/spacing.js +0 -227
  87. package/dist/es2019/tokens/default/spacing/spacing.js +0 -620
  88. package/dist/es2019/tokens/default/utility/utility.js +1 -9
  89. package/dist/es2019/use-theme-observer.js +26 -0
  90. package/dist/es2019/utils/color-detection.js +3 -5
  91. package/dist/es2019/utils/theme-loading.js +18 -0
  92. package/dist/es2019/utils/theme-state-transformer.js +47 -0
  93. package/dist/es2019/utils/token-ids.js +5 -4
  94. package/dist/es2019/version.json +1 -1
  95. package/dist/esm/artifacts/replacement-mapping.js +3 -610
  96. package/dist/esm/artifacts/theme-import-map.js +39 -0
  97. package/dist/esm/artifacts/themes/atlassian-dark.js +6 -0
  98. package/dist/esm/artifacts/themes/atlassian-legacy-dark.js +6 -0
  99. package/dist/esm/artifacts/themes/atlassian-legacy-light.js +6 -0
  100. package/dist/esm/artifacts/themes/atlassian-light.js +6 -0
  101. package/dist/esm/artifacts/themes/atlassian-spacing.js +6 -0
  102. package/dist/esm/artifacts/themes/atlassian-typography.js +6 -0
  103. package/dist/esm/artifacts/token-default-values.js +2 -169
  104. package/dist/esm/artifacts/token-names.js +2 -169
  105. package/dist/esm/artifacts/tokens-raw/atlassian-dark.js +1354 -4260
  106. package/dist/esm/artifacts/tokens-raw/atlassian-legacy-dark.js +1349 -4255
  107. package/dist/esm/artifacts/tokens-raw/atlassian-legacy-light.js +1397 -4283
  108. package/dist/esm/artifacts/tokens-raw/atlassian-light.js +1360 -4246
  109. package/dist/esm/artifacts/tokens-raw/atlassian-spacing.js +1 -1581
  110. package/dist/esm/babel-plugin/plugin.js +17 -35
  111. package/dist/esm/get-global-theme.js +22 -0
  112. package/dist/esm/get-token-value.js +1 -6
  113. package/dist/esm/get-token.js +3 -8
  114. package/dist/esm/index.js +5 -2
  115. package/dist/esm/palettes/legacy-palette.js +1 -0
  116. package/dist/esm/palettes/spacing-scale.js +1 -0
  117. package/dist/esm/palettes/typography-palette.js +0 -3
  118. package/dist/esm/set-global-theme.js +263 -37
  119. package/dist/esm/theme-config.js +5 -21
  120. package/dist/esm/{theme-change-observer.js → theme-mutation-observer.js} +4 -49
  121. package/dist/esm/tokens/atlassian-dark/utility/utility.js +1 -4
  122. package/dist/esm/tokens/atlassian-legacy-dark/utility/utility.js +1 -4
  123. package/dist/esm/tokens/atlassian-legacy-light/utility/utility.js +1 -4
  124. package/dist/esm/tokens/atlassian-light/utility/utility.js +1 -4
  125. package/dist/esm/tokens/atlassian-spacing/spacing.js +0 -227
  126. package/dist/esm/tokens/default/spacing/spacing.js +0 -620
  127. package/dist/esm/tokens/default/utility/utility.js +1 -9
  128. package/dist/esm/use-theme-observer.js +34 -0
  129. package/dist/esm/utils/color-detection.js +23 -30
  130. package/dist/esm/utils/theme-loading.js +60 -0
  131. package/dist/esm/utils/theme-state-transformer.js +64 -0
  132. package/dist/esm/utils/token-ids.js +5 -4
  133. package/dist/esm/version.json +1 -1
  134. package/dist/types/artifacts/replacement-mapping.d.ts +1 -1
  135. package/dist/types/artifacts/theme-import-map.d.ts +16 -0
  136. package/dist/types/artifacts/themes/atlassian-dark.d.ts +7 -0
  137. package/dist/types/artifacts/themes/atlassian-legacy-dark.d.ts +7 -0
  138. package/dist/types/artifacts/themes/atlassian-legacy-light.d.ts +7 -0
  139. package/dist/types/artifacts/themes/atlassian-light.d.ts +7 -0
  140. package/dist/types/artifacts/themes/atlassian-spacing.d.ts +7 -0
  141. package/dist/types/artifacts/themes/atlassian-typography.d.ts +7 -0
  142. package/dist/types/artifacts/token-default-values.d.ts +2 -169
  143. package/dist/types/artifacts/token-names.d.ts +3 -337
  144. package/dist/types/artifacts/tokens-raw/atlassian-dark.d.ts +1 -115
  145. package/dist/types/artifacts/tokens-raw/atlassian-legacy-dark.d.ts +1 -115
  146. package/dist/types/artifacts/tokens-raw/atlassian-legacy-light.d.ts +1 -91
  147. package/dist/types/artifacts/tokens-raw/atlassian-light.d.ts +1 -91
  148. package/dist/types/artifacts/tokens-raw/atlassian-spacing.d.ts +1 -89
  149. package/dist/types/artifacts/types-internal.d.ts +2 -2
  150. package/dist/types/artifacts/types.d.ts +2 -2
  151. package/dist/types/get-global-theme.d.ts +2 -0
  152. package/dist/types/index.d.ts +7 -3
  153. package/dist/types/set-global-theme.d.ts +63 -7
  154. package/dist/types/theme-config.d.ts +10 -7
  155. package/dist/types/{theme-change-observer.d.ts → theme-mutation-observer.d.ts} +2 -16
  156. package/dist/types/types.d.ts +1 -286
  157. package/dist/types/use-theme-observer.d.ts +15 -0
  158. package/dist/types/utils/theme-loading.d.ts +3 -0
  159. package/dist/types/utils/theme-state-transformer.d.ts +26 -0
  160. package/figma/atlassian-dark.json +1 -102
  161. package/figma/atlassian-legacy-dark.json +1 -102
  162. package/figma/atlassian-legacy-light.json +1 -102
  163. package/figma/atlassian-light.json +1 -102
  164. package/figma/atlassian-spacing.json +1 -761
  165. package/package.json +5 -8
  166. package/report.api.md +85 -351
  167. package/tmp/api-report-tmp.d.ts +58 -342
  168. package/css/atlassian-dark.css +0 -786
  169. package/css/atlassian-legacy-dark.css +0 -786
  170. package/css/atlassian-legacy-light.css +0 -786
  171. package/css/atlassian-light.css +0 -786
  172. package/css/atlassian-spacing.css +0 -87
  173. package/dist/cjs/artifacts/typescript/atlassian-light-token-default-values.js +0 -407
  174. package/dist/cjs/tokens/atlassian-dark/deprecated/deprecated.js +0 -435
  175. package/dist/cjs/tokens/atlassian-legacy-dark/deprecated/deprecated.js +0 -532
  176. package/dist/cjs/tokens/atlassian-legacy-light/deprecated/deprecated.js +0 -523
  177. package/dist/cjs/tokens/atlassian-light/deprecated/deprecated.js +0 -421
  178. package/dist/cjs/tokens/default/deprecated/deprecated.js +0 -1186
  179. package/dist/es2019/artifacts/typescript/atlassian-light-token-default-values.js +0 -399
  180. package/dist/es2019/tokens/atlassian-dark/deprecated/deprecated.js +0 -428
  181. package/dist/es2019/tokens/atlassian-legacy-dark/deprecated/deprecated.js +0 -525
  182. package/dist/es2019/tokens/atlassian-legacy-light/deprecated/deprecated.js +0 -516
  183. package/dist/es2019/tokens/atlassian-light/deprecated/deprecated.js +0 -414
  184. package/dist/es2019/tokens/default/deprecated/deprecated.js +0 -1217
  185. package/dist/esm/artifacts/typescript/atlassian-light-token-default-values.js +0 -399
  186. package/dist/esm/tokens/atlassian-dark/deprecated/deprecated.js +0 -428
  187. package/dist/esm/tokens/atlassian-legacy-dark/deprecated/deprecated.js +0 -525
  188. package/dist/esm/tokens/atlassian-legacy-light/deprecated/deprecated.js +0 -516
  189. package/dist/esm/tokens/atlassian-light/deprecated/deprecated.js +0 -414
  190. package/dist/esm/tokens/default/deprecated/deprecated.js +0 -1179
  191. package/dist/types/artifacts/typescript/atlassian-light-token-default-values.d.ts +0 -399
  192. package/dist/types/tokens/atlassian-dark/deprecated/deprecated.d.ts +0 -4
  193. package/dist/types/tokens/atlassian-legacy-dark/deprecated/deprecated.d.ts +0 -4
  194. package/dist/types/tokens/atlassian-legacy-light/deprecated/deprecated.d.ts +0 -4
  195. package/dist/types/tokens/atlassian-light/deprecated/deprecated.d.ts +0 -4
  196. package/dist/types/tokens/default/deprecated/deprecated.d.ts +0 -4
@@ -11,49 +11,44 @@ export default function plugin() {
11
11
  function CallExpression(_x) {
12
12
  return _CallExpression.apply(this, arguments);
13
13
  }
14
-
15
14
  CallExpression.toString = function () {
16
15
  return _CallExpression.toString();
17
16
  };
18
-
19
17
  return CallExpression;
20
18
  }(function (path) {
21
19
  var tokenImportScope = getTokenImportScope(path);
22
-
23
20
  if (!tokenImportScope) {
24
21
  return;
25
- } // Check arguments have correct format
26
-
22
+ }
27
23
 
24
+ // Check arguments have correct format
28
25
  if (!path.node.arguments[0]) {
29
26
  throw new Error("token() requires at least one argument");
30
27
  } else if (!t.isStringLiteral(path.node.arguments[0])) {
31
28
  throw new Error("token() must have a string as the first argument");
32
29
  } else if (path.node.arguments.length > 2) {
33
30
  throw new Error("token() does not accept ".concat(path.node.arguments.length, " arguments"));
34
- } // Check the token exists
35
-
31
+ }
36
32
 
33
+ // Check the token exists
37
34
  var tokenName = path.node.arguments[0].value;
38
35
  var cssTokenValue = tokenNames[tokenName];
39
-
40
36
  if (!cssTokenValue) {
41
37
  throw new Error("token '".concat(tokenName, "' does not exist"));
42
38
  }
39
+ var replacementNode;
43
40
 
44
- var replacementNode; // if no fallback is set, optionally find one from the default theme
45
-
41
+ // if no fallback is set, optionally find one from the default theme
46
42
  if (path.node.arguments.length < 2) {
47
43
  if (state.opts.shouldUseAutoFallback) {
48
44
  replacementNode = t.stringLiteral("var(".concat(cssTokenValue, ", ").concat(getDefaultFallback(tokenName), ")"));
49
45
  } else {
50
46
  replacementNode = t.stringLiteral("var(".concat(cssTokenValue, ")"));
51
47
  }
52
- } // Handle fallbacks
53
-
48
+ }
54
49
 
50
+ // Handle fallbacks
55
51
  var fallback = path.node.arguments[1];
56
-
57
52
  if (t.isStringLiteral(fallback)) {
58
53
  // String literals can be concatenated into css variable call
59
54
  // Empty string fallbacks are ignored. For now, as the user did specify a fallback, no default is inserted
@@ -68,11 +63,11 @@ export default function plugin() {
68
63
  raw: ')',
69
64
  cooked: ')'
70
65
  }, true)], [fallback]);
71
- } // Replace path and call scope.crawl() to refresh the scope bindings + references
72
-
73
-
74
- replacementNode && path.replaceWith(replacementNode); // @ts-ignore crawl is a valid property
66
+ }
75
67
 
68
+ // Replace path and call scope.crawl() to refresh the scope bindings + references
69
+ replacementNode && path.replaceWith(replacementNode);
70
+ // @ts-ignore crawl is a valid property
76
71
  tokenImportScope.crawl();
77
72
  })
78
73
  });
@@ -84,23 +79,22 @@ export default function plugin() {
84
79
  if (path.node.source.value !== '@atlaskit/tokens') {
85
80
  return;
86
81
  }
87
-
88
82
  path.get('specifiers').forEach(function (specifier) {
89
83
  if (!specifier.isImportSpecifier()) {
90
84
  return;
91
85
  }
92
-
93
86
  if (getNonAliasedImportName(specifier.node) !== 'token') {
94
87
  return;
95
88
  }
89
+ var binding = path.scope.bindings[getAliasedImportName(specifier.node)];
96
90
 
97
- var binding = path.scope.bindings[getAliasedImportName(specifier.node)]; // if no longer used, remove
98
-
91
+ // if no longer used, remove
99
92
  if (!binding.referenced) {
100
93
  specifier.remove();
101
94
  }
102
- }); // remove '@atlaskit/tokens' import if it is no longer needed
95
+ });
103
96
 
97
+ // remove '@atlaskit/tokens' import if it is no longer needed
104
98
  if (path.get('specifiers').length === 0) {
105
99
  path.remove();
106
100
  }
@@ -111,55 +105,43 @@ export default function plugin() {
111
105
  }
112
106
  };
113
107
  }
114
-
115
108
  function getDefaultFallback(tokenName) {
116
109
  return tokenDefaultValues[tokenName];
117
110
  }
118
-
119
111
  function getNonAliasedImportName(node) {
120
112
  if (t.isIdentifier(node.imported)) {
121
113
  return node.imported.name;
122
114
  }
123
-
124
115
  return node.imported.value;
125
116
  }
126
-
127
117
  function getAliasedImportName(node) {
128
118
  return node.local.name;
129
119
  }
120
+
130
121
  /**
131
122
  * Determine if the current call is to a token function, and
132
123
  * return the relevant scope
133
124
  */
134
-
135
-
136
125
  function getTokenImportScope(path) {
137
126
  var callee = path.node.callee;
138
-
139
127
  if (!t.isIdentifier(callee)) {
140
128
  return undefined;
141
129
  }
142
-
143
130
  var binding = getTokenBinding(path.scope, callee.name);
144
-
145
131
  if (!binding || !t.isImportSpecifier(binding.path.node)) {
146
132
  return undefined;
147
133
  }
148
-
149
134
  if (binding.path.parent && t.isImportDeclaration(binding.path.parent)) {
150
135
  if (binding.path.parent.source.value !== '@atlaskit/tokens') {
151
136
  return undefined;
152
137
  }
153
138
  }
154
-
155
139
  return getNonAliasedImportName(binding.path.node) === 'token' ? binding.scope : undefined;
156
140
  }
157
-
158
141
  function getTokenBinding(scope, name) {
159
142
  if (!scope) {
160
143
  return undefined;
161
144
  }
162
-
163
145
  if (scope.bindings[name]) {
164
146
  return scope.bindings[name];
165
147
  } else {
@@ -0,0 +1,22 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+ import { COLOR_MODE_ATTRIBUTE, THEME_DATA_ATTRIBUTE } from './constants';
5
+ import { themeColorModes } from './theme-config';
6
+ import { themeStringToObject } from './utils/theme-state-transformer';
7
+ var isThemeColorMode = function isThemeColorMode(colorMode) {
8
+ return themeColorModes.find(function (mode) {
9
+ return mode === colorMode;
10
+ }) !== undefined;
11
+ };
12
+ export var getGlobalTheme = function getGlobalTheme() {
13
+ if (typeof document === 'undefined') {
14
+ return {};
15
+ }
16
+ var element = document.documentElement;
17
+ var colorMode = element.getAttribute(COLOR_MODE_ATTRIBUTE) || '';
18
+ var theme = element.getAttribute(THEME_DATA_ATTRIBUTE) || '';
19
+ return _objectSpread(_objectSpread({}, themeStringToObject(theme)), isThemeColorMode(colorMode) && {
20
+ colorMode: colorMode
21
+ });
22
+ };
@@ -1,8 +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 = "0.13.5";
5
-
4
+ var version = "1.1.0";
6
5
  /**
7
6
  * Takes a dot-separated token name and and an optional fallback, and returns the current computed CSS value for the
8
7
  * resulting CSS Custom Property.
@@ -28,18 +27,14 @@ function getTokenValue(tokenId) {
28
27
  var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
29
28
  var token = tokens[tokenId];
30
29
  var tokenValue = fallback;
31
-
32
30
  if (process.env.NODE_ENV !== 'production' && !token) {
33
31
  warnOnce("Unknown token id at path: ".concat(tokenId, " for ").concat(name, "@").concat(version));
34
32
  }
35
-
36
33
  if (typeof window === 'undefined') {
37
34
  return tokenValue;
38
35
  }
39
-
40
36
  tokenValue = window.getComputedStyle(document.documentElement).getPropertyValue(token);
41
37
  tokenValue = tokenValue || fallback;
42
38
  return tokenValue;
43
39
  }
44
-
45
40
  export default getTokenValue;
@@ -2,8 +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 = "0.13.5";
6
-
5
+ var version = "1.1.0";
7
6
  /**
8
7
  * Takes a dot-separated token name and an optional fallback, and returns the CSS custom property for the corresponding token.
9
8
  * This should be used to implement design decisions throughout your application.
@@ -28,19 +27,15 @@ var version = "0.13.5";
28
27
  */
29
28
  function token(path, fallback) {
30
29
  var token = tokens[path];
31
-
32
30
  if (process.env.NODE_ENV !== 'production' && !token) {
33
- token = tokens['utility.UNSAFE_util.MISSING_TOKEN'];
34
31
  warnOnce("Unknown token id at path: ".concat(path, " for ").concat(name, "@").concat(version));
35
- } // if the token is not found - replacing it with variable name without any value, to avoid it being undefined which would result in invalid css
36
-
32
+ }
37
33
 
34
+ // if the token is not found - replacing it with variable name without any value, to avoid it being undefined which would result in invalid css
38
35
  if (!token) {
39
36
  token = TOKEN_NOT_FOUND_CSS_VAR;
40
37
  }
41
-
42
38
  var tokenCall = fallback ? "var(".concat(token, ", ").concat(fallback, ")") : "var(".concat(token, ")");
43
39
  return tokenCall;
44
40
  }
45
-
46
41
  export default token;
package/dist/esm/index.js CHANGED
@@ -1,5 +1,8 @@
1
1
  export { default as token } from './get-token';
2
2
  export { default as getTokenValue } from './get-token-value';
3
- export { default as setGlobalTheme } from './set-global-theme';
3
+ export { default as setGlobalTheme, getThemeStyles, getThemeHtmlAttrs, getSSRAutoScript } from './set-global-theme';
4
4
  export { default as themeConfig } from './theme-config';
5
- export { useThemeObserver, ThemeMutationObserver } from './theme-change-observer';
5
+ export { useThemeObserver } from './use-theme-observer';
6
+ export { ThemeMutationObserver } from './theme-mutation-observer';
7
+ export { getGlobalTheme } from './get-global-theme';
8
+ export { themeStringToObject, themeObjectToString } from './utils/theme-state-transformer';
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * This palette should be exclusively used for backwards compatible themes
5
5
  */
6
+
6
7
  var palette = {
7
8
  value: {
8
9
  opacity: {
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Types are inferred from the base tokens below
3
3
  */
4
+
4
5
  var baseSpacingTokens = {
5
6
  Space0: {
6
7
  value: '0',
@@ -1,9 +1,6 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
-
3
2
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
-
5
3
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
-
7
4
  var lineHeightScale = {
8
5
  lineHeight: {
9
6
  LineHeight100: {
@@ -1,49 +1,275 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
4
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
5
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
6
+ import { bind } from 'bind-event-listener';
7
+ import noop from '@atlaskit/ds-lib/noop';
1
8
  import { COLOR_MODE_ATTRIBUTE, THEME_DATA_ATTRIBUTE } from './constants';
2
- import themeConfig from './theme-config';
9
+ import { loadAndAppendThemeCss, loadThemeCss } from './utils/theme-loading';
10
+ import { themeObjectToString } from './utils/theme-state-transformer';
11
+ var defaultColorMode = 'light';
12
+ var isMatchMediaAvailable = typeof window !== 'undefined' && 'matchMedia' in window;
13
+ var darkModeMediaQuery = '(prefers-color-scheme: dark)';
14
+ var darkModeMql = isMatchMediaAvailable && window.matchMedia(darkModeMediaQuery);
15
+ var unbindThemeChangeListener = noop;
16
+ var themeStateDefaults = {
17
+ colorMode: 'auto',
18
+ dark: 'dark',
19
+ light: 'light',
20
+ spacing: undefined,
21
+ typography: undefined
22
+ };
23
+
24
+ /**
25
+ * Updates the current theme when the system theme changes. Should be bound
26
+ * to an event listener listening on the '(prefers-color-scheme: dark)' query
27
+ * @param e The event representing a change in system theme.
28
+ */
29
+ var checkNativeListener = function checkNativeListener(e) {
30
+ var element = document.documentElement;
31
+ element.setAttribute(COLOR_MODE_ATTRIBUTE, e.matches ? 'dark' : 'light');
32
+ };
33
+
3
34
  /**
4
- * Sets the theme globally at runtime. This updates the `data-theme` and `data-color-scheme` attributes on your page's <html> tag.
35
+ * Sets the theme globally at runtime. This updates the `data-theme` and `data-color-mode` attributes on your page's <html> tag.
5
36
  *
6
- * @param {string} themeId - Which theme should be used by default.
7
- * @param {string} [shouldMatchSystem=false] - Whether the theme should automatically switch between themes to match the system preference.
37
+ * @param {Object<string, string>} themeState The themes and color mode that should be applied.
38
+ * @param {string} themeState.colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
39
+ * @param {string} themeState.dark The color theme to be applied when the color mode resolves to 'dark'.
40
+ * @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'.
41
+ * @param {string} themeState.spacing The spacing theme to be applied.
42
+ * @param {string} themeState.typography The typography theme to be applied.
43
+ *
44
+ * @returns A Promise of an unbind function, that can be used to stop listening for changes to system theme.
8
45
  *
9
46
  * @example
10
47
  * ```
11
- * // Set light theme as the default theme, but switch to others based on the system color theme.
12
- * setGlobalTheme('light', true);
48
+ * setGlobalTheme({colorMode: 'auto', light: 'light', dark: 'dark', spacing: 'spacing'});
13
49
  * ```
14
50
  */
51
+ var setGlobalTheme = /*#__PURE__*/function () {
52
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
53
+ var _ref2,
54
+ _ref2$colorMode,
55
+ colorMode,
56
+ _ref2$dark,
57
+ dark,
58
+ _ref2$light,
59
+ light,
60
+ _ref2$spacing,
61
+ spacing,
62
+ _ref2$typography,
63
+ typography,
64
+ themePreferences,
65
+ themeAttributes,
66
+ _args2 = arguments;
67
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
68
+ while (1) {
69
+ switch (_context2.prev = _context2.next) {
70
+ case 0:
71
+ _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$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;
72
+ // Dedupe list of themes to avoid race condition
73
+ themePreferences = new Set([dark, light, spacing, typography]);
74
+ _context2.next = 4;
75
+ return Promise.all(_toConsumableArray(themePreferences).filter(function (themeId) {
76
+ return themeId !== undefined;
77
+ }).map( /*#__PURE__*/function () {
78
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(themeId) {
79
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
80
+ while (1) {
81
+ switch (_context.prev = _context.next) {
82
+ case 0:
83
+ _context.next = 2;
84
+ return loadAndAppendThemeCss(themeId);
85
+ case 2:
86
+ return _context.abrupt("return", _context.sent);
87
+ case 3:
88
+ case "end":
89
+ return _context.stop();
90
+ }
91
+ }
92
+ }, _callee);
93
+ }));
94
+ return function (_x) {
95
+ return _ref3.apply(this, arguments);
96
+ };
97
+ }()));
98
+ case 4:
99
+ if (colorMode === 'auto' && darkModeMql) {
100
+ colorMode = darkModeMql.matches ? 'dark' : 'light';
101
+ // Add an event listener for changes to the system theme.
102
+ // If the function exists, it will not be added again.
103
+ unbindThemeChangeListener = bind(darkModeMql, {
104
+ type: 'change',
105
+ listener: checkNativeListener
106
+ });
107
+ } else {
108
+ unbindThemeChangeListener();
109
+ }
110
+ themeAttributes = getThemeHtmlAttrs({
111
+ colorMode: colorMode,
112
+ dark: dark,
113
+ light: light,
114
+ spacing: spacing,
115
+ typography: typography
116
+ });
117
+ Object.entries(themeAttributes).forEach(function (_ref4) {
118
+ var _ref5 = _slicedToArray(_ref4, 2),
119
+ key = _ref5[0],
120
+ value = _ref5[1];
121
+ document.documentElement.setAttribute(key, value);
122
+ });
123
+ return _context2.abrupt("return", unbindThemeChangeListener);
124
+ case 8:
125
+ case "end":
126
+ return _context2.stop();
127
+ }
128
+ }
129
+ }, _callee2);
130
+ }));
131
+ return function setGlobalTheme() {
132
+ return _ref.apply(this, arguments);
133
+ };
134
+ }();
135
+ /**
136
+ * Takes an object containing theme preferences, and returns an array of objects for use in applying styles to the document head.
137
+ * Only supplies the color themes necessary for initial render, based on the current themeState. I.e. if in light mode, dark mode themes are not returned.
138
+ *
139
+ * @param {Object<string, string>} themeState The themes and color mode that should be applied.
140
+ * @param {string} themeState.colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
141
+ * @param {string} themeState.dark The color theme to be applied when the color mode resolves to 'dark'.
142
+ * @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'.
143
+ * @param {string} themeState.spacing The spacing theme to be applied.
144
+ * @param {string} themeState.typography The typography theme to be applied.
145
+ *
146
+ * @returns A Promise of an object array, containing theme IDs, data-attributes to attach to the theme, and the theme CSS.
147
+ * If an error is encountered while loading themes, the themes arrav will be emptv.
148
+ */
149
+ export var getThemeStyles = /*#__PURE__*/function () {
150
+ var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
151
+ var _ref7,
152
+ _ref7$colorMode,
153
+ colorMode,
154
+ _ref7$dark,
155
+ dark,
156
+ _ref7$light,
157
+ light,
158
+ _ref7$spacing,
159
+ spacing,
160
+ _ref7$typography,
161
+ typography,
162
+ themePreferences,
163
+ results,
164
+ _args4 = arguments;
165
+ return _regeneratorRuntime.wrap(function _callee4$(_context4) {
166
+ while (1) {
167
+ switch (_context4.prev = _context4.next) {
168
+ case 0:
169
+ _ref7 = _args4.length > 0 && _args4[0] !== undefined ? _args4[0] : {}, _ref7$colorMode = _ref7.colorMode, colorMode = _ref7$colorMode === void 0 ? themeStateDefaults['colorMode'] : _ref7$colorMode, _ref7$dark = _ref7.dark, dark = _ref7$dark === void 0 ? themeStateDefaults['dark'] : _ref7$dark, _ref7$light = _ref7.light, light = _ref7$light === void 0 ? themeStateDefaults['light'] : _ref7$light, _ref7$spacing = _ref7.spacing, spacing = _ref7$spacing === void 0 ? themeStateDefaults['spacing'] : _ref7$spacing, _ref7$typography = _ref7.typography, typography = _ref7$typography === void 0 ? themeStateDefaults['typography'] : _ref7$typography;
170
+ themePreferences = colorMode === 'auto' ? [light, dark] : [colorMode];
171
+ [spacing, typography].forEach(function (themeId) {
172
+ if (themeId) {
173
+ themePreferences.push(themeId);
174
+ }
175
+ });
176
+ _context4.next = 5;
177
+ return Promise.all(themePreferences.map( /*#__PURE__*/function () {
178
+ var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(themeId) {
179
+ var css;
180
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
181
+ while (1) {
182
+ switch (_context3.prev = _context3.next) {
183
+ case 0:
184
+ _context3.prev = 0;
185
+ _context3.next = 3;
186
+ return loadThemeCss(themeId);
187
+ case 3:
188
+ css = _context3.sent;
189
+ return _context3.abrupt("return", {
190
+ id: themeId,
191
+ attrs: {
192
+ 'data-theme': themeId
193
+ },
194
+ css: css
195
+ });
196
+ case 7:
197
+ _context3.prev = 7;
198
+ _context3.t0 = _context3["catch"](0);
199
+ return _context3.abrupt("return", undefined);
200
+ case 10:
201
+ case "end":
202
+ return _context3.stop();
203
+ }
204
+ }
205
+ }, _callee3, null, [[0, 7]]);
206
+ }));
207
+ return function (_x2) {
208
+ return _ref8.apply(this, arguments);
209
+ };
210
+ }()));
211
+ case 5:
212
+ results = _context4.sent;
213
+ return _context4.abrupt("return", results.filter(function (theme) {
214
+ return theme !== undefined;
215
+ }));
216
+ case 7:
217
+ case "end":
218
+ return _context4.stop();
219
+ }
220
+ }
221
+ }, _callee4);
222
+ }));
223
+ return function getThemeStyles() {
224
+ return _ref6.apply(this, arguments);
225
+ };
226
+ }();
15
227
 
16
- var setGlobalTheme = function setGlobalTheme(themeId) {
17
- var shouldMatchSystem = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
18
- var theme = Object.values(themeConfig).find(function (_ref) {
19
- var id = _ref.id;
20
- return id === themeId;
21
- });
22
-
23
- if (process.env.NODE_ENV !== 'production') {
24
- if (!theme) {
25
- var themeIds = Object.values(themeConfig).map(function (_ref2) {
26
- var id = _ref2.id;
27
- return id;
28
- });
29
- throw new Error("setGlobalTheme only accepts themes: ".concat(themeIds.join(', ')));
30
- }
31
- }
32
-
33
- if (!theme) {
34
- return;
35
- }
36
-
37
- var element = document.documentElement;
38
- element.setAttribute(THEME_DATA_ATTRIBUTE, theme.id);
39
-
40
- if (theme.attributes.type === 'color') {
41
- element.setAttribute(COLOR_MODE_ATTRIBUTE, theme.attributes.mode);
42
- }
43
-
44
- if (shouldMatchSystem) {
45
- element.setAttribute(COLOR_MODE_ATTRIBUTE, 'auto');
46
- }
228
+ /**
229
+ * Server-side rendering utility. Generates the valid HTML attributes for a given theme.
230
+ * Note: this utility does not handle automatic theme switching.
231
+ *
232
+ * @param {Object<string, string>} themeOptions - Theme options object
233
+ * @param {string} themeState.colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
234
+ * @param {string} themeState.dark The color theme to be applied when the color mode resolves to 'dark'.
235
+ * @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'.
236
+ * @param {string} themeState.spacing The spacing theme to be applied.
237
+ * @param {string} themeState.typography The typography theme to be applied.
238
+ *
239
+ * @returns {Object} Object of HTML attributes to be applied to the document root
240
+ */
241
+ export var getThemeHtmlAttrs = function getThemeHtmlAttrs() {
242
+ var _ref10;
243
+ var _ref9 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
244
+ _ref9$colorMode = _ref9.colorMode,
245
+ colorMode = _ref9$colorMode === void 0 ? themeStateDefaults['colorMode'] : _ref9$colorMode,
246
+ _ref9$dark = _ref9.dark,
247
+ dark = _ref9$dark === void 0 ? themeStateDefaults['dark'] : _ref9$dark,
248
+ _ref9$light = _ref9.light,
249
+ light = _ref9$light === void 0 ? themeStateDefaults['light'] : _ref9$light,
250
+ _ref9$spacing = _ref9.spacing,
251
+ spacing = _ref9$spacing === void 0 ? themeStateDefaults['spacing'] : _ref9$spacing,
252
+ _ref9$typography = _ref9.typography,
253
+ typography = _ref9$typography === void 0 ? themeStateDefaults['typography'] : _ref9$typography;
254
+ var themePreferences = {
255
+ dark: dark,
256
+ light: light,
257
+ spacing: spacing,
258
+ typography: typography
259
+ };
260
+ var themeAttribute = themeObjectToString(themePreferences);
261
+ return _ref10 = {}, _defineProperty(_ref10, THEME_DATA_ATTRIBUTE, themeAttribute), _defineProperty(_ref10, COLOR_MODE_ATTRIBUTE, colorMode === 'auto' ? defaultColorMode : colorMode), _ref10;
47
262
  };
48
263
 
264
+ /**
265
+ * Provides a script that, when executed before paint, sets the `data-color-mode` attribute based on the current system theme,
266
+ * to enable SSR support for automatic theme switching, avoid a flash of un-themed content on first paint.
267
+ *
268
+ * @param {string} colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
269
+ *
270
+ * @returns {string} A string to be added to the innerHTML of a script tag in the document head
271
+ */
272
+ export var getSSRAutoScript = function getSSRAutoScript(colorMode) {
273
+ return colorMode === 'auto' ? "(\n () => {\n try {\n const mql = window.matchMedia('".concat(darkModeMediaQuery, "');\n const colorMode = mql.matches ? 'dark' : 'light';\n document.documentElement.setAttribute('").concat(COLOR_MODE_ATTRIBUTE, "', colorMode);\n } catch (e) {}\n }\n)()") : undefined;
274
+ };
49
275
  export default setGlobalTheme;
@@ -16,34 +16,18 @@
16
16
 
17
17
  /**
18
18
  * Theme modes: The general purpose of a theme.
19
- * This attr is used to apply the appropriate system-preference media selector
19
+ * This attr is used to apply the appropriate system-preference option
20
20
  * It may also be used as a selector for mode-specific overrides such as light/dark images.
21
21
  * The idea is there may exist many color themes, but every theme must either fit into light or dark.
22
22
  */
23
-
23
+ export var themeColorModes = ['light', 'dark', 'auto'];
24
24
  /**
25
- * Theme ids: The value that will mounted to the DOM as a data attr
26
- * For example: `data-theme="light"
25
+ * Theme ids: The value that will be mounted to the DOM as a data attr
26
+ * For example: `data-theme="light:light dark:dark spacing:spacing"
27
27
  *
28
28
  * These ids must be kebab case
29
29
  */
30
-
31
- /**
32
- * Theme to use a base. This will create the theme as
33
- * an extension with all token values marked as optional
34
- * to allow tokens to be overridden as required.
35
- */
36
-
37
- /**
38
- * Palettes: The set of base tokens a given theme may be populated with.
39
- * For example: legacy light & dark themes use the "legacyPalette" containing colors from our
40
- * previous color set.
41
- */
42
-
43
- /**
44
- * ThemeConfig: the source of truth for all theme meta-data.
45
- * This object should be used whenever interfacing with themes.
46
- */
30
+ export var themeIds = ['light', 'dark', 'legacy-light', 'legacy-dark', 'spacing', 'typography'];
47
31
  var themeConfig = {
48
32
  'atlassian-light': {
49
33
  id: 'light',