Skip to content

Commit 4003029

Browse files
authored
refactor: remove placeholder span from shadow root (#503)
1 parent 5709e31 commit 4003029

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+353
-361
lines changed

packages/base/src/CSS.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,56 @@
11
import { getTheme } from "./Configuration.js";
22
import { getEffectiveStyle } from "./Theming.js";
3+
import { injectWebComponentStyle } from "./theming/StyleInjection.js";
34

45
const styleMap = new Map();
56

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);
1716
};
1817

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);
2027
const theme = getTheme();
2128
const key = theme + tagName;
2229
if (styleMap.has(key)) {
2330
return styleMap.get(key);
2431
}
2532

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);
3235

3336
styleMap.set(key, style);
3437
return style;
3538
};
3639

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+
3951
const styleContent = getEffectiveStyle(ElementClass);
40-
return _createStyle(tagName, styleContent);
52+
return styleContent;
4153
};
4254

4355
// eslint-disable-next-line
44-
export { createStyle };
56+
export { createHeadStyle, getConstructableStyle, getShadowRootStyle};

packages/base/src/Renderer.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import LitRenderer from "./renderer/LitRenderer.js";
2+
import { getShadowRootStyle } from "./CSS.js";
23

34
const RendererImpl = LitRenderer;
45

56
class Renderer {
67
static render(element) {
7-
const root = element._getRoot();
8-
const renderer = Object.getPrototypeOf(element).constructor.renderer.render;
9-
const renderResult = renderer(element);
8+
const root = element.shadowRoot;
9+
const { render } = Object.getPrototypeOf(element).constructor.renderer;
10+
const renderResult = render(element);
1011

11-
RendererImpl.render(renderResult, root, { eventContext: element });
12+
// For browsers that do not support constructable style sheets (and not using the polyfill)
13+
const styleToPrepend = getShadowRootStyle(element.constructor);
14+
15+
RendererImpl.render(renderResult, root, element, styleToPrepend);
1216
}
1317
}
1418

packages/base/src/UI5Element.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { getWCNoConflict, getCompactSize } from "./Configuration.js";
22
import DOMObserver from "./compatibility/DOMObserver.js";
3-
import ShadowDOM from "./compatibility/ShadowDOM.js";
43
import UI5ElementMetadata from "./UI5ElementMetadata.js";
54
import Integer from "./types/Integer.js";
65
import Renderer from "./Renderer.js";
76
import RenderScheduler from "./RenderScheduler.js";
8-
import { createStyle } from "./CSS.js";
7+
import { getConstructableStyle, createHeadStyle } from "./CSS.js";
98
import { attachThemeChange } from "./Theming.js";
109

1110
const metadata = {
@@ -44,7 +43,7 @@ class UI5Element extends HTMLElement {
4443
// polyfill theme handling is in head styles directly
4544
return;
4645
}
47-
const newStyle = createStyle(this.constructor);
46+
const newStyle = getConstructableStyle(this.constructor);
4847
if (document.adoptedStyleSheets) {
4948
this.shadowRoot.adoptedStyleSheets = [newStyle];
5049
} else {
@@ -69,11 +68,15 @@ class UI5Element extends HTMLElement {
6968
}
7069

7170
this.attachShadow({ mode: "open" });
72-
const shadowDOM = await ShadowDOM.prepareShadowDOM(this.constructor);
73-
this.shadowRoot.appendChild(shadowDOM);
7471

72+
// IE11, Edge
73+
if (window.ShadyDOM) {
74+
createHeadStyle(this.constructor);
75+
}
76+
77+
// Chrome
7578
if (document.adoptedStyleSheets) {
76-
const style = createStyle(this.constructor);
79+
const style = getConstructableStyle(this.constructor);
7780
this.shadowRoot.adoptedStyleSheets = [style];
7881
}
7982
}
@@ -467,17 +470,14 @@ class UI5Element extends HTMLElement {
467470
return;
468471
}
469472

470-
return this._getRoot().children[0];
473+
return this.shadowRoot.children.length === 1
474+
? this.shadowRoot.children[0] : this.shadowRoot.children[1];
471475
}
472476

473477
_waitForDomRef() {
474478
return this._domRefReadyPromise;
475479
}
476480

