@atlaskit/tokens 0.13.5 → 1.0.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.
- package/CHANGELOG.md +289 -0
- package/README.md +7 -16
- package/dist/cjs/artifacts/palettes-raw/legacy-palette.js +0 -1
- package/dist/cjs/artifacts/palettes-raw/palette.js +0 -1
- package/dist/cjs/artifacts/palettes-raw/spacing-scale.js +0 -1
- package/dist/cjs/artifacts/palettes-raw/typography-palette.js +0 -1
- package/dist/cjs/artifacts/replacement-mapping.js +2 -402
- package/dist/cjs/artifacts/theme-import-map.js +56 -0
- package/dist/cjs/artifacts/themes/atlassian-dark.js +13 -0
- package/dist/cjs/artifacts/themes/atlassian-legacy-dark.js +13 -0
- package/dist/cjs/artifacts/themes/atlassian-legacy-light.js +13 -0
- package/dist/cjs/artifacts/themes/atlassian-light.js +13 -0
- package/dist/cjs/artifacts/themes/atlassian-spacing.js +13 -0
- package/dist/cjs/artifacts/themes/atlassian-typography.js +13 -0
- package/dist/cjs/artifacts/token-default-values.js +2 -104
- package/dist/cjs/artifacts/token-names.js +2 -104
- package/dist/cjs/artifacts/tokens-raw/atlassian-dark.js +1354 -4261
- package/dist/cjs/artifacts/tokens-raw/atlassian-legacy-dark.js +1352 -4259
- package/dist/cjs/artifacts/tokens-raw/atlassian-legacy-light.js +1396 -4283
- package/dist/cjs/artifacts/tokens-raw/atlassian-light.js +1365 -4252
- package/dist/cjs/artifacts/tokens-raw/atlassian-spacing.js +0 -1
- package/dist/cjs/artifacts/tokens-raw/atlassian-typography.js +0 -1
- package/dist/cjs/artifacts/typescript/atlassian-dark-token-names.js +0 -1
- package/dist/cjs/artifacts/typescript/atlassian-light-token-names.js +0 -1
- package/dist/cjs/babel-plugin/index.js +0 -2
- package/dist/cjs/babel-plugin/plugin.js +17 -43
- package/dist/cjs/entry-points/babel-plugin.js +0 -2
- package/dist/cjs/entry-points/palettes-raw.js +0 -2
- package/dist/cjs/entry-points/rename-mapping.js +0 -2
- package/dist/cjs/entry-points/token-ids.js +0 -1
- package/dist/cjs/entry-points/token-names.js +0 -2
- package/dist/cjs/entry-points/tokens-raw.js +0 -5
- package/dist/cjs/get-global-theme.js +30 -0
- package/dist/cjs/get-token-value.js +1 -10
- package/dist/cjs/get-token.js +3 -13
- package/dist/cjs/index.js +46 -10
- package/dist/cjs/palettes/legacy-palette.js +1 -1
- package/dist/cjs/palettes/spacing-scale.js +1 -1
- package/dist/cjs/palettes/typography-palette.js +0 -5
- package/dist/cjs/set-global-theme.js +265 -39
- package/dist/cjs/theme-config.js +8 -23
- package/dist/cjs/{theme-change-observer.js → theme-mutation-observer.js} +5 -57
- package/dist/cjs/tokens/atlassian-dark/utility/utility.js +1 -4
- package/dist/cjs/tokens/atlassian-legacy-dark/utility/utility.js +1 -4
- package/dist/cjs/tokens/atlassian-legacy-light/utility/utility.js +1 -4
- package/dist/cjs/tokens/atlassian-light/utility/utility.js +1 -4
- package/dist/cjs/tokens/atlassian-spacing/spacing.js +0 -1
- package/dist/cjs/tokens/default/spacing/spacing.js +0 -1
- package/dist/cjs/tokens/default/utility/utility.js +1 -9
- package/dist/cjs/use-theme-observer.js +41 -0
- package/dist/cjs/utils/color-detection.js +23 -43
- package/dist/cjs/utils/theme-loading.js +69 -0
- package/dist/cjs/utils/theme-state-transformer.js +73 -0
- package/dist/cjs/utils/token-ids.js +4 -12
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/artifacts/replacement-mapping.js +2 -401
- package/dist/es2019/artifacts/theme-import-map.js +27 -0
- package/dist/es2019/artifacts/themes/atlassian-dark.js +296 -0
- package/dist/es2019/artifacts/themes/atlassian-legacy-dark.js +296 -0
- package/dist/es2019/artifacts/themes/atlassian-legacy-light.js +296 -0
- package/dist/es2019/artifacts/themes/atlassian-light.js +296 -0
- package/{css/atlassian-spacing.css → dist/es2019/artifacts/themes/atlassian-spacing.js} +4 -2
- package/{css/atlassian-typography.css → dist/es2019/artifacts/themes/atlassian-typography.js} +4 -2
- package/dist/es2019/artifacts/token-default-values.js +2 -103
- package/dist/es2019/artifacts/token-names.js +2 -103
- package/dist/es2019/artifacts/tokens-raw/atlassian-dark.js +1354 -4260
- package/dist/es2019/artifacts/tokens-raw/atlassian-legacy-dark.js +1349 -4255
- package/dist/es2019/artifacts/tokens-raw/atlassian-legacy-light.js +1397 -4283
- package/dist/es2019/artifacts/tokens-raw/atlassian-light.js +1360 -4246
- package/dist/es2019/babel-plugin/plugin.js +17 -37
- package/dist/es2019/get-global-theme.js +20 -0
- package/dist/es2019/get-token-value.js +1 -6
- package/dist/es2019/get-token.js +3 -8
- package/dist/es2019/index.js +5 -2
- package/dist/es2019/palettes/legacy-palette.js +1 -0
- package/dist/es2019/palettes/spacing-scale.js +1 -0
- package/dist/es2019/palettes/typography-palette.js +2 -1
- package/dist/es2019/set-global-theme.js +162 -32
- package/dist/es2019/theme-config.js +5 -21
- package/dist/es2019/{theme-change-observer.js → theme-mutation-observer.js} +4 -37
- package/dist/es2019/tokens/atlassian-dark/utility/utility.js +1 -4
- package/dist/es2019/tokens/atlassian-legacy-dark/utility/utility.js +1 -4
- package/dist/es2019/tokens/atlassian-legacy-light/utility/utility.js +1 -4
- package/dist/es2019/tokens/atlassian-light/utility/utility.js +1 -4
- package/dist/es2019/tokens/default/utility/utility.js +1 -9
- package/dist/es2019/use-theme-observer.js +26 -0
- package/dist/es2019/utils/color-detection.js +3 -5
- package/dist/es2019/utils/theme-loading.js +18 -0
- package/dist/es2019/utils/theme-state-transformer.js +47 -0
- package/dist/es2019/utils/token-ids.js +5 -4
- package/dist/es2019/version.json +1 -1
- package/dist/esm/artifacts/replacement-mapping.js +2 -401
- package/dist/esm/artifacts/theme-import-map.js +39 -0
- package/dist/esm/artifacts/themes/atlassian-dark.js +6 -0
- package/dist/esm/artifacts/themes/atlassian-legacy-dark.js +6 -0
- package/dist/esm/artifacts/themes/atlassian-legacy-light.js +6 -0
- package/dist/esm/artifacts/themes/atlassian-light.js +6 -0
- package/dist/esm/artifacts/themes/atlassian-spacing.js +6 -0
- package/dist/esm/artifacts/themes/atlassian-typography.js +6 -0
- package/dist/esm/artifacts/token-default-values.js +2 -103
- package/dist/esm/artifacts/token-names.js +2 -103
- package/dist/esm/artifacts/tokens-raw/atlassian-dark.js +1354 -4260
- package/dist/esm/artifacts/tokens-raw/atlassian-legacy-dark.js +1349 -4255
- package/dist/esm/artifacts/tokens-raw/atlassian-legacy-light.js +1397 -4283
- package/dist/esm/artifacts/tokens-raw/atlassian-light.js +1360 -4246
- package/dist/esm/babel-plugin/plugin.js +17 -35
- package/dist/esm/get-global-theme.js +22 -0
- package/dist/esm/get-token-value.js +1 -6
- package/dist/esm/get-token.js +3 -8
- package/dist/esm/index.js +5 -2
- package/dist/esm/palettes/legacy-palette.js +1 -0
- package/dist/esm/palettes/spacing-scale.js +1 -0
- package/dist/esm/palettes/typography-palette.js +0 -3
- package/dist/esm/set-global-theme.js +263 -37
- package/dist/esm/theme-config.js +5 -21
- package/dist/esm/{theme-change-observer.js → theme-mutation-observer.js} +4 -49
- package/dist/esm/tokens/atlassian-dark/utility/utility.js +1 -4
- package/dist/esm/tokens/atlassian-legacy-dark/utility/utility.js +1 -4
- package/dist/esm/tokens/atlassian-legacy-light/utility/utility.js +1 -4
- package/dist/esm/tokens/atlassian-light/utility/utility.js +1 -4
- package/dist/esm/tokens/default/utility/utility.js +1 -9
- package/dist/esm/use-theme-observer.js +34 -0
- package/dist/esm/utils/color-detection.js +23 -30
- package/dist/esm/utils/theme-loading.js +60 -0
- package/dist/esm/utils/theme-state-transformer.js +64 -0
- package/dist/esm/utils/token-ids.js +5 -4
- package/dist/esm/version.json +1 -1
- package/dist/types/artifacts/replacement-mapping.d.ts +1 -1
- package/dist/types/artifacts/theme-import-map.d.ts +16 -0
- package/dist/types/artifacts/themes/atlassian-dark.d.ts +7 -0
- package/dist/types/artifacts/themes/atlassian-legacy-dark.d.ts +7 -0
- package/dist/types/artifacts/themes/atlassian-legacy-light.d.ts +7 -0
- package/dist/types/artifacts/themes/atlassian-light.d.ts +7 -0
- package/dist/types/artifacts/themes/atlassian-spacing.d.ts +7 -0
- package/dist/types/artifacts/themes/atlassian-typography.d.ts +7 -0
- package/dist/types/artifacts/token-default-values.d.ts +2 -103
- package/dist/types/artifacts/token-names.d.ts +3 -205
- package/dist/types/artifacts/tokens-raw/atlassian-dark.d.ts +1 -115
- package/dist/types/artifacts/tokens-raw/atlassian-legacy-dark.d.ts +1 -115
- package/dist/types/artifacts/tokens-raw/atlassian-legacy-light.d.ts +1 -91
- package/dist/types/artifacts/tokens-raw/atlassian-light.d.ts +1 -91
- package/dist/types/artifacts/types-internal.d.ts +2 -2
- package/dist/types/artifacts/types.d.ts +2 -2
- package/dist/types/get-global-theme.d.ts +2 -0
- package/dist/types/index.d.ts +7 -3
- package/dist/types/set-global-theme.d.ts +63 -7
- package/dist/types/theme-config.d.ts +10 -7
- package/dist/types/{theme-change-observer.d.ts → theme-mutation-observer.d.ts} +2 -16
- package/dist/types/types.d.ts +1 -180
- package/dist/types/use-theme-observer.d.ts +15 -0
- package/dist/types/utils/theme-loading.d.ts +3 -0
- package/dist/types/utils/theme-state-transformer.d.ts +26 -0
- package/figma/atlassian-dark.json +1 -102
- package/figma/atlassian-legacy-dark.json +1 -102
- package/figma/atlassian-legacy-light.json +1 -102
- package/figma/atlassian-light.json +1 -102
- package/package.json +5 -8
- package/report.api.md +85 -219
- package/tmp/api-report-tmp.d.ts +58 -210
- package/css/atlassian-dark.css +0 -786
- package/css/atlassian-legacy-dark.css +0 -786
- package/css/atlassian-legacy-light.css +0 -786
- package/css/atlassian-light.css +0 -786
- package/dist/cjs/artifacts/typescript/atlassian-light-token-default-values.js +0 -407
- package/dist/cjs/tokens/atlassian-dark/deprecated/deprecated.js +0 -435
- package/dist/cjs/tokens/atlassian-legacy-dark/deprecated/deprecated.js +0 -532
- package/dist/cjs/tokens/atlassian-legacy-light/deprecated/deprecated.js +0 -523
- package/dist/cjs/tokens/atlassian-light/deprecated/deprecated.js +0 -421
- package/dist/cjs/tokens/default/deprecated/deprecated.js +0 -1186
- package/dist/es2019/artifacts/typescript/atlassian-light-token-default-values.js +0 -399
- package/dist/es2019/tokens/atlassian-dark/deprecated/deprecated.js +0 -428
- package/dist/es2019/tokens/atlassian-legacy-dark/deprecated/deprecated.js +0 -525
- package/dist/es2019/tokens/atlassian-legacy-light/deprecated/deprecated.js +0 -516
- package/dist/es2019/tokens/atlassian-light/deprecated/deprecated.js +0 -414
- package/dist/es2019/tokens/default/deprecated/deprecated.js +0 -1217
- package/dist/esm/artifacts/typescript/atlassian-light-token-default-values.js +0 -399
- package/dist/esm/tokens/atlassian-dark/deprecated/deprecated.js +0 -428
- package/dist/esm/tokens/atlassian-legacy-dark/deprecated/deprecated.js +0 -525
- package/dist/esm/tokens/atlassian-legacy-light/deprecated/deprecated.js +0 -516
- package/dist/esm/tokens/atlassian-light/deprecated/deprecated.js +0 -414
- package/dist/esm/tokens/default/deprecated/deprecated.js +0 -1179
- package/dist/types/artifacts/typescript/atlassian-light-token-default-values.d.ts +0 -399
- package/dist/types/tokens/atlassian-dark/deprecated/deprecated.d.ts +0 -4
- package/dist/types/tokens/atlassian-legacy-dark/deprecated/deprecated.d.ts +0 -4
- package/dist/types/tokens/atlassian-legacy-light/deprecated/deprecated.d.ts +0 -4
- package/dist/types/tokens/atlassian-light/deprecated/deprecated.d.ts +0 -4
- package/dist/types/tokens/default/deprecated/deprecated.d.ts +0 -4
|
@@ -9,41 +9,38 @@ export default function plugin() {
|
|
|
9
9
|
path.traverse({
|
|
10
10
|
CallExpression(path) {
|
|
11
11
|
const tokenImportScope = getTokenImportScope(path);
|
|
12
|
-
|
|
13
12
|
if (!tokenImportScope) {
|
|
14
13
|
return;
|
|
15
|
-
}
|
|
16
|
-
|
|
14
|
+
}
|
|
17
15
|
|
|
16
|
+
// Check arguments have correct format
|
|
18
17
|
if (!path.node.arguments[0]) {
|
|
19
18
|
throw new Error(`token() requires at least one argument`);
|
|
20
19
|
} else if (!t.isStringLiteral(path.node.arguments[0])) {
|
|
21
20
|
throw new Error(`token() must have a string as the first argument`);
|
|
22
21
|
} else if (path.node.arguments.length > 2) {
|
|
23
22
|
throw new Error(`token() does not accept ${path.node.arguments.length} arguments`);
|
|
24
|
-
}
|
|
25
|
-
|
|
23
|
+
}
|
|
26
24
|
|
|
25
|
+
// Check the token exists
|
|
27
26
|
const tokenName = path.node.arguments[0].value;
|
|
28
27
|
const cssTokenValue = tokenNames[tokenName];
|
|
29
|
-
|
|
30
28
|
if (!cssTokenValue) {
|
|
31
29
|
throw new Error(`token '${tokenName}' does not exist`);
|
|
32
30
|
}
|
|
31
|
+
var replacementNode;
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
// if no fallback is set, optionally find one from the default theme
|
|
36
34
|
if (path.node.arguments.length < 2) {
|
|
37
35
|
if (state.opts.shouldUseAutoFallback) {
|
|
38
36
|
replacementNode = t.stringLiteral(`var(${cssTokenValue}, ${getDefaultFallback(tokenName)})`);
|
|
39
37
|
} else {
|
|
40
38
|
replacementNode = t.stringLiteral(`var(${cssTokenValue})`);
|
|
41
39
|
}
|
|
42
|
-
}
|
|
43
|
-
|
|
40
|
+
}
|
|
44
41
|
|
|
42
|
+
// Handle fallbacks
|
|
45
43
|
const fallback = path.node.arguments[1];
|
|
46
|
-
|
|
47
44
|
if (t.isStringLiteral(fallback)) {
|
|
48
45
|
// String literals can be concatenated into css variable call
|
|
49
46
|
// Empty string fallbacks are ignored. For now, as the user did specify a fallback, no default is inserted
|
|
@@ -58,17 +55,15 @@ export default function plugin() {
|
|
|
58
55
|
raw: ')',
|
|
59
56
|
cooked: ')'
|
|
60
57
|
}, true)], [fallback]);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
replacementNode && path.replaceWith(replacementNode); // @ts-ignore crawl is a valid property
|
|
58
|
+
}
|
|
65
59
|
|
|
60
|
+
// Replace path and call scope.crawl() to refresh the scope bindings + references
|
|
61
|
+
replacementNode && path.replaceWith(replacementNode);
|
|
62
|
+
// @ts-ignore crawl is a valid property
|
|
66
63
|
tokenImportScope.crawl();
|
|
67
64
|
}
|
|
68
|
-
|
|
69
65
|
});
|
|
70
66
|
},
|
|
71
|
-
|
|
72
67
|
exit(path) {
|
|
73
68
|
path.traverse({
|
|
74
69
|
ImportDeclaration(path) {
|
|
@@ -76,84 +71,69 @@ export default function plugin() {
|
|
|
76
71
|
if (path.node.source.value !== '@atlaskit/tokens') {
|
|
77
72
|
return;
|
|
78
73
|
}
|
|
79
|
-
|
|
80
74
|
path.get('specifiers').forEach(specifier => {
|
|
81
75
|
if (!specifier.isImportSpecifier()) {
|
|
82
76
|
return;
|
|
83
77
|
}
|
|
84
|
-
|
|
85
78
|
if (getNonAliasedImportName(specifier.node) !== 'token') {
|
|
86
79
|
return;
|
|
87
80
|
}
|
|
81
|
+
const binding = path.scope.bindings[getAliasedImportName(specifier.node)];
|
|
88
82
|
|
|
89
|
-
|
|
90
|
-
|
|
83
|
+
// if no longer used, remove
|
|
91
84
|
if (!binding.referenced) {
|
|
92
85
|
specifier.remove();
|
|
93
86
|
}
|
|
94
|
-
});
|
|
87
|
+
});
|
|
95
88
|
|
|
89
|
+
// remove '@atlaskit/tokens' import if it is no longer needed
|
|
96
90
|
if (path.get('specifiers').length === 0) {
|
|
97
91
|
path.remove();
|
|
98
92
|
}
|
|
99
93
|
}
|
|
100
|
-
|
|
101
94
|
});
|
|
102
95
|
}
|
|
103
|
-
|
|
104
96
|
}
|
|
105
97
|
}
|
|
106
98
|
};
|
|
107
99
|
}
|
|
108
|
-
|
|
109
100
|
function getDefaultFallback(tokenName) {
|
|
110
101
|
return tokenDefaultValues[tokenName];
|
|
111
102
|
}
|
|
112
|
-
|
|
113
103
|
function getNonAliasedImportName(node) {
|
|
114
104
|
if (t.isIdentifier(node.imported)) {
|
|
115
105
|
return node.imported.name;
|
|
116
106
|
}
|
|
117
|
-
|
|
118
107
|
return node.imported.value;
|
|
119
108
|
}
|
|
120
|
-
|
|
121
109
|
function getAliasedImportName(node) {
|
|
122
110
|
return node.local.name;
|
|
123
111
|
}
|
|
112
|
+
|
|
124
113
|
/**
|
|
125
114
|
* Determine if the current call is to a token function, and
|
|
126
115
|
* return the relevant scope
|
|
127
116
|
*/
|
|
128
|
-
|
|
129
|
-
|
|
130
117
|
function getTokenImportScope(path) {
|
|
131
118
|
const callee = path.node.callee;
|
|
132
|
-
|
|
133
119
|
if (!t.isIdentifier(callee)) {
|
|
134
120
|
return undefined;
|
|
135
121
|
}
|
|
136
|
-
|
|
137
122
|
const binding = getTokenBinding(path.scope, callee.name);
|
|
138
|
-
|
|
139
123
|
if (!binding || !t.isImportSpecifier(binding.path.node)) {
|
|
140
124
|
return undefined;
|
|
141
125
|
}
|
|
142
|
-
|
|
143
126
|
if (binding.path.parent && t.isImportDeclaration(binding.path.parent)) {
|
|
144
127
|
if (binding.path.parent.source.value !== '@atlaskit/tokens') {
|
|
145
128
|
return undefined;
|
|
146
129
|
}
|
|
147
130
|
}
|
|
148
|
-
|
|
149
131
|
return getNonAliasedImportName(binding.path.node) === 'token' ? binding.scope : undefined;
|
|
150
132
|
}
|
|
151
|
-
|
|
152
133
|
function getTokenBinding(scope, name) {
|
|
153
134
|
if (!scope) {
|
|
154
135
|
return undefined;
|
|
155
136
|
}
|
|
156
|
-
|
|
157
137
|
if (scope.bindings[name]) {
|
|
158
138
|
return scope.bindings[name];
|
|
159
139
|
} else {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { COLOR_MODE_ATTRIBUTE, THEME_DATA_ATTRIBUTE } from './constants';
|
|
2
|
+
import { themeColorModes } from './theme-config';
|
|
3
|
+
import { themeStringToObject } from './utils/theme-state-transformer';
|
|
4
|
+
const isThemeColorMode = colorMode => {
|
|
5
|
+
return themeColorModes.find(mode => mode === colorMode) !== undefined;
|
|
6
|
+
};
|
|
7
|
+
export const getGlobalTheme = () => {
|
|
8
|
+
if (typeof document === 'undefined') {
|
|
9
|
+
return {};
|
|
10
|
+
}
|
|
11
|
+
const element = document.documentElement;
|
|
12
|
+
const colorMode = element.getAttribute(COLOR_MODE_ATTRIBUTE) || '';
|
|
13
|
+
const theme = element.getAttribute(THEME_DATA_ATTRIBUTE) || '';
|
|
14
|
+
return {
|
|
15
|
+
...themeStringToObject(theme),
|
|
16
|
+
...(isThemeColorMode(colorMode) && {
|
|
17
|
+
colorMode
|
|
18
|
+
})
|
|
19
|
+
};
|
|
20
|
+
};
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import warnOnce from '@atlaskit/ds-lib/warn-once';
|
|
2
2
|
import tokens from './artifacts/token-names';
|
|
3
3
|
const name = "@atlaskit/tokens";
|
|
4
|
-
const version = "0.
|
|
5
|
-
|
|
4
|
+
const version = "1.0.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.
|
|
@@ -27,18 +26,14 @@ const version = "0.13.5";
|
|
|
27
26
|
function getTokenValue(tokenId, fallback = '') {
|
|
28
27
|
let token = tokens[tokenId];
|
|
29
28
|
let tokenValue = fallback;
|
|
30
|
-
|
|
31
29
|
if (process.env.NODE_ENV !== 'production' && !token) {
|
|
32
30
|
warnOnce(`Unknown token id at path: ${tokenId} for ${name}@${version}`);
|
|
33
31
|
}
|
|
34
|
-
|
|
35
32
|
if (typeof window === 'undefined') {
|
|
36
33
|
return tokenValue;
|
|
37
34
|
}
|
|
38
|
-
|
|
39
35
|
tokenValue = window.getComputedStyle(document.documentElement).getPropertyValue(token);
|
|
40
36
|
tokenValue = tokenValue || fallback;
|
|
41
37
|
return tokenValue;
|
|
42
38
|
}
|
|
43
|
-
|
|
44
39
|
export default getTokenValue;
|
package/dist/es2019/get-token.js
CHANGED
|
@@ -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
|
const name = "@atlaskit/tokens";
|
|
5
|
-
const version = "0.
|
|
6
|
-
|
|
5
|
+
const version = "1.0.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 @@ const version = "0.13.5";
|
|
|
28
27
|
*/
|
|
29
28
|
function token(path, fallback) {
|
|
30
29
|
let 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: ${path} for ${name}@${version}`);
|
|
35
|
-
}
|
|
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
|
const tokenCall = fallback ? `var(${token}, ${fallback})` : `var(${token})`;
|
|
43
39
|
return tokenCall;
|
|
44
40
|
}
|
|
45
|
-
|
|
46
41
|
export default token;
|
package/dist/es2019/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
|
|
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';
|
|
@@ -1,46 +1,176 @@
|
|
|
1
|
+
import { bind } from 'bind-event-listener';
|
|
2
|
+
import noop from '@atlaskit/ds-lib/noop';
|
|
1
3
|
import { COLOR_MODE_ATTRIBUTE, THEME_DATA_ATTRIBUTE } from './constants';
|
|
2
|
-
import
|
|
4
|
+
import { loadAndAppendThemeCss, loadThemeCss } from './utils/theme-loading';
|
|
5
|
+
import { themeObjectToString } from './utils/theme-state-transformer';
|
|
6
|
+
const defaultColorMode = 'light';
|
|
7
|
+
const isMatchMediaAvailable = typeof window !== 'undefined' && 'matchMedia' in window;
|
|
8
|
+
const darkModeMediaQuery = '(prefers-color-scheme: dark)';
|
|
9
|
+
const darkModeMql = isMatchMediaAvailable && window.matchMedia(darkModeMediaQuery);
|
|
10
|
+
let unbindThemeChangeListener = noop;
|
|
11
|
+
const themeStateDefaults = {
|
|
12
|
+
colorMode: 'auto',
|
|
13
|
+
dark: 'dark',
|
|
14
|
+
light: 'light',
|
|
15
|
+
spacing: undefined,
|
|
16
|
+
typography: undefined
|
|
17
|
+
};
|
|
18
|
+
|
|
3
19
|
/**
|
|
4
|
-
*
|
|
20
|
+
* Updates the current theme when the system theme changes. Should be bound
|
|
21
|
+
* to an event listener listening on the '(prefers-color-scheme: dark)' query
|
|
22
|
+
* @param e The event representing a change in system theme.
|
|
23
|
+
*/
|
|
24
|
+
const checkNativeListener = function (e) {
|
|
25
|
+
const element = document.documentElement;
|
|
26
|
+
element.setAttribute(COLOR_MODE_ATTRIBUTE, e.matches ? 'dark' : 'light');
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Sets the theme globally at runtime. This updates the `data-theme` and `data-color-mode` attributes on your page's <html> tag.
|
|
5
31
|
*
|
|
6
|
-
* @param {string}
|
|
7
|
-
* @param {string}
|
|
32
|
+
* @param {Object<string, string>} themeState The themes and color mode that should be applied.
|
|
33
|
+
* @param {string} themeState.colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
|
|
34
|
+
* @param {string} themeState.dark The color theme to be applied when the color mode resolves to 'dark'.
|
|
35
|
+
* @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'.
|
|
36
|
+
* @param {string} themeState.spacing The spacing theme to be applied.
|
|
37
|
+
* @param {string} themeState.typography The typography theme to be applied.
|
|
38
|
+
*
|
|
39
|
+
* @returns A Promise of an unbind function, that can be used to stop listening for changes to system theme.
|
|
8
40
|
*
|
|
9
41
|
* @example
|
|
10
42
|
* ```
|
|
11
|
-
*
|
|
12
|
-
* setGlobalTheme('light', true);
|
|
43
|
+
* setGlobalTheme({colorMode: 'auto', light: 'light', dark: 'dark', spacing: 'spacing'});
|
|
13
44
|
* ```
|
|
14
45
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
const setGlobalTheme = async ({
|
|
47
|
+
colorMode = themeStateDefaults['colorMode'],
|
|
48
|
+
dark = themeStateDefaults['dark'],
|
|
49
|
+
light = themeStateDefaults['light'],
|
|
50
|
+
spacing = themeStateDefaults['spacing'],
|
|
51
|
+
typography = themeStateDefaults['typography']
|
|
52
|
+
} = {}) => {
|
|
53
|
+
// Dedupe list of themes to avoid race condition
|
|
54
|
+
const themePreferences = new Set([dark, light, spacing, typography]);
|
|
55
|
+
await Promise.all([...themePreferences].filter(themeId => themeId !== undefined).map(async themeId => await loadAndAppendThemeCss(themeId)));
|
|
56
|
+
if (colorMode === 'auto' && darkModeMql) {
|
|
57
|
+
colorMode = darkModeMql.matches ? 'dark' : 'light';
|
|
58
|
+
// Add an event listener for changes to the system theme.
|
|
59
|
+
// If the function exists, it will not be added again.
|
|
60
|
+
unbindThemeChangeListener = bind(darkModeMql, {
|
|
61
|
+
type: 'change',
|
|
62
|
+
listener: checkNativeListener
|
|
63
|
+
});
|
|
64
|
+
} else {
|
|
65
|
+
unbindThemeChangeListener();
|
|
32
66
|
}
|
|
67
|
+
const themeAttributes = getThemeHtmlAttrs({
|
|
68
|
+
colorMode,
|
|
69
|
+
dark,
|
|
70
|
+
light,
|
|
71
|
+
spacing,
|
|
72
|
+
typography
|
|
73
|
+
});
|
|
74
|
+
Object.entries(themeAttributes).forEach(([key, value]) => {
|
|
75
|
+
document.documentElement.setAttribute(key, value);
|
|
76
|
+
});
|
|
77
|
+
return unbindThemeChangeListener;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Takes an object containing theme preferences, and returns an array of objects for use in applying styles to the document head.
|
|
81
|
+
* 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.
|
|
82
|
+
*
|
|
83
|
+
* @param {Object<string, string>} themeState The themes and color mode that should be applied.
|
|
84
|
+
* @param {string} themeState.colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
|
|
85
|
+
* @param {string} themeState.dark The color theme to be applied when the color mode resolves to 'dark'.
|
|
86
|
+
* @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'.
|
|
87
|
+
* @param {string} themeState.spacing The spacing theme to be applied.
|
|
88
|
+
* @param {string} themeState.typography The typography theme to be applied.
|
|
89
|
+
*
|
|
90
|
+
* @returns A Promise of an object array, containing theme IDs, data-attributes to attach to the theme, and the theme CSS.
|
|
91
|
+
* If an error is encountered while loading themes, the themes arrav will be emptv.
|
|
92
|
+
*/
|
|
93
|
+
export const getThemeStyles = async ({
|
|
94
|
+
colorMode = themeStateDefaults['colorMode'],
|
|
95
|
+
dark = themeStateDefaults['dark'],
|
|
96
|
+
light = themeStateDefaults['light'],
|
|
97
|
+
spacing = themeStateDefaults['spacing'],
|
|
98
|
+
typography = themeStateDefaults['typography']
|
|
99
|
+
} = {}) => {
|
|
100
|
+
const themePreferences = colorMode === 'auto' ? [light, dark] : [colorMode];
|
|
101
|
+
[spacing, typography].forEach(themeId => {
|
|
102
|
+
if (themeId) {
|
|
103
|
+
themePreferences.push(themeId);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
const results = await Promise.all(themePreferences.map(async themeId => {
|
|
107
|
+
try {
|
|
108
|
+
const css = await loadThemeCss(themeId);
|
|
109
|
+
return {
|
|
110
|
+
id: themeId,
|
|
111
|
+
attrs: {
|
|
112
|
+
'data-theme': themeId
|
|
113
|
+
},
|
|
114
|
+
css
|
|
115
|
+
};
|
|
116
|
+
} catch {
|
|
117
|
+
// Return an empty string if there's an error loading it.
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
}));
|
|
121
|
+
return results.filter(theme => theme !== undefined);
|
|
122
|
+
};
|
|
33
123
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Server-side rendering utility. Generates the valid HTML attributes for a given theme.
|
|
126
|
+
* Note: this utility does not handle automatic theme switching.
|
|
127
|
+
*
|
|
128
|
+
* @param {Object<string, string>} themeOptions - Theme options object
|
|
129
|
+
* @param {string} themeState.colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
|
|
130
|
+
* @param {string} themeState.dark The color theme to be applied when the color mode resolves to 'dark'.
|
|
131
|
+
* @param {string} themeState.light The color theme to be applied when the color mode resolves to 'light'.
|
|
132
|
+
* @param {string} themeState.spacing The spacing theme to be applied.
|
|
133
|
+
* @param {string} themeState.typography The typography theme to be applied.
|
|
134
|
+
*
|
|
135
|
+
* @returns {Object} Object of HTML attributes to be applied to the document root
|
|
136
|
+
*/
|
|
137
|
+
export const getThemeHtmlAttrs = ({
|
|
138
|
+
colorMode = themeStateDefaults['colorMode'],
|
|
139
|
+
dark = themeStateDefaults['dark'],
|
|
140
|
+
light = themeStateDefaults['light'],
|
|
141
|
+
spacing = themeStateDefaults['spacing'],
|
|
142
|
+
typography = themeStateDefaults['typography']
|
|
143
|
+
} = {}) => {
|
|
144
|
+
const themePreferences = {
|
|
145
|
+
dark,
|
|
146
|
+
light,
|
|
147
|
+
spacing,
|
|
148
|
+
typography
|
|
149
|
+
};
|
|
150
|
+
const themeAttribute = themeObjectToString(themePreferences);
|
|
151
|
+
return {
|
|
152
|
+
[THEME_DATA_ATTRIBUTE]: themeAttribute,
|
|
153
|
+
[COLOR_MODE_ATTRIBUTE]: colorMode === 'auto' ? defaultColorMode : colorMode
|
|
154
|
+
};
|
|
155
|
+
};
|
|
40
156
|
|
|
41
|
-
|
|
42
|
-
|
|
157
|
+
/**
|
|
158
|
+
* Provides a script that, when executed before paint, sets the `data-color-mode` attribute based on the current system theme,
|
|
159
|
+
* to enable SSR support for automatic theme switching, avoid a flash of un-themed content on first paint.
|
|
160
|
+
*
|
|
161
|
+
* @param {string} colorMode Determines which color theme is applied. If set to `auto`, the theme applied will be determined by the OS setting.
|
|
162
|
+
*
|
|
163
|
+
* @returns {string} A string to be added to the innerHTML of a script tag in the document head
|
|
164
|
+
*/
|
|
165
|
+
export const getSSRAutoScript = colorMode => {
|
|
166
|
+
return colorMode === 'auto' ? `(
|
|
167
|
+
() => {
|
|
168
|
+
try {
|
|
169
|
+
const mql = window.matchMedia('${darkModeMediaQuery}');
|
|
170
|
+
const colorMode = mql.matches ? 'dark' : 'light';
|
|
171
|
+
document.documentElement.setAttribute('${COLOR_MODE_ATTRIBUTE}', colorMode);
|
|
172
|
+
} catch (e) {}
|
|
43
173
|
}
|
|
174
|
+
)()` : undefined;
|
|
44
175
|
};
|
|
45
|
-
|
|
46
176
|
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
|
|
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 const 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 const themeIds = ['light', 'dark', 'legacy-light', 'legacy-dark', 'spacing', 'typography'];
|
|
47
31
|
const themeConfig = {
|
|
48
32
|
'atlassian-light': {
|
|
49
33
|
id: 'light',
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const getGlobalTheme = () => typeof document !== 'undefined' ? document.documentElement.getAttribute(THEME_DATA_ATTRIBUTE) : null;
|
|
2
|
+
import { COLOR_MODE_ATTRIBUTE, THEME_DATA_ATTRIBUTE } from './constants';
|
|
3
|
+
import { getGlobalTheme } from './get-global-theme';
|
|
6
4
|
/**
|
|
7
5
|
* A MutationObserver which watches the `<html>` element for changes to the theme.
|
|
8
6
|
*
|
|
@@ -16,54 +14,23 @@ const getGlobalTheme = () => typeof document !== 'undefined' ? document.document
|
|
|
16
14
|
* observer.observe();
|
|
17
15
|
* ```
|
|
18
16
|
*/
|
|
19
|
-
|
|
20
|
-
|
|
21
17
|
export class ThemeMutationObserver {
|
|
22
18
|
constructor(callback) {
|
|
23
19
|
_defineProperty(this, "observer", null);
|
|
24
|
-
|
|
25
20
|
_defineProperty(this, "mediaObserver", null);
|
|
26
|
-
|
|
27
21
|
this.callback = callback;
|
|
28
22
|
}
|
|
29
|
-
|
|
30
23
|
observe() {
|
|
31
24
|
if (!this.observer) {
|
|
32
25
|
this.observer = new MutationObserver(() => {
|
|
33
26
|
this.callback(getGlobalTheme());
|
|
34
27
|
});
|
|
35
28
|
}
|
|
36
|
-
|
|
37
29
|
this.observer.observe(document.documentElement, {
|
|
38
|
-
attributeFilter: [THEME_DATA_ATTRIBUTE]
|
|
30
|
+
attributeFilter: [THEME_DATA_ATTRIBUTE, COLOR_MODE_ATTRIBUTE]
|
|
39
31
|
});
|
|
40
32
|
}
|
|
41
|
-
|
|
42
33
|
disconnect() {
|
|
43
34
|
this.observer && this.observer.disconnect();
|
|
44
35
|
}
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* A React hook which returns the current theme set on `<html>`, or `null` if not set.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```
|
|
52
|
-
* const theme = useThemeObserver(); // Returns 'light' or 'dark'
|
|
53
|
-
*
|
|
54
|
-
* // Performing side effects when it changes
|
|
55
|
-
* useEffect(() => {
|
|
56
|
-
* console.log(`The theme has changed to ${theme}`);
|
|
57
|
-
* }, [theme]);
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
|
-
export const useThemeObserver = () => {
|
|
62
|
-
const [theme, setTheme] = useState(getGlobalTheme());
|
|
63
|
-
useEffect(() => {
|
|
64
|
-
const observer = new ThemeMutationObserver(theme => setTheme(theme));
|
|
65
|
-
observer.observe();
|
|
66
|
-
return () => observer.disconnect();
|
|
67
|
-
}, []);
|
|
68
|
-
return theme;
|
|
69
|
-
};
|
|
36
|
+
}
|