@atlaskit/app-provider
Version:
A top level provider for the Design System.
123 lines (122 loc) • 4.07 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { getDocument } from '@atlaskit/browser-apis';
import { themeImportMap } from '@atlaskit/tokens/artifacts/theme-import-map';
import { isThemeMounted } from './is-theme-mounted';
var loadThemeCss = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(themeId) {
var _yield$themeImportMap, themeCss;
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
if (!isThemeMounted(themeId)) {
_context.next = 1;
break;
}
return _context.abrupt("return");
case 1:
if (themeId) {
_context.next = 2;
break;
}
return _context.abrupt("return");
case 2:
_context.next = 3;
return themeImportMap[themeId]();
case 3:
_yield$themeImportMap = _context.sent;
themeCss = _yield$themeImportMap.default;
return _context.abrupt("return", themeCss);
case 4:
case "end":
return _context.stop();
}
}, _callee);
}));
return function loadThemeCss(_x) {
return _ref.apply(this, arguments);
};
}();
var mountThemeCss = /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(css, themeId) {
var doc, style;
return _regeneratorRuntime.wrap(function (_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
// SSR-safe: Only mount on client side
doc = getDocument();
if (doc) {
_context2.next = 1;
break;
}
return _context2.abrupt("return");
case 1:
style = doc.createElement('style');
style.textContent = css;
style.dataset.theme = themeId;
// Prevent duplicate mounting of themes.
// It's possible the theme was already being loaded elsewhere.
if (!isThemeMounted(themeId)) {
_context2.next = 2;
break;
}
return _context2.abrupt("return");
case 2:
doc.head.appendChild(style);
case 3:
case "end":
return _context2.stop();
}
}, _callee2);
}));
return function mountThemeCss(_x2, _x3) {
return _ref2.apply(this, arguments);
};
}();
var loadAndMountThemeCss = /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(themeId) {
var themeCss;
return _regeneratorRuntime.wrap(function (_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 1;
return loadThemeCss(themeId);
case 1:
themeCss = _context3.sent;
if (themeCss) {
_context3.next = 2;
break;
}
return _context3.abrupt("return");
case 2:
mountThemeCss(themeCss, themeId);
case 3:
case "end":
return _context3.stop();
}
}, _callee3);
}));
return function loadAndMountThemeCss(_x4) {
return _ref3.apply(this, arguments);
};
}();
export var loadAndMountThemes = /*#__PURE__*/function () {
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(theme) {
var themesToLoad;
return _regeneratorRuntime.wrap(function (_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
themesToLoad = Object.values(theme).filter(function (themeId) {
return !!themeId;
});
themesToLoad.forEach(loadAndMountThemeCss);
case 1:
case "end":
return _context4.stop();
}
}, _callee4);
}));
return function loadAndMountThemes(_x5) {
return _ref4.apply(this, arguments);
};
}();