477-
_getRoot() {
478-
return this.shadowRoot.querySelector("[data-sap-ui-wc-root]");
479-
}
480-
481481
getFocusDomRef() {
482482
const domRef = this.getDomRef();
483483
if (domRef) {

packages/base/src/compatibility/ShadowDOM.js

Lines changed: 0 additions & 102 deletions
This file was deleted.

packages/base/src/renderer/LitRenderer.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import { render } from "lit-html";
1+
import { html, render } from "lit-html";
22

33
class LitRenderer {
4-
static render(renderResult, domNode, options) {
5-
render(renderResult, domNode, options);
4+
/**
5+
* Renders "templateResult" by replacing the content of "domNode", and optionally prepends a style tag containing "styles"
6+
* @param templateResult - lit template result object
7+
* @param domNode - the node whose content will be replaced
8+
* @param eventContext - the context of events, bound via the template
9+
* @param styles - if given, will be prepended in a style tag
10+
*/
11+
static render(templateResult, domNode, eventContext, styles) {
12+
if (styles) {
13+
templateResult = html`<style data-ui5-shadow-root-styles>${styles}</style>${templateResult}`;
14+
}
15+
render(templateResult, domNode, { eventContext });
616
}
717
}
818

packages/main/src/Button.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
aria-disabled="{{ariaDisabled}}"
66
data-sap-focus-ref
77
{{> ariaPressed}}
8+
dir="{{rtl}}"
89
>
910
{{#if icon}}
1011
<ui5-icon

packages/main/src/Button.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import UI5Element from "@ui5/webcomponents-base/src/UI5Element.js";
22
import Bootstrap from "@ui5/webcomponents-base/src/Bootstrap.js";
33
import { isSpace, isEnter } from "@ui5/webcomponents-base/src/events/PseudoEvents.js";
4+
import { getCompactSize } from "@ui5/webcomponents-base/src/Configuration.js";
5+
import getEffectiveRTL from "@ui5/webcomponents-base/src/util/getEffectiveRTL.js";
46
import ButtonType from "./types/ButtonType.js";
57
import ButtonRenderer from "./build/compiled/ButtonRenderer.lit.js";
68
import Icon from "./Icon.js";
@@ -252,6 +254,7 @@ class Button extends UI5Element {
252254
sapMBtnDisabled: this.disabled,
253255
sapMBtnIconEnd: this.iconEnd,
254256
[`sapMBtn${this.type}`]: true,
257+
sapUiSizeCompact: getCompactSize(),
255258
},
256259
icon: {
257260
sapWCIconInButton: true,
@@ -266,6 +269,10 @@ class Button extends UI5Element {
266269
return this.disabled ? "true" : undefined;
267270
}
268271

272+
get rtl() {
273+
return getEffectiveRTL() ? "rtl" : undefined;
274+
}
275+
269276
static async define(...params) {
270277
await Icon.define();
271278

packages/main/src/CalendarHeader.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<div
2-
class="{{classes.main}}">
2+
class="{{classes.main}}"
3+
dir="{{rtl}}"
4+
>
35

46
<ui5-icon id="{{_id}}-btnPrev"
57
class="{{classes.buttons}}"

packages/main/src/CalendarHeader.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import UI5Element from "@ui5/webcomponents-base/src/UI5Element.js";
22
import Bootstrap from "@ui5/webcomponents-base/src/Bootstrap.js";
33
import { isSpace, isEnter } from "@ui5/webcomponents-base/src/events/PseudoEvents.js";
4+
import { getCompactSize } from "@ui5/webcomponents-base/src/Configuration.js";
5+
import getEffectiveRTL from "@ui5/webcomponents-base/src/util/getEffectiveRTL.js";
46
import Button from "./Button.js";
57
import ButtonType from "./types/ButtonType.js";
68
import CalendarHeaderRenderer from "./build/compiled/CalendarHeaderRenderer.lit.js";
@@ -123,6 +125,7 @@ class CalendarHeader extends UI5Element {
123125
return {
124126
main: {
125127
sapWCCalHead: true,
128+
sapUiSizeCompact: getCompactSize(),
126129
},
127130
buttons: {
128131
sapWCCalHeadArrowButton: true,
@@ -134,6 +137,10 @@ class CalendarHeader extends UI5Element {
134137
};
135138
}
136139

140+
get rtl() {
141+
return getEffectiveRTL() ? "rtl" : undefined;
142+
}
143+
137144
static async define(...params) {
138145
await Button.define();
139146

packages/main/src/Card.hbs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<div class="{{classes.main}}">
1+
<div
2+
class="{{classes.main}}"
3+
dir="{{rtl}}"
4+
>
25
<header class="{{classes.header}}"
36
@click="{{_headerClick}}"
47
@keydown="{{_headerKeydown}}"

0 commit comments

Comments
 (0)