@atlaskit/tokens 0.10.1 → 0.10.4
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 +21 -0
- package/dist/cjs/constants.js +8 -4
- package/dist/cjs/get-token.js +9 -2
- package/dist/cjs/index.js +15 -1
- package/dist/cjs/set-global-theme.js +4 -3
- package/dist/cjs/theme-change-observer.js +105 -0
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/constants.js +4 -2
- package/dist/es2019/get-token.js +8 -2
- package/dist/es2019/index.js +2 -1
- package/dist/es2019/set-global-theme.js +4 -3
- package/dist/es2019/theme-change-observer.js +67 -0
- package/dist/es2019/version.json +1 -1
- package/dist/esm/constants.js +4 -2
- package/dist/esm/get-token.js +8 -2
- package/dist/esm/index.js +2 -1
- package/dist/esm/set-global-theme.js +4 -3
- package/dist/esm/theme-change-observer.js +89 -0
- package/dist/esm/version.json +1 -1
- package/dist/types/constants.d.ts +4 -2
- package/dist/types/index.d.ts +2 -0
- package/dist/types/set-global-theme.d.ts +2 -1
- package/dist/types/theme-change-observer.d.ts +35 -0
- package/dist/types/types.d.ts +2 -0
- package/package.json +11 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @atlaskit/tokens
|
|
2
2
|
|
|
3
|
+
## 0.10.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`7b9be57869b`](https://bitbucket.org/atlassian/atlassian-frontend/commits/7b9be57869b) - ED-14905 made fallback token name work for production
|
|
8
|
+
|
|
9
|
+
## 0.10.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [`acbd8d5576a`](https://bitbucket.org/atlassian/atlassian-frontend/commits/acbd8d5576a) - Added a color-contrast test for our color pairs in the Tokens package.
|
|
14
|
+
|
|
15
|
+
## 0.10.2
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [`5b8212f08b0`](https://bitbucket.org/atlassian/atlassian-frontend/commits/5b8212f08b0) - The current theme can now be accessed and monitored for changes using new exports.
|
|
20
|
+
|
|
21
|
+
- `useThemeObserver` React hook
|
|
22
|
+
- `ThemeMutationObserver` Mutation Observer
|
|
23
|
+
|
|
3
24
|
## 0.10.1
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
package/dist/cjs/constants.js
CHANGED
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
7
|
-
var
|
|
8
|
-
exports.
|
|
6
|
+
exports.TOKEN_NOT_FOUND_CSS_VAR = exports.THEME_DATA_ATTRIBUTE = exports.THEMES = exports.LONG_SHORT_MAPPING = exports.DEFAULT_THEME = exports.CSS_PREFIX = void 0;
|
|
7
|
+
var THEMES = ['light', 'dark'];
|
|
8
|
+
exports.THEMES = THEMES;
|
|
9
|
+
var THEME_DATA_ATTRIBUTE = 'data-theme';
|
|
10
|
+
exports.THEME_DATA_ATTRIBUTE = THEME_DATA_ATTRIBUTE;
|
|
9
11
|
var DEFAULT_THEME = 'light';
|
|
10
12
|
exports.DEFAULT_THEME = DEFAULT_THEME;
|
|
11
13
|
var CSS_PREFIX = 'ds'; // Maps the longer theme name to a shorthand used in css/code
|
|
@@ -15,4 +17,6 @@ var LONG_SHORT_MAPPING = {
|
|
|
15
17
|
'atlassian-light': 'light',
|
|
16
18
|
'atlassian-dark': 'dark'
|
|
17
19
|
};
|
|
18
|
-
exports.LONG_SHORT_MAPPING = LONG_SHORT_MAPPING;
|
|
20
|
+
exports.LONG_SHORT_MAPPING = LONG_SHORT_MAPPING;
|
|
21
|
+
var TOKEN_NOT_FOUND_CSS_VAR = "--".concat(CSS_PREFIX, "-token-not-found");
|
|
22
|
+
exports.TOKEN_NOT_FOUND_CSS_VAR = TOKEN_NOT_FOUND_CSS_VAR;
|
package/dist/cjs/get-token.js
CHANGED
|
@@ -11,15 +11,22 @@ var _warnOnce = _interopRequireDefault(require("@atlaskit/ds-lib/warn-once"));
|
|
|
11
11
|
|
|
12
12
|
var _tokenNames = _interopRequireDefault(require("./artifacts/token-names"));
|
|
13
13
|
|
|
14
|
+
var _constants = require("./constants");
|
|
15
|
+
|
|
14
16
|
var name = "@atlaskit/tokens";
|
|
15
|
-
var version = "0.10.
|
|
17
|
+
var version = "0.10.4";
|
|
16
18
|
|
|
17
19
|
function token(path, fallback) {
|
|
18
20
|
var token = _tokenNames.default[path];
|
|
19
21
|
|
|
20
22
|
if (process.env.NODE_ENV !== 'production' && !token) {
|
|
21
|
-
(0, _warnOnce.default)("Unknown token id at path: ".concat(path, " for ").concat(name, "@").concat(version));
|
|
22
23
|
token = _tokenNames.default['utility.UNSAFE_util.MISSING_TOKEN'];
|
|
24
|
+
(0, _warnOnce.default)("Unknown token id at path: ".concat(path, " for ").concat(name, "@").concat(version));
|
|
25
|
+
} // 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
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if (!token) {
|
|
29
|
+
token = _constants.TOKEN_NOT_FOUND_CSS_VAR;
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
var tokenCall = fallback ? "var(".concat(token, ", ").concat(fallback, ")") : "var(".concat(token, ")");
|
package/dist/cjs/index.js
CHANGED
|
@@ -5,6 +5,12 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
+
Object.defineProperty(exports, "ThemeMutationObserver", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function get() {
|
|
11
|
+
return _themeChangeObserver.ThemeMutationObserver;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
8
14
|
Object.defineProperty(exports, "setGlobalTheme", {
|
|
9
15
|
enumerable: true,
|
|
10
16
|
get: function get() {
|
|
@@ -17,7 +23,15 @@ Object.defineProperty(exports, "token", {
|
|
|
17
23
|
return _getToken.default;
|
|
18
24
|
}
|
|
19
25
|
});
|
|
26
|
+
Object.defineProperty(exports, "useThemeObserver", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function get() {
|
|
29
|
+
return _themeChangeObserver.useThemeObserver;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
20
32
|
|
|
21
33
|
var _getToken = _interopRequireDefault(require("./get-token"));
|
|
22
34
|
|
|
23
|
-
var _setGlobalTheme = _interopRequireDefault(require("./set-global-theme"));
|
|
35
|
+
var _setGlobalTheme = _interopRequireDefault(require("./set-global-theme"));
|
|
36
|
+
|
|
37
|
+
var _themeChangeObserver = require("./theme-change-observer");
|
|
@@ -5,11 +5,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
+
var _constants = require("./constants");
|
|
9
|
+
|
|
8
10
|
var setGlobalTheme = function setGlobalTheme(theme) {
|
|
9
11
|
if (process.env.NODE_ENV !== 'production') {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
throw new Error("setGlobalTheme only accepts 'light' or 'dark' themes");
|
|
12
|
+
if (!_constants.THEMES.includes(theme)) {
|
|
13
|
+
throw new Error("setGlobalTheme only accepts themes: ".concat(_constants.THEMES.join(', ')));
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
16
|
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.useThemeObserver = exports.ThemeMutationObserver = void 0;
|
|
9
|
+
|
|
10
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
11
|
+
|
|
12
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
13
|
+
|
|
14
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
15
|
+
|
|
16
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
17
|
+
|
|
18
|
+
var _react = require("react");
|
|
19
|
+
|
|
20
|
+
var _constants = require("./constants");
|
|
21
|
+
|
|
22
|
+
var getGlobalTheme = function getGlobalTheme() {
|
|
23
|
+
return typeof document !== 'undefined' ? document.documentElement.getAttribute(_constants.THEME_DATA_ATTRIBUTE) : null;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* A MutationObserver which watches the `<html>` element for changes to the theme.
|
|
27
|
+
*
|
|
28
|
+
* In React, use the {@link useThemeObserver `useThemeObserver`} hook instead.
|
|
29
|
+
*
|
|
30
|
+
* @param {function} callback - A callback function which fires when the theme changes.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```
|
|
34
|
+
* const observer = new ThemeMutationObserver((theme) => {});
|
|
35
|
+
* observer.observe();
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
var ThemeMutationObserver = /*#__PURE__*/function () {
|
|
41
|
+
function ThemeMutationObserver(callback) {
|
|
42
|
+
(0, _classCallCheck2.default)(this, ThemeMutationObserver);
|
|
43
|
+
(0, _defineProperty2.default)(this, "observer", null);
|
|
44
|
+
this.callback = callback;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
(0, _createClass2.default)(ThemeMutationObserver, [{
|
|
48
|
+
key: "observe",
|
|
49
|
+
value: function observe() {
|
|
50
|
+
var _this = this;
|
|
51
|
+
|
|
52
|
+
if (!this.observer) {
|
|
53
|
+
this.observer = new MutationObserver(function () {
|
|
54
|
+
_this.callback(getGlobalTheme());
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.observer.observe(document.documentElement, {
|
|
59
|
+
attributeFilter: [_constants.THEME_DATA_ATTRIBUTE]
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}, {
|
|
63
|
+
key: "disconnect",
|
|
64
|
+
value: function disconnect() {
|
|
65
|
+
this.observer && this.observer.disconnect();
|
|
66
|
+
}
|
|
67
|
+
}]);
|
|
68
|
+
return ThemeMutationObserver;
|
|
69
|
+
}();
|
|
70
|
+
/**
|
|
71
|
+
* A React hook which returns the current theme set on `<html>`, or `null` if not set.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```
|
|
75
|
+
* const theme = useThemeObserver(); // Returns 'light' or 'dark'
|
|
76
|
+
*
|
|
77
|
+
* // Performing side effects when it changes
|
|
78
|
+
* useEffect(() => {
|
|
79
|
+
* console.log(`The theme has changed to ${theme}`);
|
|
80
|
+
* }, [theme]);
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
exports.ThemeMutationObserver = ThemeMutationObserver;
|
|
86
|
+
|
|
87
|
+
var useThemeObserver = function useThemeObserver() {
|
|
88
|
+
var _useState = (0, _react.useState)(getGlobalTheme()),
|
|
89
|
+
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
|
90
|
+
theme = _useState2[0],
|
|
91
|
+
setTheme = _useState2[1];
|
|
92
|
+
|
|
93
|
+
(0, _react.useEffect)(function () {
|
|
94
|
+
var observer = new ThemeMutationObserver(function (theme) {
|
|
95
|
+
return setTheme(theme);
|
|
96
|
+
});
|
|
97
|
+
observer.observe();
|
|
98
|
+
return function () {
|
|
99
|
+
return observer.disconnect();
|
|
100
|
+
};
|
|
101
|
+
}, []);
|
|
102
|
+
return theme;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
exports.useThemeObserver = useThemeObserver;
|
package/dist/cjs/version.json
CHANGED
package/dist/es2019/constants.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export const
|
|
1
|
+
export const THEMES = ['light', 'dark'];
|
|
2
|
+
export const THEME_DATA_ATTRIBUTE = 'data-theme';
|
|
2
3
|
export const DEFAULT_THEME = 'light';
|
|
3
4
|
export const CSS_PREFIX = 'ds'; // Maps the longer theme name to a shorthand used in css/code
|
|
4
5
|
|
|
5
6
|
export const LONG_SHORT_MAPPING = {
|
|
6
7
|
'atlassian-light': 'light',
|
|
7
8
|
'atlassian-dark': 'dark'
|
|
8
|
-
};
|
|
9
|
+
};
|
|
10
|
+
export const TOKEN_NOT_FOUND_CSS_VAR = `--${CSS_PREFIX}-token-not-found`;
|
package/dist/es2019/get-token.js
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import warnOnce from '@atlaskit/ds-lib/warn-once';
|
|
2
2
|
import tokens from './artifacts/token-names';
|
|
3
|
+
import { TOKEN_NOT_FOUND_CSS_VAR } from './constants';
|
|
3
4
|
const name = "@atlaskit/tokens";
|
|
4
|
-
const version = "0.10.
|
|
5
|
+
const version = "0.10.4";
|
|
5
6
|
|
|
6
7
|
function token(path, fallback) {
|
|
7
8
|
let token = tokens[path];
|
|
8
9
|
|
|
9
10
|
if (process.env.NODE_ENV !== 'production' && !token) {
|
|
10
|
-
warnOnce(`Unknown token id at path: ${path} for ${name}@${version}`);
|
|
11
11
|
token = tokens['utility.UNSAFE_util.MISSING_TOKEN'];
|
|
12
|
+
warnOnce(`Unknown token id at path: ${path} for ${name}@${version}`);
|
|
13
|
+
} // 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
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
if (!token) {
|
|
17
|
+
token = TOKEN_NOT_FOUND_CSS_VAR;
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
const tokenCall = fallback ? `var(${token}, ${fallback})` : `var(${token})`;
|
package/dist/es2019/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { THEMES } from './constants';
|
|
2
|
+
|
|
1
3
|
const setGlobalTheme = theme => {
|
|
2
4
|
if (process.env.NODE_ENV !== 'production') {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
throw new Error("setGlobalTheme only accepts 'light' or 'dark' themes");
|
|
5
|
+
if (!THEMES.includes(theme)) {
|
|
6
|
+
throw new Error(`setGlobalTheme only accepts themes: ${THEMES.join(', ')}`);
|
|
6
7
|
}
|
|
7
8
|
}
|
|
8
9
|
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { THEME_DATA_ATTRIBUTE } from './constants';
|
|
4
|
+
|
|
5
|
+
const getGlobalTheme = () => typeof document !== 'undefined' ? document.documentElement.getAttribute(THEME_DATA_ATTRIBUTE) : null;
|
|
6
|
+
/**
|
|
7
|
+
* A MutationObserver which watches the `<html>` element for changes to the theme.
|
|
8
|
+
*
|
|
9
|
+
* In React, use the {@link useThemeObserver `useThemeObserver`} hook instead.
|
|
10
|
+
*
|
|
11
|
+
* @param {function} callback - A callback function which fires when the theme changes.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```
|
|
15
|
+
* const observer = new ThemeMutationObserver((theme) => {});
|
|
16
|
+
* observer.observe();
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export class ThemeMutationObserver {
|
|
22
|
+
constructor(callback) {
|
|
23
|
+
_defineProperty(this, "observer", null);
|
|
24
|
+
|
|
25
|
+
this.callback = callback;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
observe() {
|
|
29
|
+
if (!this.observer) {
|
|
30
|
+
this.observer = new MutationObserver(() => {
|
|
31
|
+
this.callback(getGlobalTheme());
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.observer.observe(document.documentElement, {
|
|
36
|
+
attributeFilter: [THEME_DATA_ATTRIBUTE]
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
disconnect() {
|
|
41
|
+
this.observer && this.observer.disconnect();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A React hook which returns the current theme set on `<html>`, or `null` if not set.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```
|
|
50
|
+
* const theme = useThemeObserver(); // Returns 'light' or 'dark'
|
|
51
|
+
*
|
|
52
|
+
* // Performing side effects when it changes
|
|
53
|
+
* useEffect(() => {
|
|
54
|
+
* console.log(`The theme has changed to ${theme}`);
|
|
55
|
+
* }, [theme]);
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
export const useThemeObserver = () => {
|
|
60
|
+
const [theme, setTheme] = useState(getGlobalTheme());
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
const observer = new ThemeMutationObserver(theme => setTheme(theme));
|
|
63
|
+
observer.observe();
|
|
64
|
+
return () => observer.disconnect();
|
|
65
|
+
}, []);
|
|
66
|
+
return theme;
|
|
67
|
+
};
|
package/dist/es2019/version.json
CHANGED
package/dist/esm/constants.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export var
|
|
1
|
+
export var THEMES = ['light', 'dark'];
|
|
2
|
+
export var THEME_DATA_ATTRIBUTE = 'data-theme';
|
|
2
3
|
export var DEFAULT_THEME = 'light';
|
|
3
4
|
export var CSS_PREFIX = 'ds'; // Maps the longer theme name to a shorthand used in css/code
|
|
4
5
|
|
|
5
6
|
export var LONG_SHORT_MAPPING = {
|
|
6
7
|
'atlassian-light': 'light',
|
|
7
8
|
'atlassian-dark': 'dark'
|
|
8
|
-
};
|
|
9
|
+
};
|
|
10
|
+
export var TOKEN_NOT_FOUND_CSS_VAR = "--".concat(CSS_PREFIX, "-token-not-found");
|
package/dist/esm/get-token.js
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import warnOnce from '@atlaskit/ds-lib/warn-once';
|
|
2
2
|
import tokens from './artifacts/token-names';
|
|
3
|
+
import { TOKEN_NOT_FOUND_CSS_VAR } from './constants';
|
|
3
4
|
var name = "@atlaskit/tokens";
|
|
4
|
-
var version = "0.10.
|
|
5
|
+
var version = "0.10.4";
|
|
5
6
|
|
|
6
7
|
function token(path, fallback) {
|
|
7
8
|
var token = tokens[path];
|
|
8
9
|
|
|
9
10
|
if (process.env.NODE_ENV !== 'production' && !token) {
|
|
10
|
-
warnOnce("Unknown token id at path: ".concat(path, " for ").concat(name, "@").concat(version));
|
|
11
11
|
token = tokens['utility.UNSAFE_util.MISSING_TOKEN'];
|
|
12
|
+
warnOnce("Unknown token id at path: ".concat(path, " for ").concat(name, "@").concat(version));
|
|
13
|
+
} // 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
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
if (!token) {
|
|
17
|
+
token = TOKEN_NOT_FOUND_CSS_VAR;
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
var tokenCall = fallback ? "var(".concat(token, ", ").concat(fallback, ")") : "var(".concat(token, ")");
|
package/dist/esm/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { THEMES } from './constants';
|
|
2
|
+
|
|
1
3
|
var setGlobalTheme = function setGlobalTheme(theme) {
|
|
2
4
|
if (process.env.NODE_ENV !== 'production') {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
throw new Error("setGlobalTheme only accepts 'light' or 'dark' themes");
|
|
5
|
+
if (!THEMES.includes(theme)) {
|
|
6
|
+
throw new Error("setGlobalTheme only accepts themes: ".concat(THEMES.join(', ')));
|
|
6
7
|
}
|
|
7
8
|
}
|
|
8
9
|
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
3
|
+
import _createClass from "@babel/runtime/helpers/createClass";
|
|
4
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
5
|
+
import { useEffect, useState } from 'react';
|
|
6
|
+
import { THEME_DATA_ATTRIBUTE } from './constants';
|
|
7
|
+
|
|
8
|
+
var getGlobalTheme = function getGlobalTheme() {
|
|
9
|
+
return typeof document !== 'undefined' ? document.documentElement.getAttribute(THEME_DATA_ATTRIBUTE) : null;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* A MutationObserver which watches the `<html>` element for changes to the theme.
|
|
13
|
+
*
|
|
14
|
+
* In React, use the {@link useThemeObserver `useThemeObserver`} hook instead.
|
|
15
|
+
*
|
|
16
|
+
* @param {function} callback - A callback function which fires when the theme changes.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```
|
|
20
|
+
* const observer = new ThemeMutationObserver((theme) => {});
|
|
21
|
+
* observer.observe();
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
export var ThemeMutationObserver = /*#__PURE__*/function () {
|
|
27
|
+
function ThemeMutationObserver(callback) {
|
|
28
|
+
_classCallCheck(this, ThemeMutationObserver);
|
|
29
|
+
|
|
30
|
+
_defineProperty(this, "observer", null);
|
|
31
|
+
|
|
32
|
+
this.callback = callback;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_createClass(ThemeMutationObserver, [{
|
|
36
|
+
key: "observe",
|
|
37
|
+
value: function observe() {
|
|
38
|
+
var _this = this;
|
|
39
|
+
|
|
40
|
+
if (!this.observer) {
|
|
41
|
+
this.observer = new MutationObserver(function () {
|
|
42
|
+
_this.callback(getGlobalTheme());
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.observer.observe(document.documentElement, {
|
|
47
|
+
attributeFilter: [THEME_DATA_ATTRIBUTE]
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}, {
|
|
51
|
+
key: "disconnect",
|
|
52
|
+
value: function disconnect() {
|
|
53
|
+
this.observer && this.observer.disconnect();
|
|
54
|
+
}
|
|
55
|
+
}]);
|
|
56
|
+
|
|
57
|
+
return ThemeMutationObserver;
|
|
58
|
+
}();
|
|
59
|
+
/**
|
|
60
|
+
* A React hook which returns the current theme set on `<html>`, or `null` if not set.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```
|
|
64
|
+
* const theme = useThemeObserver(); // Returns 'light' or 'dark'
|
|
65
|
+
*
|
|
66
|
+
* // Performing side effects when it changes
|
|
67
|
+
* useEffect(() => {
|
|
68
|
+
* console.log(`The theme has changed to ${theme}`);
|
|
69
|
+
* }, [theme]);
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
export var useThemeObserver = function useThemeObserver() {
|
|
74
|
+
var _useState = useState(getGlobalTheme()),
|
|
75
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
76
|
+
theme = _useState2[0],
|
|
77
|
+
setTheme = _useState2[1];
|
|
78
|
+
|
|
79
|
+
useEffect(function () {
|
|
80
|
+
var observer = new ThemeMutationObserver(function (theme) {
|
|
81
|
+
return setTheme(theme);
|
|
82
|
+
});
|
|
83
|
+
observer.observe();
|
|
84
|
+
return function () {
|
|
85
|
+
return observer.disconnect();
|
|
86
|
+
};
|
|
87
|
+
}, []);
|
|
88
|
+
return theme;
|
|
89
|
+
};
|
package/dist/esm/version.json
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const THEMES: readonly ["light", "dark"];
|
|
2
|
+
export declare const THEME_DATA_ATTRIBUTE = "data-theme";
|
|
2
3
|
export declare const DEFAULT_THEME = "light";
|
|
3
4
|
export declare const CSS_PREFIX = "ds";
|
|
4
|
-
export declare const LONG_SHORT_MAPPING: Record<string,
|
|
5
|
+
export declare const LONG_SHORT_MAPPING: Record<string, typeof THEMES[number]>;
|
|
6
|
+
export declare const TOKEN_NOT_FOUND_CSS_VAR: string;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { default as token } from './get-token';
|
|
2
2
|
export { default as setGlobalTheme } from './set-global-theme';
|
|
3
3
|
export type { CSSToken } from './artifacts/token-names';
|
|
4
|
+
export type { Themes } from './types';
|
|
5
|
+
export { useThemeObserver, ThemeMutationObserver, } from './theme-change-observer';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Themes } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* A MutationObserver which watches the `<html>` element for changes to the theme.
|
|
4
|
+
*
|
|
5
|
+
* In React, use the {@link useThemeObserver `useThemeObserver`} hook instead.
|
|
6
|
+
*
|
|
7
|
+
* @param {function} callback - A callback function which fires when the theme changes.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```
|
|
11
|
+
* const observer = new ThemeMutationObserver((theme) => {});
|
|
12
|
+
* observer.observe();
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare class ThemeMutationObserver {
|
|
16
|
+
private callback;
|
|
17
|
+
observer: MutationObserver | null;
|
|
18
|
+
constructor(callback: (theme: Themes | null) => unknown);
|
|
19
|
+
observe(): void;
|
|
20
|
+
disconnect(): void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A React hook which returns the current theme set on `<html>`, or `null` if not set.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```
|
|
27
|
+
* const theme = useThemeObserver(); // Returns 'light' or 'dark'
|
|
28
|
+
*
|
|
29
|
+
* // Performing side effects when it changes
|
|
30
|
+
* useEffect(() => {
|
|
31
|
+
* console.log(`The theme has changed to ${theme}`);
|
|
32
|
+
* }, [theme]);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare const useThemeObserver: () => Themes | null;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { InternalTokenIds } from './artifacts/types-internal';
|
|
2
|
+
import { THEMES } from './constants';
|
|
2
3
|
export declare type Groups = 'raw' | 'paint' | 'shadow' | 'palette';
|
|
3
4
|
export declare type ActiveTokenState = 'active';
|
|
4
5
|
export declare type DeprecatedTokenState = 'deprecated';
|
|
@@ -6,6 +7,7 @@ export declare type DeletedTokenState = 'deleted';
|
|
|
6
7
|
export declare type TokenState = ActiveTokenState | DeprecatedTokenState | DeletedTokenState;
|
|
7
8
|
export declare type Replacement = InternalTokenIds | InternalTokenIds[];
|
|
8
9
|
export declare type PaletteCategory = 'blue' | 'purple' | 'red' | 'magenta' | 'orange' | 'yellow' | 'green' | 'teal' | 'light mode neutral' | 'dark mode neutral';
|
|
10
|
+
export declare type Themes = typeof THEMES[number];
|
|
9
11
|
export interface Token<TValue, Group extends Groups> {
|
|
10
12
|
value: TValue;
|
|
11
13
|
attributes: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaskit/tokens",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.4",
|
|
4
4
|
"author": "Atlassian Pty Ltd",
|
|
5
5
|
"description": "Design tokens are the single source of truth to name and store design decisions.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"draft": true
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
|
-
"repository": "https://bitbucket.org/atlassian/atlassian-frontend",
|
|
19
|
+
"repository": "https://bitbucket.org/atlassian/atlassian-frontend-mirror",
|
|
20
20
|
"main": "dist/cjs/index.js",
|
|
21
21
|
"module": "dist/esm/index.js",
|
|
22
22
|
"module:es2019": "dist/es2019/index.js",
|
|
@@ -47,13 +47,17 @@
|
|
|
47
47
|
"@babel/traverse": "^7.15.0",
|
|
48
48
|
"@babel/types": "^7.15.0"
|
|
49
49
|
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"react": "^16.8.0"
|
|
52
|
+
},
|
|
50
53
|
"devDependencies": {
|
|
54
|
+
"@af/accessibility-testing": "*",
|
|
51
55
|
"@af/codegen": "*",
|
|
52
56
|
"@atlaskit/badge": "^15.0.11",
|
|
53
57
|
"@atlaskit/button": "^16.3.0",
|
|
54
58
|
"@atlaskit/code": "^14.3.0",
|
|
55
59
|
"@atlaskit/docs": "^9.0.10",
|
|
56
|
-
"@atlaskit/dropdown-menu": "^11.
|
|
60
|
+
"@atlaskit/dropdown-menu": "^11.2.0",
|
|
57
61
|
"@atlaskit/empty-state": "^7.3.9",
|
|
58
62
|
"@atlaskit/focus-ring": "^1.0.0",
|
|
59
63
|
"@atlaskit/heading": "^0.1.8",
|
|
@@ -62,6 +66,7 @@
|
|
|
62
66
|
"@atlaskit/modal-dialog": "^12.2.4",
|
|
63
67
|
"@atlaskit/section-message": "^6.1.7",
|
|
64
68
|
"@atlaskit/tabs": "^13.2.4",
|
|
69
|
+
"@atlaskit/tag": "^11.3.1",
|
|
65
70
|
"@atlaskit/textfield": "^5.1.5",
|
|
66
71
|
"@atlaskit/theme": "^12.1.2",
|
|
67
72
|
"@atlaskit/toggle": "^12.4.2",
|
|
@@ -69,6 +74,9 @@
|
|
|
69
74
|
"@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
|
|
70
75
|
"@babel/core": "^7.12.3",
|
|
71
76
|
"@emotion/core": "^10.0.9",
|
|
77
|
+
"@testing-library/dom": "^7.7.3",
|
|
78
|
+
"@testing-library/react": "^8.0.1",
|
|
79
|
+
"@testing-library/react-hooks": "^1.0.4",
|
|
72
80
|
"@types/chrome": "^0.0.171",
|
|
73
81
|
"copy-webpack-plugin": "^6.4.0",
|
|
74
82
|
"lodash": "^4.17.21",
|