UNPKG

@atlaskit/app-provider

Version:

A top level provider for the Design System.

47 lines (45 loc) 1.35 kB
import { getDocument } from '@atlaskit/browser-apis'; import { themeImportMap } from '@atlaskit/tokens/artifacts/theme-import-map'; import { isThemeMounted } from './is-theme-mounted'; const loadThemeCss = async themeId => { // Prevent duplicate loading of themes if (isThemeMounted(themeId)) { return; } // Gracefully handles falsy values like `null` and `undefined` when makers ignore // TS to avoid loading default themes. if (!themeId) { return; } const { default: themeCss } = await themeImportMap[themeId](); return themeCss; }; const mountThemeCss = async (css, themeId) => { // SSR-safe: Only mount on client side const doc = getDocument(); if (!doc) { return; } const 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)) { return; } doc.head.appendChild(style); }; const loadAndMountThemeCss = async themeId => { const themeCss = await loadThemeCss(themeId); if (!themeCss) { return; } mountThemeCss(themeCss, themeId); }; export const loadAndMountThemes = async theme => { const themesToLoad = Object.values(theme).filter(themeId => !!themeId); themesToLoad.forEach(loadAndMountThemeCss); };