@atlaskit/app-provider 3.2.9 → 3.3.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 +15 -0
- package/dist/cjs/app-provider.js +4 -6
- package/dist/cjs/context.js +16 -0
- package/dist/cjs/index.js +12 -9
- package/dist/cjs/theme-provider/context/color-mode.js +16 -0
- package/dist/cjs/theme-provider/context/inside-theme-provider.js +11 -0
- package/dist/cjs/theme-provider/context/theme.js +16 -0
- package/dist/cjs/theme-provider/hooks/use-color-mode-for-migration.js +19 -0
- package/dist/cjs/theme-provider/hooks/use-color-mode.js +41 -0
- package/dist/cjs/theme-provider/hooks/use-is-inside-theme-provider.js +17 -0
- package/dist/cjs/theme-provider/hooks/use-set-color-mode.js +20 -0
- package/dist/cjs/theme-provider/hooks/use-set-theme.js +20 -0
- package/dist/cjs/theme-provider/hooks/use-theme.js +35 -0
- package/dist/cjs/theme-provider/index.js +221 -0
- package/dist/cjs/theme-provider/utils/is-theme-mounted.js +21 -0
- package/dist/cjs/theme-provider/utils/load-and-mount-themes.js +130 -0
- package/dist/es2019/app-provider.js +3 -3
- package/dist/es2019/context.js +11 -0
- package/dist/es2019/index.js +10 -1
- package/dist/es2019/theme-provider/context/color-mode.js +10 -0
- package/dist/es2019/theme-provider/context/inside-theme-provider.js +6 -0
- package/dist/es2019/theme-provider/context/theme.js +10 -0
- package/dist/es2019/theme-provider/hooks/use-color-mode-for-migration.js +14 -0
- package/dist/es2019/theme-provider/hooks/use-color-mode.js +29 -0
- package/dist/es2019/theme-provider/hooks/use-is-inside-theme-provider.js +12 -0
- package/dist/es2019/theme-provider/hooks/use-set-color-mode.js +15 -0
- package/dist/es2019/theme-provider/hooks/use-set-theme.js +15 -0
- package/dist/es2019/theme-provider/hooks/use-theme.js +23 -0
- package/dist/es2019/theme-provider/index.js +170 -0
- package/dist/es2019/theme-provider/utils/is-theme-mounted.js +16 -0
- package/dist/es2019/theme-provider/utils/load-and-mount-themes.js +47 -0
- package/dist/esm/app-provider.js +3 -3
- package/dist/esm/context.js +11 -0
- package/dist/esm/index.js +10 -1
- package/dist/esm/theme-provider/context/color-mode.js +10 -0
- package/dist/esm/theme-provider/context/inside-theme-provider.js +6 -0
- package/dist/esm/theme-provider/context/theme.js +10 -0
- package/dist/esm/theme-provider/hooks/use-color-mode-for-migration.js +14 -0
- package/dist/esm/theme-provider/hooks/use-color-mode.js +35 -0
- package/dist/esm/theme-provider/hooks/use-is-inside-theme-provider.js +12 -0
- package/dist/esm/theme-provider/hooks/use-set-color-mode.js +15 -0
- package/dist/esm/theme-provider/hooks/use-set-theme.js +15 -0
- package/dist/esm/theme-provider/hooks/use-theme.js +29 -0
- package/dist/esm/theme-provider/index.js +212 -0
- package/dist/esm/theme-provider/utils/is-theme-mounted.js +16 -0
- package/dist/esm/theme-provider/utils/load-and-mount-themes.js +123 -0
- package/dist/types/app-provider.d.ts +3 -2
- package/dist/types/context.d.ts +7 -0
- package/dist/types/index.d.ts +7 -2
- package/dist/types/theme-provider/context/color-mode.d.ts +10 -0
- package/dist/types/theme-provider/context/inside-theme-provider.d.ts +4 -0
- package/dist/types/theme-provider/context/theme.d.ts +10 -0
- package/dist/types/theme-provider/hooks/use-color-mode-for-migration.d.ts +9 -0
- package/dist/types/theme-provider/hooks/use-color-mode.d.ts +7 -0
- package/dist/types/theme-provider/hooks/use-is-inside-theme-provider.d.ts +6 -0
- package/dist/types/theme-provider/hooks/use-set-color-mode.d.ts +7 -0
- package/dist/types/theme-provider/hooks/use-set-theme.d.ts +7 -0
- package/dist/types/theme-provider/hooks/use-theme.d.ts +7 -0
- package/dist/types/theme-provider/index.d.ts +19 -0
- package/dist/types/theme-provider/utils/is-theme-mounted.d.ts +8 -0
- package/dist/types/theme-provider/utils/load-and-mount-themes.d.ts +2 -0
- package/dist/types-ts4.5/app-provider.d.ts +3 -2
- package/dist/types-ts4.5/context.d.ts +7 -0
- package/dist/types-ts4.5/index.d.ts +7 -2
- package/dist/types-ts4.5/theme-provider/context/color-mode.d.ts +10 -0
- package/dist/types-ts4.5/theme-provider/context/inside-theme-provider.d.ts +4 -0
- package/dist/types-ts4.5/theme-provider/context/theme.d.ts +10 -0
- package/dist/types-ts4.5/theme-provider/hooks/use-color-mode-for-migration.d.ts +9 -0
- package/dist/types-ts4.5/theme-provider/hooks/use-color-mode.d.ts +7 -0
- package/dist/types-ts4.5/theme-provider/hooks/use-is-inside-theme-provider.d.ts +6 -0
- package/dist/types-ts4.5/theme-provider/hooks/use-set-color-mode.d.ts +7 -0
- package/dist/types-ts4.5/theme-provider/hooks/use-set-theme.d.ts +7 -0
- package/dist/types-ts4.5/theme-provider/hooks/use-theme.d.ts +7 -0
- package/dist/types-ts4.5/theme-provider/index.d.ts +19 -0
- package/dist/types-ts4.5/theme-provider/utils/is-theme-mounted.d.ts +8 -0
- package/dist/types-ts4.5/theme-provider/utils/load-and-mount-themes.d.ts +2 -0
- package/package.json +12 -7
- package/dist/cjs/theme-provider.js +0 -286
- package/dist/es2019/theme-provider.js +0 -216
- package/dist/esm/theme-provider.js +0 -272
- package/dist/types/theme-provider.d.ts +0 -55
- package/dist/types-ts4.5/theme-provider.d.ts +0 -55
- /package/dist/cjs/{theme-provider.compiled.css → theme-provider/index.compiled.css} +0 -0
- /package/dist/es2019/{theme-provider.compiled.css → theme-provider/index.compiled.css} +0 -0
- /package/dist/esm/{theme-provider.compiled.css → theme-provider/index.compiled.css} +0 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import { useContext, useEffect, useState } from 'react';
|
|
3
|
+
import { getGlobalTheme, ThemeMutationObserver } from '@atlaskit/tokens';
|
|
4
|
+
import { ColorModeContext } from '../context/color-mode';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* __useColorMode()__
|
|
8
|
+
*
|
|
9
|
+
* Returns the current color mode when inside the app provider.
|
|
10
|
+
*/
|
|
11
|
+
export function useColorMode() {
|
|
12
|
+
var value = useContext(ColorModeContext);
|
|
13
|
+
|
|
14
|
+
// TODO: This will only return 'light' or 'dark' but never 'auto', which in some cases
|
|
15
|
+
// may be desirable. We should consider returning both the reconciled color mode (e.g. 'light' or 'dark') and the selected color mode ("auto").
|
|
16
|
+
var theme = getGlobalTheme();
|
|
17
|
+
var _useState = useState((theme === null || theme === void 0 ? void 0 : theme.colorMode) || 'light'),
|
|
18
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
19
|
+
resolvedColorMode = _useState2[0],
|
|
20
|
+
setResolvedColorMode = _useState2[1];
|
|
21
|
+
useEffect(function () {
|
|
22
|
+
// We are using theme from context so no need to reference the DOM
|
|
23
|
+
if (value) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
var observer = new ThemeMutationObserver(function (theme) {
|
|
27
|
+
setResolvedColorMode((theme === null || theme === void 0 ? void 0 : theme.colorMode) || 'light');
|
|
28
|
+
});
|
|
29
|
+
observer.observe();
|
|
30
|
+
return function () {
|
|
31
|
+
return observer.disconnect();
|
|
32
|
+
};
|
|
33
|
+
}, [value]);
|
|
34
|
+
return value ? value : resolvedColorMode;
|
|
35
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { InsideThemeProviderContext } from '../context/inside-theme-provider';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* __useIsInsideThemeProvider()__
|
|
6
|
+
*
|
|
7
|
+
* Returns true if the current component is inside a ThemeProvider.
|
|
8
|
+
*/
|
|
9
|
+
export var useIsInsideThemeProvider = function useIsInsideThemeProvider() {
|
|
10
|
+
var _useContext;
|
|
11
|
+
return (_useContext = useContext(InsideThemeProviderContext)) !== null && _useContext !== void 0 ? _useContext : false;
|
|
12
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { SetColorModeContext } from '../context/color-mode';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* __useSetColorMode()__
|
|
6
|
+
*
|
|
7
|
+
* Returns the color mode setter when inside the app provider.
|
|
8
|
+
*/
|
|
9
|
+
export function useSetColorMode() {
|
|
10
|
+
var value = useContext(SetColorModeContext);
|
|
11
|
+
if (!value) {
|
|
12
|
+
throw new Error('useSetColorMode must be used within ThemeProvider.');
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { SetThemeContext } from '../context/theme';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* __useSetTheme()__
|
|
6
|
+
*
|
|
7
|
+
* Returns the theme setter when inside the app provider.
|
|
8
|
+
*/
|
|
9
|
+
export function useSetTheme() {
|
|
10
|
+
var value = useContext(SetThemeContext);
|
|
11
|
+
if (!value) {
|
|
12
|
+
throw new Error('useSetTheme must be used within ThemeProvider.');
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import { useContext, useEffect, useState } from 'react';
|
|
3
|
+
import { getGlobalTheme, ThemeMutationObserver } from '@atlaskit/tokens';
|
|
4
|
+
import { ThemeContext } from '../context/theme';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* __useTheme()__
|
|
8
|
+
*
|
|
9
|
+
* Returns the current theme settings when inside the app provider.
|
|
10
|
+
*/
|
|
11
|
+
export function useTheme() {
|
|
12
|
+
var theme = useContext(ThemeContext);
|
|
13
|
+
var _useState = useState(theme || getGlobalTheme()),
|
|
14
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
15
|
+
resolvedTheme = _useState2[0],
|
|
16
|
+
setResolvedTheme = _useState2[1];
|
|
17
|
+
useEffect(function () {
|
|
18
|
+
// We are using theme from context so no need to reference the DOM
|
|
19
|
+
if (theme) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
var observer = new ThemeMutationObserver(setResolvedTheme);
|
|
23
|
+
observer.observe();
|
|
24
|
+
return function () {
|
|
25
|
+
return observer.disconnect();
|
|
26
|
+
};
|
|
27
|
+
}, [theme]);
|
|
28
|
+
return theme ? theme : resolvedTheme;
|
|
29
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/* index.tsx generated by @compiled/babel-plugin v0.38.1 */
|
|
2
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
3
|
+
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
4
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
5
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
6
|
+
import "./index.compiled.css";
|
|
7
|
+
import { ax, ix } from "@compiled/react/runtime";
|
|
8
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
9
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
10
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
11
|
+
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
12
|
+
import { bind } from 'bind-event-listener';
|
|
13
|
+
import { fg } from '@atlaskit/platform-feature-flags';
|
|
14
|
+
import { getThemeHtmlAttrs, setGlobalTheme, SUBTREE_THEME_ATTRIBUTE, themeStateDefaults } from '@atlaskit/tokens';
|
|
15
|
+
import { useIsInsideAppProvider } from '../context';
|
|
16
|
+
import { ColorModeContext, SetColorModeContext } from './context/color-mode';
|
|
17
|
+
import { InsideThemeProviderContext } from './context/inside-theme-provider';
|
|
18
|
+
import { SetThemeContext, ThemeContext } from './context/theme';
|
|
19
|
+
import { useIsInsideThemeProvider } from './hooks/use-is-inside-theme-provider';
|
|
20
|
+
import { loadAndMountThemes } from './utils/load-and-mount-themes';
|
|
21
|
+
var defaultThemeSettings = {
|
|
22
|
+
dark: 'dark',
|
|
23
|
+
light: 'light',
|
|
24
|
+
spacing: 'spacing',
|
|
25
|
+
typography: 'typography'
|
|
26
|
+
};
|
|
27
|
+
var isMatchMediaAvailable = typeof window !== 'undefined' && 'matchMedia' in window;
|
|
28
|
+
var prefersDarkModeMql = isMatchMediaAvailable ? window.matchMedia('(prefers-color-scheme: dark)') : undefined;
|
|
29
|
+
|
|
30
|
+
// TODO: currently 'auto' color mode will always return 'light' in SSR.
|
|
31
|
+
// Additional work required: https://product-fabric.atlassian.net/browse/DSP-9781
|
|
32
|
+
function getReconciledColorMode(colorMode) {
|
|
33
|
+
if (colorMode === 'auto') {
|
|
34
|
+
return prefersDarkModeMql !== null && prefersDarkModeMql !== void 0 && prefersDarkModeMql.matches ? 'dark' : 'light';
|
|
35
|
+
}
|
|
36
|
+
return colorMode;
|
|
37
|
+
}
|
|
38
|
+
var contentStyles = {
|
|
39
|
+
body: "_1e0c1bgi"
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* __Theme provider__
|
|
43
|
+
*
|
|
44
|
+
* Provides global theming configuration.
|
|
45
|
+
*/
|
|
46
|
+
function ThemeProvider(_ref) {
|
|
47
|
+
var children = _ref.children,
|
|
48
|
+
_ref$defaultColorMode = _ref.defaultColorMode,
|
|
49
|
+
defaultColorMode = _ref$defaultColorMode === void 0 ? 'auto' : _ref$defaultColorMode,
|
|
50
|
+
defaultTheme = _ref.defaultTheme;
|
|
51
|
+
var _useState = useState(defaultColorMode),
|
|
52
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
53
|
+
chosenColorMode = _useState2[0],
|
|
54
|
+
setChosenColorMode = _useState2[1];
|
|
55
|
+
var _useState3 = useState(getReconciledColorMode(defaultColorMode)),
|
|
56
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
57
|
+
reconciledColorMode = _useState4[0],
|
|
58
|
+
setReconciledColorMode = _useState4[1];
|
|
59
|
+
var _useState5 = useState(function () {
|
|
60
|
+
return _objectSpread(_objectSpread({}, defaultThemeSettings), defaultTheme);
|
|
61
|
+
}),
|
|
62
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
63
|
+
theme = _useState6[0],
|
|
64
|
+
setTheme = _useState6[1];
|
|
65
|
+
var setColorMode = useCallback(function (colorMode) {
|
|
66
|
+
setChosenColorMode(colorMode);
|
|
67
|
+
setReconciledColorMode(getReconciledColorMode(colorMode));
|
|
68
|
+
}, []);
|
|
69
|
+
var setPartialTheme = useCallback(function (nextTheme) {
|
|
70
|
+
setTheme(function (theme) {
|
|
71
|
+
return _objectSpread(_objectSpread({}, theme), nextTheme);
|
|
72
|
+
});
|
|
73
|
+
}, []);
|
|
74
|
+
var lastSetGlobalThemePromiseRef = useRef(null);
|
|
75
|
+
var isInsideAppProvider = useIsInsideAppProvider();
|
|
76
|
+
var isInsideThemeProvider = useIsInsideThemeProvider();
|
|
77
|
+
var isRootThemeProvider = isInsideAppProvider && !isInsideThemeProvider;
|
|
78
|
+
useEffect(function () {
|
|
79
|
+
if (
|
|
80
|
+
/**
|
|
81
|
+
* A top-level ThemeProvider is detected by being the first ThemeProvider inside an AppProvider.
|
|
82
|
+
*
|
|
83
|
+
* This will not use sub-tree theming but instead set the global theme state using the
|
|
84
|
+
* `@atlaskit/tokens` APIs, as it's required for styling root `html` and `body` elements
|
|
85
|
+
* for compatibility with `@atlaskit/css-reset`.
|
|
86
|
+
*
|
|
87
|
+
* In the future we could consider moving away from DOM mutations and require AppProvider to wrap
|
|
88
|
+
* `html` in order to apply global theme state, which would allow a more consistent approach to
|
|
89
|
+
* theme loading.
|
|
90
|
+
*/
|
|
91
|
+
isRootThemeProvider || (
|
|
92
|
+
/**
|
|
93
|
+
* Or when not behind feature flag, partially revert to legacy behavior.
|
|
94
|
+
* This only affects theme providers that are not inside an AppProvider or a ThemeProvider,
|
|
95
|
+
* as we still need to set global theme state to prevent breaking existing apps,
|
|
96
|
+
* but also prevent multiple theme providers from loading conflicting theme states.
|
|
97
|
+
*
|
|
98
|
+
* After we roll out the feature flag, this will be removed as we will
|
|
99
|
+
* only support sub-tree theming when used outside of AppProvider.
|
|
100
|
+
*/
|
|
101
|
+
!isInsideAppProvider && !isInsideThemeProvider && !fg('platform_dst_subtree_theming'))) {
|
|
102
|
+
/**
|
|
103
|
+
* We need to wait for any previous `setGlobalTheme` calls to finish before calling it again.
|
|
104
|
+
* This is to prevent race conditions as `setGlobalTheme` is async and mutates the DOM (e.g. sets the
|
|
105
|
+
* `data-color-mode` attribute on the root element).
|
|
106
|
+
*
|
|
107
|
+
* Since we can't safely abort the `setGlobalTheme` execution, we need to wait for it to properly finish before
|
|
108
|
+
* applying the new theme.
|
|
109
|
+
*
|
|
110
|
+
* Without this, we can end up in the following scenario:
|
|
111
|
+
* 1. app loads with the default 'light' theme, kicking off `setGlobalTheme`
|
|
112
|
+
* 2. app switches to 'dark' theme after retrieving value persisted in local storage, calling `setGlobalTheme` again
|
|
113
|
+
* 3. `setGlobalTheme` function execution for `dark` finishes before the initial `light` execution
|
|
114
|
+
* 4. `setGlobalTheme` function execution for `light` then finishes, resulting in the 'light' theme being applied.
|
|
115
|
+
*/
|
|
116
|
+
var cleanupLastFnCall = /*#__PURE__*/function () {
|
|
117
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
118
|
+
var unbindFn;
|
|
119
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
120
|
+
while (1) switch (_context.prev = _context.next) {
|
|
121
|
+
case 0:
|
|
122
|
+
if (!lastSetGlobalThemePromiseRef.current) {
|
|
123
|
+
_context.next = 6;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
_context.next = 3;
|
|
127
|
+
return lastSetGlobalThemePromiseRef.current;
|
|
128
|
+
case 3:
|
|
129
|
+
unbindFn = _context.sent;
|
|
130
|
+
unbindFn();
|
|
131
|
+
lastSetGlobalThemePromiseRef.current = null;
|
|
132
|
+
case 6:
|
|
133
|
+
case "end":
|
|
134
|
+
return _context.stop();
|
|
135
|
+
}
|
|
136
|
+
}, _callee);
|
|
137
|
+
}));
|
|
138
|
+
return function cleanupLastFnCall() {
|
|
139
|
+
return _ref2.apply(this, arguments);
|
|
140
|
+
};
|
|
141
|
+
}();
|
|
142
|
+
var safelySetGlobalTheme = /*#__PURE__*/function () {
|
|
143
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
|
|
144
|
+
var promise;
|
|
145
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
146
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
147
|
+
case 0:
|
|
148
|
+
_context2.next = 2;
|
|
149
|
+
return cleanupLastFnCall();
|
|
150
|
+
case 2:
|
|
151
|
+
promise = setGlobalTheme(_objectSpread(_objectSpread({}, theme), {}, {
|
|
152
|
+
colorMode: reconciledColorMode
|
|
153
|
+
}));
|
|
154
|
+
lastSetGlobalThemePromiseRef.current = promise;
|
|
155
|
+
case 4:
|
|
156
|
+
case "end":
|
|
157
|
+
return _context2.stop();
|
|
158
|
+
}
|
|
159
|
+
}, _callee2);
|
|
160
|
+
}));
|
|
161
|
+
return function safelySetGlobalTheme() {
|
|
162
|
+
return _ref3.apply(this, arguments);
|
|
163
|
+
};
|
|
164
|
+
}();
|
|
165
|
+
safelySetGlobalTheme();
|
|
166
|
+
return function cleanup() {
|
|
167
|
+
cleanupLastFnCall();
|
|
168
|
+
};
|
|
169
|
+
} else {
|
|
170
|
+
// For other theme providers (whether outside AppProvider or nested inside a ThemeProvider),
|
|
171
|
+
// we treat them as sub-tree themes that do not load global theme state.
|
|
172
|
+
var themeState = _objectSpread(_objectSpread({}, theme), {}, {
|
|
173
|
+
colorMode: reconciledColorMode,
|
|
174
|
+
contrastMode: themeStateDefaults.contrastMode,
|
|
175
|
+
shape: themeStateDefaults.shape(),
|
|
176
|
+
typography: themeStateDefaults.typography()
|
|
177
|
+
});
|
|
178
|
+
loadAndMountThemes(themeState);
|
|
179
|
+
}
|
|
180
|
+
}, [isInsideAppProvider, isInsideThemeProvider, isRootThemeProvider, reconciledColorMode, theme]);
|
|
181
|
+
useEffect(function () {
|
|
182
|
+
if (!prefersDarkModeMql) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
var unbindListener = bind(prefersDarkModeMql, {
|
|
186
|
+
type: 'change',
|
|
187
|
+
listener: function listener(event) {
|
|
188
|
+
if (chosenColorMode === 'auto') {
|
|
189
|
+
setReconciledColorMode(event.matches ? 'dark' : 'light');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
return unbindListener;
|
|
194
|
+
}, [chosenColorMode]);
|
|
195
|
+
var attrs = _objectSpread(_objectSpread({}, getThemeHtmlAttrs(_objectSpread(_objectSpread({}, theme), {}, {
|
|
196
|
+
colorMode: reconciledColorMode
|
|
197
|
+
}))), {}, _defineProperty({}, SUBTREE_THEME_ATTRIBUTE, true));
|
|
198
|
+
return /*#__PURE__*/React.createElement(InsideThemeProviderContext.Provider, {
|
|
199
|
+
value: true
|
|
200
|
+
}, /*#__PURE__*/React.createElement(ColorModeContext.Provider, {
|
|
201
|
+
value: reconciledColorMode
|
|
202
|
+
}, /*#__PURE__*/React.createElement(SetColorModeContext.Provider, {
|
|
203
|
+
value: setColorMode
|
|
204
|
+
}, /*#__PURE__*/React.createElement(ThemeContext.Provider, {
|
|
205
|
+
value: theme
|
|
206
|
+
}, /*#__PURE__*/React.createElement(SetThemeContext.Provider, {
|
|
207
|
+
value: setPartialTheme
|
|
208
|
+
}, fg('platform_dst_subtree_theming') ? /*#__PURE__*/React.createElement("div", _extends({}, attrs, {
|
|
209
|
+
className: ax([contentStyles.body])
|
|
210
|
+
}), children) : children)))));
|
|
211
|
+
}
|
|
212
|
+
export default ThemeProvider;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { getDocument } from '@atlaskit/browser-apis';
|
|
2
|
+
import { THEME_DATA_ATTRIBUTE } from '@atlaskit/tokens';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a theme is mounted in the document head.
|
|
6
|
+
*
|
|
7
|
+
* Eventually this won't be necessary as we'll utilise AppProvider context
|
|
8
|
+
* to track theme loading.
|
|
9
|
+
*/
|
|
10
|
+
export function isThemeMounted(themeId) {
|
|
11
|
+
var doc = getDocument();
|
|
12
|
+
if (!doc) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return doc.head.querySelector("style[".concat(THEME_DATA_ATTRIBUTE, "=\"").concat(themeId, "\"]"));
|
|
16
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
2
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
3
|
+
import { getDocument } from '@atlaskit/browser-apis';
|
|
4
|
+
import { themeImportMap } from '@atlaskit/tokens';
|
|
5
|
+
import { isThemeMounted } from './is-theme-mounted';
|
|
6
|
+
var loadThemeCss = /*#__PURE__*/function () {
|
|
7
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(themeId) {
|
|
8
|
+
var _yield$themeImportMap, themeCss;
|
|
9
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
10
|
+
while (1) switch (_context.prev = _context.next) {
|
|
11
|
+
case 0:
|
|
12
|
+
if (!isThemeMounted(themeId)) {
|
|
13
|
+
_context.next = 2;
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
return _context.abrupt("return");
|
|
17
|
+
case 2:
|
|
18
|
+
if (themeId) {
|
|
19
|
+
_context.next = 4;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
return _context.abrupt("return");
|
|
23
|
+
case 4:
|
|
24
|
+
_context.next = 6;
|
|
25
|
+
return themeImportMap[themeId]();
|
|
26
|
+
case 6:
|
|
27
|
+
_yield$themeImportMap = _context.sent;
|
|
28
|
+
themeCss = _yield$themeImportMap.default;
|
|
29
|
+
return _context.abrupt("return", themeCss);
|
|
30
|
+
case 9:
|
|
31
|
+
case "end":
|
|
32
|
+
return _context.stop();
|
|
33
|
+
}
|
|
34
|
+
}, _callee);
|
|
35
|
+
}));
|
|
36
|
+
return function loadThemeCss(_x) {
|
|
37
|
+
return _ref.apply(this, arguments);
|
|
38
|
+
};
|
|
39
|
+
}();
|
|
40
|
+
var mountThemeCss = /*#__PURE__*/function () {
|
|
41
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(css, themeId) {
|
|
42
|
+
var doc, style;
|
|
43
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
44
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
45
|
+
case 0:
|
|
46
|
+
// SSR-safe: Only mount on client side
|
|
47
|
+
doc = getDocument();
|
|
48
|
+
if (doc) {
|
|
49
|
+
_context2.next = 3;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
return _context2.abrupt("return");
|
|
53
|
+
case 3:
|
|
54
|
+
style = doc.createElement('style');
|
|
55
|
+
style.textContent = css;
|
|
56
|
+
style.dataset.theme = themeId;
|
|
57
|
+
|
|
58
|
+
// Prevent duplicate mounting of themes.
|
|
59
|
+
// It's possible the theme was already being loaded elsewhere.
|
|
60
|
+
if (!isThemeMounted(themeId)) {
|
|
61
|
+
_context2.next = 8;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
return _context2.abrupt("return");
|
|
65
|
+
case 8:
|
|
66
|
+
doc.head.appendChild(style);
|
|
67
|
+
case 9:
|
|
68
|
+
case "end":
|
|
69
|
+
return _context2.stop();
|
|
70
|
+
}
|
|
71
|
+
}, _callee2);
|
|
72
|
+
}));
|
|
73
|
+
return function mountThemeCss(_x2, _x3) {
|
|
74
|
+
return _ref2.apply(this, arguments);
|
|
75
|
+
};
|
|
76
|
+
}();
|
|
77
|
+
var loadAndMountThemeCss = /*#__PURE__*/function () {
|
|
78
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(themeId) {
|
|
79
|
+
var themeCss;
|
|
80
|
+
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
81
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
82
|
+
case 0:
|
|
83
|
+
_context3.next = 2;
|
|
84
|
+
return loadThemeCss(themeId);
|
|
85
|
+
case 2:
|
|
86
|
+
themeCss = _context3.sent;
|
|
87
|
+
if (themeCss) {
|
|
88
|
+
_context3.next = 5;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
return _context3.abrupt("return");
|
|
92
|
+
case 5:
|
|
93
|
+
mountThemeCss(themeCss, themeId);
|
|
94
|
+
case 6:
|
|
95
|
+
case "end":
|
|
96
|
+
return _context3.stop();
|
|
97
|
+
}
|
|
98
|
+
}, _callee3);
|
|
99
|
+
}));
|
|
100
|
+
return function loadAndMountThemeCss(_x4) {
|
|
101
|
+
return _ref3.apply(this, arguments);
|
|
102
|
+
};
|
|
103
|
+
}();
|
|
104
|
+
export var loadAndMountThemes = /*#__PURE__*/function () {
|
|
105
|
+
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(themeState) {
|
|
106
|
+
var themesToLoad;
|
|
107
|
+
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
|
|
108
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
109
|
+
case 0:
|
|
110
|
+
themesToLoad = [themeState.light, themeState.dark, themeState.spacing, themeState.shape, themeState.typography].filter(function (themeId) {
|
|
111
|
+
return !!themeId;
|
|
112
|
+
});
|
|
113
|
+
themesToLoad.forEach(loadAndMountThemeCss);
|
|
114
|
+
case 2:
|
|
115
|
+
case "end":
|
|
116
|
+
return _context4.stop();
|
|
117
|
+
}
|
|
118
|
+
}, _callee4);
|
|
119
|
+
}));
|
|
120
|
+
return function loadAndMountThemes(_x5) {
|
|
121
|
+
return _ref4.apply(this, arguments);
|
|
122
|
+
};
|
|
123
|
+
}();
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import type { ThemeColorModes } from '@atlaskit/tokens';
|
|
2
3
|
import { type RouterLinkComponent } from './router-link-provider';
|
|
3
|
-
import { type
|
|
4
|
+
import { type Theme } from './theme-provider/context/theme';
|
|
4
5
|
interface AppProviderProps {
|
|
5
6
|
/**
|
|
6
7
|
* Initial color mode.
|
|
7
8
|
*/
|
|
8
|
-
defaultColorMode?:
|
|
9
|
+
defaultColorMode?: ThemeColorModes;
|
|
9
10
|
/**
|
|
10
11
|
* Theme settings.
|
|
11
12
|
*/
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* __Inside app provider context__
|
|
3
|
+
*
|
|
4
|
+
* A context that indicates if the current component is inside an AppProvider.
|
|
5
|
+
*/
|
|
6
|
+
export declare const InsideAppProviderContext: import("react").Context<boolean>;
|
|
7
|
+
export declare const useIsInsideAppProvider: () => boolean;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
export { default } from './app-provider';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
2
|
+
export { UNSAFE_useColorModeForMigration } from './theme-provider/hooks/use-color-mode-for-migration';
|
|
3
|
+
export { useColorMode } from './theme-provider/hooks/use-color-mode';
|
|
4
|
+
export { useSetColorMode } from './theme-provider/hooks/use-set-color-mode';
|
|
5
|
+
export { useSetTheme } from './theme-provider/hooks/use-set-theme';
|
|
6
|
+
export { useTheme } from './theme-provider/hooks/use-theme';
|
|
7
|
+
export type { Theme } from './theme-provider/context/theme';
|
|
8
|
+
export { default as ThemeProvider, type ThemeProviderProps } from './theme-provider';
|
|
4
9
|
export { type RouterLinkComponent, type RouterLinkComponentProps } from './router-link-provider';
|
|
5
10
|
import useRouterLink from './router-link-provider/hooks/use-router-link';
|
|
6
11
|
export { useRouterLink };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ThemeColorModes } from '@atlaskit/tokens';
|
|
2
|
+
export type ReconciledColorMode = Exclude<ThemeColorModes, 'auto'>;
|
|
3
|
+
/**
|
|
4
|
+
* __Color mode context__
|
|
5
|
+
*/
|
|
6
|
+
export declare const ColorModeContext: import("react").Context<ReconciledColorMode | undefined>;
|
|
7
|
+
/**
|
|
8
|
+
* __Set color mode context__
|
|
9
|
+
*/
|
|
10
|
+
export declare const SetColorModeContext: import("react").Context<((value: ThemeColorModes) => void) | undefined>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ThemeState } from '@atlaskit/tokens';
|
|
2
|
+
export type Theme = Omit<ThemeState, 'colorMode' | 'contrastMode'>;
|
|
3
|
+
/**
|
|
4
|
+
* __Theme context__
|
|
5
|
+
*/
|
|
6
|
+
export declare const ThemeContext: import("react").Context<Theme | undefined>;
|
|
7
|
+
/**
|
|
8
|
+
* __Set theme context__
|
|
9
|
+
*/
|
|
10
|
+
export declare const SetThemeContext: import("react").Context<((value: Partial<Theme>) => void) | undefined>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ReconciledColorMode } from '../context/color-mode';
|
|
2
|
+
/**
|
|
3
|
+
* __UNSAFE_useColorModeForMigration()__
|
|
4
|
+
*
|
|
5
|
+
* Returns the current color mode when inside the app provider.
|
|
6
|
+
* Unlike useColorMode, this utility returns undefined, instead of throwing an error, when the app provider is missing.
|
|
7
|
+
* This allows it to be used by components that need to operate with and without an app provider.
|
|
8
|
+
*/
|
|
9
|
+
export declare function UNSAFE_useColorModeForMigration(): ReconciledColorMode | undefined;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jsxRuntime classic
|
|
3
|
+
* @jsx jsx
|
|
4
|
+
*/
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import { type ThemeColorModes } from '@atlaskit/tokens';
|
|
7
|
+
import { type Theme } from './context/theme';
|
|
8
|
+
export interface ThemeProviderProps {
|
|
9
|
+
defaultColorMode?: ThemeColorModes;
|
|
10
|
+
defaultTheme?: Partial<Theme>;
|
|
11
|
+
children: React.ReactNode;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* __Theme provider__
|
|
15
|
+
*
|
|
16
|
+
* Provides global theming configuration.
|
|
17
|
+
*/
|
|
18
|
+
declare function ThemeProvider({ children, defaultColorMode, defaultTheme }: ThemeProviderProps): JSX.Element;
|
|
19
|
+
export default ThemeProvider;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ThemeIds } from '@atlaskit/tokens';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a theme is mounted in the document head.
|
|
4
|
+
*
|
|
5
|
+
* Eventually this won't be necessary as we'll utilise AppProvider context
|
|
6
|
+
* to track theme loading.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isThemeMounted(themeId: ThemeIds): false | Element | null;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import type { ThemeColorModes } from '@atlaskit/tokens';
|
|
2
3
|
import { type RouterLinkComponent } from './router-link-provider';
|
|
3
|
-
import { type
|
|
4
|
+
import { type Theme } from './theme-provider/context/theme';
|
|
4
5
|
interface AppProviderProps {
|
|
5
6
|
/**
|
|
6
7
|
* Initial color mode.
|
|
7
8
|
*/
|
|
8
|
-
defaultColorMode?:
|
|
9
|
+
defaultColorMode?: ThemeColorModes;
|
|
9
10
|
/**
|
|
10
11
|
* Theme settings.
|
|
11
12
|
*/
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* __Inside app provider context__
|
|
3
|
+
*
|
|
4
|
+
* A context that indicates if the current component is inside an AppProvider.
|
|
5
|
+
*/
|
|
6
|
+
export declare const InsideAppProviderContext: import("react").Context<boolean>;
|
|
7
|
+
export declare const useIsInsideAppProvider: () => boolean;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
export { default } from './app-provider';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
2
|
+
export { UNSAFE_useColorModeForMigration } from './theme-provider/hooks/use-color-mode-for-migration';
|
|
3
|
+
export { useColorMode } from './theme-provider/hooks/use-color-mode';
|
|
4
|
+
export { useSetColorMode } from './theme-provider/hooks/use-set-color-mode';
|
|
5
|
+
export { useSetTheme } from './theme-provider/hooks/use-set-theme';
|
|
6
|
+
export { useTheme } from './theme-provider/hooks/use-theme';
|
|
7
|
+
export type { Theme } from './theme-provider/context/theme';
|
|
8
|
+
export { default as ThemeProvider, type ThemeProviderProps } from './theme-provider';
|
|
4
9
|
export { type RouterLinkComponent, type RouterLinkComponentProps } from './router-link-provider';
|
|
5
10
|
import useRouterLink from './router-link-provider/hooks/use-router-link';
|
|
6
11
|
export { useRouterLink };
|