|
1 |
| -import { getTheme } from "../Configuration"; |
2 |
| -import { attachThemeChange, getEffectiveStyle } from "../Theming"; |
3 |
| - |
4 |
| -class StyleInjection { |
5 |
| - constructor() { |
6 |
| - this.tagNamesInHead = []; |
7 |
| - this.tagsToStyleUrls = new Map(); |
8 |
| - attachThemeChange(this.updateStylesInHead.bind(this)); |
9 |
| - } |
| 1 | +import createStyleInHead from "../util/createStyleInHead"; |
10 | 2 |
|
11 |
| - createStyleTag(tagName, styleUrls, cssText) { |
12 |
| - if (this.tagNamesInHead.indexOf(tagName) !== -1) { |
13 |
| - return; |
14 |
| - } |
| 3 | +const injectedForTags = []; |
15 | 4 |
|
16 |
| - const style = document.createElement("style"); |
17 |
| - style.type = "text/css"; |
18 |
| - style.setAttribute("data-sap-source", tagName); |
19 |
| - style.innerHTML = cssText; |
20 |
| - document.head.appendChild(style); |
| 5 | +/** |
| 6 | + * Creates/updates a style element holding all CSS Custom Properties |
| 7 | + * @param cssText |
| 8 | + */ |
| 9 | +const injectThemeProperties = cssText => { |
| 10 | + // Needed for all browsers |
| 11 | + let styleElement = document.head.querySelector(`style[ui5-webcomponents-theme-properties]`); |
| 12 | + if (styleElement) { |
| 13 | + styleElement.textContent = cssText || ""; // in case of undefined |
| 14 | + } else { |
| 15 | + styleElement = createStyleInHead(cssText, { "ui5-webcomponents-theme-properties": "" }); |
| 16 | + } |
21 | 17 |
|
22 |
| - this.tagNamesInHead.push(tagName); |
23 |
| - this.tagsToStyleUrls.set(tagName, styleUrls); |
| 18 | + // IE only |
| 19 | + if (window.CSSVarsSimulation) { |
| 20 | + window.CSSVarsSimulation.findCSSVars(cssText); |
24 | 21 | }
|
| 22 | +}; |
25 | 23 |
|
26 |
| - async updateStylesInHead() { |
27 |
| - if (!window.ShadyDOM) { |
28 |
| - return; |
29 |
| - } |
| 24 | +/** |
| 25 | + * Creates a style element holding the CSS for a web component (and resolves CSS Custom Properties for IE) |
| 26 | + * @param tagName |
| 27 | + * @param cssText |
| 28 | + */ |
| 29 | +const injectWebComponentStyle = (tagName, cssText) => { |
| 30 | + if (!window.ShadyDOM) { |
| 31 | + return; |
| 32 | + } |
30 | 33 |
|
31 |
| - const theme = getTheme(); |
32 |
| - this.tagNamesInHead.forEach(async tagName => { |
33 |
| - const styleUrls = this.tagsToStyleUrls.get(tagName); |
34 |
| - const css = await getEffectiveStyle(theme, styleUrls, tagName); |
| 34 | + // Edge and IE |
| 35 | + if (injectedForTags.indexOf(tagName) !== -1) { |
| 36 | + return; |
| 37 | + } |
| 38 | + createStyleInHead(cssText, { "data-sap-source": tagName }); |
| 39 | + injectedForTags.push(tagName); |
35 | 40 |
|
36 |
| - const styleElement = document.head.querySelector(`style[data-sap-source="${tagName}"]`); |
| 41 | + // IE only |
| 42 | + if (window.CSSVarsSimulation) { |
| 43 | + const resolvedVarsCSS = window.CSSVarsSimulation.applyCSSVars(cssText); |
| 44 | + createStyleInHead(resolvedVarsCSS, { "data-sap-source-replaced-vars": tagName }); |
| 45 | + } |
| 46 | +}; |
37 | 47 |
|
38 |
| - if (styleElement) { |
39 |
| - styleElement.innerHTML = css || ""; // in case of undefined |
40 |
| - } else { |
41 |
| - this.createStyleTag(tagName, styleUrls, css || ""); |
42 |
| - } |
43 |
| - }); |
| 48 | +/** |
| 49 | + * Updates the style elements holding the CSS for all web components by resolving the CSS Custom properties |
| 50 | + */ |
| 51 | +const updateWebComponentStyles = () => { |
| 52 | + if (!window.CSSVarsSimulation) { |
| 53 | + return; |
44 | 54 | }
|
45 |
| -} |
46 | 55 |
|
47 |
| -export default new StyleInjection(); |
| 56 | + // IE only |
| 57 | + injectedForTags.forEach(tagName => { |
| 58 | + const originalStyleElement = document.head.querySelector(`style[data-sap-source="${tagName}"]`); |
| 59 | + const replacedVarsStyleElement = document.head.querySelector(`style[data-sap-source-replaced-vars="${tagName}"]`); |
| 60 | + const resolvedVarsCSS = window.CSSVarsSimulation.applyCSSVars(originalStyleElement.textContent); |
| 61 | + replacedVarsStyleElement.textContent = resolvedVarsCSS; |
| 62 | + }); |
| 63 | +}; |
| 64 | + |
| 65 | +export { |
| 66 | + injectThemeProperties, |
| 67 | + injectWebComponentStyle, |
| 68 | + updateWebComponentStyles, |
| 69 | +}; |
0 commit comments