Skip to content

Commit 6dd67af

Browse files
authored
Merge pull request #6 from kopach/shadow
feat(*): add Ability to specify type of shadow DOM & disable it
2 parents 386db62 + 20284fe commit 6dd67af

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

dist/build.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
sendPropsToReact
55
} from "./prop-bridge";
66

7-
const getCustomElementFromReactComponent = RComponent => {
7+
const getCustomElementFromReactComponent = (RComponent, mode) => {
88
return class ReactAsCustomElement extends HTMLElement {
9-
shadow = null;
9+
targetNode = null;
1010
propBridgeRef = null;
1111
props = {};
1212
observer = null;
@@ -15,8 +15,20 @@ const getCustomElementFromReactComponent = RComponent => {
1515
super();
1616
this.props = getPropsFromNode(this);
1717
this.observer = new MutationObserver(this._onMutation);
18-
this.shadow = this.attachShadow({ mode: "closed" });
19-
renderReact2Node(RComponent, this.props, this.shadow, this._onReactMount);
18+
19+
switch (mode) {
20+
case 'open':
21+
this.targetNode = this.attachShadow({ mode: 'open' });
22+
break;
23+
case 'element':
24+
this.targetNode = this;
25+
break;
26+
default:
27+
this.targetNode = this.attachShadow({ mode: 'closed' });
28+
break;
29+
}
30+
31+
renderReact2Node(RComponent, this.props, this.targetNode, this._onReactMount);
2032
}
2133

2234
setProps = newProps => {
@@ -56,7 +68,15 @@ const getCustomElementFromReactComponent = RComponent => {
5668
};
5769
};
5870

59-
export const registerAsWebComponent = (component, customElementName) => {
60-
const ReactCustomElement = getCustomElementFromReactComponent(component);
71+
/* mode options:
72+
- no option (default) - closed shadow DOM
73+
- "open" - open shadow DOM
74+
- "element" - no shadow DOM
75+
*/
76+
export const registerAsWebComponent = (component, customElementName, mode) => {
77+
const ReactCustomElement = getCustomElementFromReactComponent(
78+
component,
79+
mode
80+
);
6181
customElements.define(customElementName, ReactCustomElement);
6282
};

src/prop-bridge.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const getPropsFromNode = node => {
4141
props[name] = node.getAttribute(name);
4242
return props;
4343
}, {});
44-
mappedProps.children = <ReactDomChild>{node.children}</ReactDomChild>;
44+
45+
const children = Array.from(node.children).map((e) => e.cloneNode(true));
46+
mappedProps.children = <ReactDomChild>{children}</ReactDomChild>;
4547
return mappedProps;
4648
};

0 commit comments

Comments
 (0)