@atlaskit/app-provider
Version:
A top level provider for the Design System.
298 lines (294 loc) • 14.4 kB
JavaScript
/* index.tsx generated by @compiled/babel-plugin v3.0.2 */
import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "./index.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import _regeneratorRuntime from "@babel/runtime/regenerator";
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; }
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; }
import React, { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
import { bind } from 'bind-event-listener';
import { fg } from '@atlaskit/platform-feature-flags/fg';
import { SUBTREE_THEME_ATTRIBUTE } from '@atlaskit/tokens/constants';
import { CUSTOM_THEME_SCOPE_ATTRIBUTE, getThemeAndOverrides } from '@atlaskit/tokens/custom-theme-overrides';
import { getThemeHtmlAttrs } from '@atlaskit/tokens/get-theme-html-attrs';
import { setGlobalTheme } from '@atlaskit/tokens/set-global-theme';
import { useIsAppProviderThemingEnabled } from '../use-is-app-provider-theming-enabled';
import { useIsInsideAppProvider } from '../use-is-inside-app-provider';
import { ColorModeContext } from './context/color-mode';
import { InsideThemeProviderContext } from './context/inside-theme-provider';
import { SetColorModeContext } from './context/set-color-mode-context';
import { SetThemeContext, ThemeContext } from './context/theme';
import { useIsInsideThemeProvider } from './hooks/use-is-inside-theme-provider';
import { getInlineThemeStyles } from './utils/get-inline-theme-styles';
import { loadAndMountThemes } from './utils/load-and-mount-themes';
var defaultThemeSettings = {
dark: 'dark',
light: 'light',
shape: 'shape',
spacing: 'spacing',
typography: 'typography'
};
var isMatchMediaAvailable = typeof window !== 'undefined' && 'matchMedia' in window;
var prefersDarkModeMql = isMatchMediaAvailable ? window.matchMedia('(prefers-color-scheme: dark)') : undefined;
// TODO: currently 'auto' color mode will always return 'light' in SSR.
// Additional work required: https://product-fabric.atlassian.net/browse/DSP-9781
function getReconciledColorMode(colorMode) {
if (colorMode === 'auto') {
return prefersDarkModeMql !== null && prefersDarkModeMql !== void 0 && prefersDarkModeMql.matches ? 'dark' : 'light';
}
return colorMode;
}
var contentStyles = {
body: "_1e0c1bgi"
};
/**
* __Theme provider__
*
* Provides global theming configuration.
*/
export function ThemeProvider(_ref) {
var children = _ref.children,
_ref$defaultColorMode = _ref.defaultColorMode,
defaultColorMode = _ref$defaultColorMode === void 0 ? 'auto' : _ref$defaultColorMode,
defaultTheme = _ref.defaultTheme;
var _useState = useState(defaultColorMode),
_useState2 = _slicedToArray(_useState, 2),
chosenColorMode = _useState2[0],
setChosenColorMode = _useState2[1];
var _useState3 = useState(getReconciledColorMode(defaultColorMode)),
_useState4 = _slicedToArray(_useState3, 2),
reconciledColorMode = _useState4[0],
setReconciledColorMode = _useState4[1];
var isInsideAppProvider = useIsInsideAppProvider();
var isAppProviderThemingEnabled = useIsAppProviderThemingEnabled();
var isInsideThemeProvider = useIsInsideThemeProvider();
/**
* A top-level ThemeProvider is detected by being the first ThemeProvider inside an AppProvider.
*
* This will not use sub-tree theming but instead set the global theme state using the
* `@atlaskit/tokens` APIs, as it's required for styling root `html` and `body` elements
* for compatibility with `@atlaskit/css-reset`.
*
* In the future we could consider moving away from DOM mutations and require AppProvider to wrap
* `html` in order to apply global theme state, which would allow a more consistent approach to
* theme loading.
*/
var isRootThemeProvider = isInsideAppProvider && !isInsideThemeProvider && isAppProviderThemingEnabled;
/**
* Each provider owns its own override stylesheet, so nested providers don't overwrite or remove
* each other's overrides. `useId` is stable between the server render and hydration.
*
* Subtree providers also scope their override selector to their own element. Otherwise every
* subtree using the same custom theme would match every provider's overrides, and the last
* stylesheet in the document would win. The root provider targets `html`, which subtrees
* inherit from.
*/
var customThemeOverridesStyleId = useId();
var _getThemeAndOverrides = getThemeAndOverrides(defaultTheme, isRootThemeProvider ? undefined : customThemeOverridesStyleId),
normalizedDefaultTheme = _getThemeAndOverrides.theme,
inlineStyles = _getThemeAndOverrides.inlineStyles;
var _useState5 = useState(function () {
return _objectSpread(_objectSpread({}, defaultThemeSettings), normalizedDefaultTheme);
}),
_useState6 = _slicedToArray(_useState5, 2),
theme = _useState6[0],
setTheme = _useState6[1];
var _useState7 = useState(false),
_useState8 = _slicedToArray(_useState7, 2),
hasHoistedInlineThemeStyles = _useState8[0],
setHasHoistedInlineThemeStyles = _useState8[1];
var setColorMode = useCallback(function (colorMode) {
setChosenColorMode(colorMode);
setReconciledColorMode(getReconciledColorMode(colorMode));
}, []);
var setPartialTheme = useCallback(function (nextTheme) {
setTheme(function (theme) {
return _objectSpread(_objectSpread({}, theme), nextTheme);
});
}, []);
var lastSetGlobalThemePromiseRef = useRef(null);
/**
* Custom theme overrides apply regardless of `platform-static-theme-loading`: that gate only
* controls how the theme stylesheets themselves are loaded.
*/
var inlineThemeStylesOverrides = inlineStyles;
useLayoutEffect(function () {
return function () {
var _document$head$queryS;
(_document$head$queryS = document.head.querySelector("style[data-theme-overrides=\"".concat(customThemeOverridesStyleId, "\"]"))) === null || _document$head$queryS === void 0 || _document$head$queryS.remove();
};
}, [customThemeOverridesStyleId]);
useLayoutEffect(function () {
var isStaticThemeLoadingEnabled = fg('platform-static-theme-loading');
if (isStaticThemeLoadingEnabled && !hasHoistedInlineThemeStyles) {
getInlineThemeStyles(theme, chosenColorMode).forEach(function (_ref2) {
var id = _ref2.id,
css = _ref2.css;
if (document.head.querySelector("style[data-theme=\"".concat(id, "\"]"))) {
return;
}
var style = document.createElement('style');
style.dataset.theme = id;
style.textContent = css;
document.head.appendChild(style);
});
}
var existingOverrideStyle = document.head.querySelector("style[data-theme-overrides=\"".concat(customThemeOverridesStyleId, "\"]"));
if (inlineThemeStylesOverrides) {
if (existingOverrideStyle) {
existingOverrideStyle.textContent = inlineThemeStylesOverrides;
} else {
var style = document.createElement('style');
style.dataset.themeOverrides = customThemeOverridesStyleId;
style.textContent = inlineThemeStylesOverrides;
document.head.appendChild(style);
}
} else {
existingOverrideStyle === null || existingOverrideStyle === void 0 || existingOverrideStyle.remove();
}
// Only re-render to drop the inline styles when there are any to drop.
if (!hasHoistedInlineThemeStyles && (isStaticThemeLoadingEnabled || inlineThemeStylesOverrides)) {
setHasHoistedInlineThemeStyles(true);
}
}, [chosenColorMode, customThemeOverridesStyleId, hasHoistedInlineThemeStyles, inlineThemeStylesOverrides, theme]);
useEffect(function () {
if (isRootThemeProvider) {
/**
* We need to wait for any previous `setGlobalTheme` calls to finish before calling it again.
* This is to prevent race conditions as `setGlobalTheme` is async and mutates the DOM (e.g. sets the
* `data-color-mode` attribute on the root element).
*
* Since we can't safely abort the `setGlobalTheme` execution, we need to wait for it to properly finish before
* applying the new theme.
*
* Without this, we can end up in the following scenario:
* 1. app loads with the default 'light' theme, kicking off `setGlobalTheme`
* 2. app switches to 'dark' theme after retrieving value persisted in local storage, calling `setGlobalTheme` again
* 3. `setGlobalTheme` function execution for `dark` finishes before the initial `light` execution
* 4. `setGlobalTheme` function execution for `light` then finishes, resulting in the 'light' theme being applied.
*/
var cleanupLastFnCall = /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var unbindFn;
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
if (!lastSetGlobalThemePromiseRef.current) {
_context.next = 2;
break;
}
_context.next = 1;
return lastSetGlobalThemePromiseRef.current;
case 1:
unbindFn = _context.sent;
unbindFn();
lastSetGlobalThemePromiseRef.current = null;
case 2:
case "end":
return _context.stop();
}
}, _callee);
}));
return function cleanupLastFnCall() {
return _ref3.apply(this, arguments);
};
}();
var safelySetGlobalTheme = /*#__PURE__*/function () {
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var promise;
return _regeneratorRuntime.wrap(function (_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 1;
return cleanupLastFnCall();
case 1:
promise = setGlobalTheme(_objectSpread(_objectSpread({}, theme), {}, {
colorMode: reconciledColorMode
}));
lastSetGlobalThemePromiseRef.current = promise;
case 2:
case "end":
return _context2.stop();
}
}, _callee2);
}));
return function safelySetGlobalTheme() {
return _ref4.apply(this, arguments);
};
}();
safelySetGlobalTheme();
return function cleanup() {
cleanupLastFnCall();
};
}
// For other theme providers (whether outside AppProvider or nested inside a ThemeProvider),
// we treat them as sub-tree themes that do not load global theme state.
loadAndMountThemes(theme);
}, [isInsideAppProvider, isInsideThemeProvider, isRootThemeProvider, reconciledColorMode, theme]);
useEffect(function () {
if (!prefersDarkModeMql) {
return;
}
var unbindListener = bind(prefersDarkModeMql, {
type: 'change',
listener: function listener(event) {
if (chosenColorMode === 'auto') {
setReconciledColorMode(event.matches ? 'dark' : 'light');
}
}
});
return unbindListener;
}, [chosenColorMode]);
var attrs = _objectSpread(_objectSpread({}, getThemeHtmlAttrs(_objectSpread(_objectSpread({}, theme), {}, {
colorMode: reconciledColorMode
}))), {}, _defineProperty({}, SUBTREE_THEME_ATTRIBUTE, true), inlineThemeStylesOverrides ? _defineProperty({}, CUSTOM_THEME_SCOPE_ATTRIBUTE, customThemeOverridesStyleId) : {});
var inlineThemeStyles = fg('platform-static-theme-loading') && !hasHoistedInlineThemeStyles ? getInlineThemeStyles(theme, chosenColorMode) : [];
return /*#__PURE__*/React.createElement(InsideThemeProviderContext.Provider, {
value: true
}, /*#__PURE__*/React.createElement(ColorModeContext.Provider, {
value: reconciledColorMode
}, /*#__PURE__*/React.createElement(SetColorModeContext.Provider, {
value: setColorMode
}, /*#__PURE__*/React.createElement(ThemeContext.Provider, {
value: theme
}, /*#__PURE__*/React.createElement(SetThemeContext.Provider, {
value: setPartialTheme
}, !isRootThemeProvider ? /*#__PURE__*/React.createElement("div", _extends({}, attrs, {
className: ax([contentStyles.body])
}), !hasHoistedInlineThemeStyles && inlineThemeStylesOverrides &&
/*#__PURE__*/
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-global-styles
React.createElement("style", {
"data-theme-overrides": customThemeOverridesStyleId
}, inlineThemeStylesOverrides), inlineThemeStyles.map(function (_ref6) {
var id = _ref6.id,
css = _ref6.css;
return (
/*#__PURE__*/
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-global-styles
React.createElement("style", {
"data-theme": id,
key: id
}, css)
);
}), children) : /*#__PURE__*/React.createElement(React.Fragment, null, !hasHoistedInlineThemeStyles && inlineThemeStylesOverrides &&
/*#__PURE__*/
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-global-styles
React.createElement("style", {
"data-theme-overrides": customThemeOverridesStyleId
}, inlineThemeStylesOverrides), inlineThemeStyles.map(function (_ref7) {
var id = _ref7.id,
css = _ref7.css;
return (
/*#__PURE__*/
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-global-styles
React.createElement("style", {
"data-theme": id,
key: id
}, css)
);
}), children))))));
}