@rettangoli/fe 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/core/runtime/props.js +49 -0
- package/src/web/componentDom.js +71 -7
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -29,6 +29,51 @@ export const readPropFallbackFromAttributes = (source, propName) => {
|
|
|
29
29
|
};
|
|
30
30
|
|
|
31
31
|
const REACTIVE_PROP_VALUES = Symbol("rtglReactivePropValues");
|
|
32
|
+
const NATIVE_HOST_STYLE = Symbol("rtglNativeHostStyle");
|
|
33
|
+
|
|
34
|
+
const findPrototypePropertyDescriptor = (source, propName) => {
|
|
35
|
+
let current = Object.getPrototypeOf(source);
|
|
36
|
+
|
|
37
|
+
while (current) {
|
|
38
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, propName);
|
|
39
|
+
if (descriptor) {
|
|
40
|
+
return descriptor;
|
|
41
|
+
}
|
|
42
|
+
current = Object.getPrototypeOf(current);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return undefined;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const getNativeHostStyle = (source) => {
|
|
49
|
+
if (!source || typeof source !== "object") {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (Object.prototype.hasOwnProperty.call(source, NATIVE_HOST_STYLE)) {
|
|
54
|
+
return source[NATIVE_HOST_STYLE];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const styleDescriptor = findPrototypePropertyDescriptor(source, "style");
|
|
58
|
+
let nativeStyle;
|
|
59
|
+
|
|
60
|
+
if (typeof styleDescriptor?.get === "function") {
|
|
61
|
+
nativeStyle = styleDescriptor.get.call(source);
|
|
62
|
+
} else if (source.style && typeof source.style === "object") {
|
|
63
|
+
nativeStyle = source.style;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (nativeStyle && typeof nativeStyle === "object") {
|
|
67
|
+
Object.defineProperty(source, NATIVE_HOST_STYLE, {
|
|
68
|
+
value: nativeStyle,
|
|
69
|
+
enumerable: false,
|
|
70
|
+
configurable: false,
|
|
71
|
+
writable: false,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return nativeStyle;
|
|
76
|
+
};
|
|
32
77
|
|
|
33
78
|
const ensureReactivePropValues = (source) => {
|
|
34
79
|
if (!Object.prototype.hasOwnProperty.call(source, REACTIVE_PROP_VALUES)) {
|
|
@@ -50,6 +95,10 @@ export const installReactiveProps = ({
|
|
|
50
95
|
}) => {
|
|
51
96
|
const reactiveValues = ensureReactivePropValues(source);
|
|
52
97
|
|
|
98
|
+
if (allowedKeys.includes("style")) {
|
|
99
|
+
getNativeHostStyle(source);
|
|
100
|
+
}
|
|
101
|
+
|
|
53
102
|
allowedKeys.forEach((propName) => {
|
|
54
103
|
if (typeof propName !== "string" || propName.length === 0) {
|
|
55
104
|
return;
|
package/src/web/componentDom.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { getNativeHostStyle } from "../core/runtime/props.js";
|
|
2
|
+
|
|
1
3
|
const COMMON_LINK_STYLE_TEXT = `
|
|
2
4
|
a, a:link, a:visited, a:hover, a:active {
|
|
3
5
|
display: contents;
|
|
@@ -12,13 +14,61 @@ const COMMON_LINK_STYLE_TEXT = `
|
|
|
12
14
|
}
|
|
13
15
|
`;
|
|
14
16
|
|
|
17
|
+
const RENDER_TARGET_ATTR = "data-rtgl-render-target";
|
|
18
|
+
const RENDER_TARGET_FLAG = "__rtglRenderTarget";
|
|
19
|
+
|
|
20
|
+
const hasRenderTargetAttr = (node) => {
|
|
21
|
+
if (!node || typeof node !== "object") {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof node.getAttribute === "function") {
|
|
26
|
+
return node.getAttribute(RENDER_TARGET_ATTR) !== null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return node[RENDER_TARGET_FLAG] === true;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const markRenderTarget = (node) => {
|
|
33
|
+
if (!node || typeof node !== "object") {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (typeof node.setAttribute === "function") {
|
|
38
|
+
node.setAttribute(RENDER_TARGET_ATTR, "");
|
|
39
|
+
} else {
|
|
40
|
+
node[RENDER_TARGET_FLAG] = true;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const findExistingRenderTarget = (shadow) => {
|
|
45
|
+
if (!shadow || typeof shadow !== "object") {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (typeof shadow.querySelector === "function") {
|
|
50
|
+
return shadow.querySelector(`[${RENDER_TARGET_ATTR}]`) ?? shadow.firstElementChild;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (Array.isArray(shadow.childNodes)) {
|
|
54
|
+
return shadow.childNodes.find(hasRenderTargetAttr) ?? shadow.childNodes[0];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (Array.isArray(shadow.children)) {
|
|
58
|
+
return shadow.children.find(hasRenderTargetAttr) ?? shadow.children[0];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return shadow.firstElementChild;
|
|
62
|
+
};
|
|
63
|
+
|
|
15
64
|
export const initializeComponentDom = ({
|
|
16
65
|
host,
|
|
17
66
|
cssText,
|
|
18
67
|
createStyleSheet = () => new CSSStyleSheet(),
|
|
19
68
|
createElement = (tagName) => document.createElement(tagName),
|
|
20
69
|
}) => {
|
|
21
|
-
const
|
|
70
|
+
const existingShadow = host.shadowRoot;
|
|
71
|
+
const shadow = existingShadow ?? host.attachShadow({ mode: "open" });
|
|
22
72
|
|
|
23
73
|
const commonStyleSheet = createStyleSheet();
|
|
24
74
|
commonStyleSheet.replaceSync(COMMON_LINK_STYLE_TEXT);
|
|
@@ -33,13 +83,27 @@ export const initializeComponentDom = ({
|
|
|
33
83
|
|
|
34
84
|
shadow.adoptedStyleSheets = adoptedStyleSheets;
|
|
35
85
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
86
|
+
let renderTarget = findExistingRenderTarget(shadow);
|
|
87
|
+
|
|
88
|
+
if (!renderTarget) {
|
|
89
|
+
renderTarget = createElement("div");
|
|
90
|
+
renderTarget.style.cssText = "display: contents;";
|
|
91
|
+
markRenderTarget(renderTarget);
|
|
92
|
+
shadow.appendChild(renderTarget);
|
|
93
|
+
} else {
|
|
94
|
+
renderTarget.style.cssText = "display: contents;";
|
|
95
|
+
if (!hasRenderTargetAttr(renderTarget)) {
|
|
96
|
+
markRenderTarget(renderTarget);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (renderTarget.parentNode !== shadow) {
|
|
101
|
+
shadow.appendChild(renderTarget);
|
|
102
|
+
}
|
|
103
|
+
const hostStyle = getNativeHostStyle(host);
|
|
104
|
+
if (hostStyle && typeof hostStyle === "object") {
|
|
105
|
+
hostStyle.display = "contents";
|
|
41
106
|
}
|
|
42
|
-
host.style.display = "contents";
|
|
43
107
|
|
|
44
108
|
return {
|
|
45
109
|
shadow,
|