UNPKG

@atlaskit/app-provider

Version:

A top level provider for the Design System.

60 lines (58 loc) • 3.34 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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 from 'react'; import { themeImportMap } from '@atlaskit/tokens/artifacts/theme-import-map'; import { getThemeOverridePreferences } from '@atlaskit/tokens/get-theme-override-preferences'; import { getThemePreferences } from '@atlaskit/tokens/get-theme-preferences'; /** * TODO: Remove this React 18 compatibility guard when React 19 has shipped everywhere. * At that point, use `import { use } from 'react'` directly. */ var themeCssPromises = new Map(); function getThemeCssPromise(themeId) { var themeCssPromise = themeCssPromises.get(themeId); if (!themeCssPromise) { themeCssPromise = themeImportMap[themeId]().then(function (_ref) { var css = _ref.default; return css; }); themeCssPromises.set(themeId, themeCssPromise); } return themeCssPromise; } function getInlineThemeCss(themeId) { var _document$body$queryS, _document$body$queryS2; var reactUse = React.use; var isServer = globalThis.__SERVER__ === true || typeof document === 'undefined'; if (isServer) { return reactUse === null || reactUse === void 0 ? void 0 : reactUse(getThemeCssPromise(themeId)); } return (_document$body$queryS = (_document$body$queryS2 = document.body.querySelector("style[data-theme=\"".concat(themeId, "\"]"))) === null || _document$body$queryS2 === void 0 ? void 0 : _document$body$queryS2.textContent) !== null && _document$body$queryS !== void 0 ? _document$body$queryS : undefined; } /** * Resolves CSS for the themes applied by a ThemeProvider. * * During React 19 server rendering, `use()` suspends the provider subtree until * each requested CSS chunk is available. The resulting styles are rendered * immediately before that subtree. React 18 does not provide `use()`, so it * safely follows the existing path without inline CSS. During hydration, the CSS * is read from server-rendered style elements so the initial client tree matches. */ export function getInlineThemeStyles(theme, colorMode) { var themeState = _objectSpread(_objectSpread({}, theme), {}, { colorMode: colorMode, contrastMode: 'no-preference' }); var themeIds = [].concat(_toConsumableArray(getThemePreferences(themeState)), _toConsumableArray(getThemeOverridePreferences(themeState))); return _toConsumableArray(new Set(themeIds)).map(function (themeId) { var css = getInlineThemeCss(themeId); return css ? { id: themeId, css: css } : undefined; }).filter(function (style) { return style !== undefined; }); }