|
1 | 1 | import { getTheme } from "./Configuration.js"; |
2 | 2 | import { getEffectiveStyle } from "./Theming.js"; |
| 3 | +import { injectWebComponentStyle } from "./theming/StyleInjection.js"; |
3 | 4 |
|
4 | 5 | const styleMap = new Map(); |
5 | 6 |
|
6 | | -const createStyleElement = css => { |
7 | | - // Create a local <style> tag for the real shadow DOM |
8 | | - const style = document.createElement("style"); |
9 | | - style.innerHTML = css; |
10 | | - return style; |
11 | | -}; |
12 | | - |
13 | | -const createConstructableStyleSheet = css => { |
14 | | - const elementStyleSheet = new CSSStyleSheet(); |
15 | | - elementStyleSheet.replaceSync(css); |
16 | | - return elementStyleSheet; |
| 7 | +/** |
| 8 | + * Creates the needed CSS for a web component class in the head tag |
| 9 | + * Note: IE11, Edge |
| 10 | + * @param ElementClass |
| 11 | + */ |
| 12 | +const createHeadStyle = ElementClass => { |
| 13 | + const tag = ElementClass.getMetadata().getTag(); |
| 14 | + const cssContent = getEffectiveStyle(ElementClass); |
| 15 | + injectWebComponentStyle(tag, cssContent); |
17 | 16 | }; |
18 | 17 |
|
19 | | -const _createStyle = (tagName, styleContent) => { |
| 18 | +/** |
| 19 | + * Returns (and caches) a constructable style sheet for a web component class |
| 20 | + * Note: Chrome |
| 21 | + * @param ElementClass |
| 22 | + * @returns {*} |
| 23 | + */ |
| 24 | +const getConstructableStyle = ElementClass => { |
| 25 | + const tagName = ElementClass.getMetadata().getTag(); |
| 26 | + const styleContent = getEffectiveStyle(ElementClass); |
20 | 27 | const theme = getTheme(); |
21 | 28 | const key = theme + tagName; |
22 | 29 | if (styleMap.has(key)) { |
23 | 30 | return styleMap.get(key); |
24 | 31 | } |
25 | 32 |
|
26 | | - let style; |
27 | | - if (document.adoptedStyleSheets) { |
28 | | - style = createConstructableStyleSheet(styleContent); |
29 | | - } else { |
30 | | - style = createStyleElement(styleContent); |
31 | | - } |
| 33 | + const style = new CSSStyleSheet(); |
| 34 | + style.replaceSync(styleContent); |
32 | 35 |
|
33 | 36 | styleMap.set(key, style); |
34 | 37 | return style; |
35 | 38 | }; |
36 | 39 |
|
37 | | -const createStyle = ElementClass => { |
38 | | - const tagName = ElementClass.getMetadata().getTag(); |
| 40 | +/** |
| 41 | + * Returns the CSS to be injected inside a web component shadow root, or undefined if not needed |
| 42 | + * Note: FF, Safari |
| 43 | + * @param ElementClass |
| 44 | + * @returns {string} |
| 45 | + */ |
| 46 | +const getShadowRootStyle = ElementClass => { |
| 47 | + if (document.adoptedStyleSheets || window.ShadyDOM) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
39 | 51 | const styleContent = getEffectiveStyle(ElementClass); |
40 | | - return _createStyle(tagName, styleContent); |
| 52 | + return styleContent; |
41 | 53 | }; |
42 | 54 |
|
43 | 55 | // eslint-disable-next-line |
44 | | -export { createStyle }; |
| 56 | +export { createHeadStyle, getConstructableStyle, getShadowRootStyle}; |
0 commit comments