Skip to content

Commit 8902a35

Browse files
tdt17chgohlke
authored andcommitted
Use web components polyfills (#17)
* add polyfills (credits to Artmann) * change version for compability * add babel plugins for custom elements * fix retargetEvents * fix overwriting props and constructorHook
1 parent 32673d9 commit 8902a35

File tree

5 files changed

+138
-92
lines changed

5 files changed

+138
-92
lines changed

.babelrc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"presets": [
33
["env", {
44
"targets": {
5-
"browsers": ["last 2 versions", "safari >= 7"]
5+
"browsers": [
6+
"last 2 version"
7+
]
68
}
79
}]
810
],
9-
"env": {
10-
"production": {
11-
"presets": ["minify"]
12-
}
13-
}
11+
"plugins": [
12+
"transform-custom-element-classes",
13+
"transform-es2015-classes"
14+
]
1415
}

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-web-component",
3-
"version": "1.3.0",
3+
"version": "2.0.0",
44
"description": "Create Web Components with React",
55
"main": "src/index.js",
66
"types": "index.d.ts",
@@ -11,7 +11,9 @@
1111
"build": "babel src/dev -d src --presets minify"
1212
},
1313
"dependencies": {
14-
"react-shadow-dom-retarget-events": "^1.0.8"
14+
"react-shadow-dom-retarget-events": "^1.0.8",
15+
"@webcomponents/custom-elements": "^1.2.1",
16+
"@webcomponents/shadydom": "^1.4.3"
1517
},
1618
"peerDependencies": {
1719
"react-dom": "^16.2.0"
@@ -20,7 +22,9 @@
2022
"@types/react": "^16.0.38",
2123
"babel-cli": "^6.26.0",
2224
"babel-minify": "^0.3.0",
23-
"babel-preset-env": "^1.6.1",
24-
"babel-preset-minify": "^0.3.0"
25+
"babel-preset-env": "^1.7.0",
26+
"babel-preset-minify": "^0.3.0",
27+
"babel-plugin-transform-custom-element-classes": "^0.1.0",
28+
"babel-plugin-transform-es2015-classes": "^6.24.1"
2529
}
2630
}

src/dev/index.js

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ const retargetEvents = require('react-shadow-dom-retarget-events');
33
const getStyleElementsFromReactWebComponentStyleLoader = require('./getStyleElementsFromReactWebComponentStyleLoader');
44
const extractAttributes = require('./extractAttributes');
55

6+
require('@webcomponents/shadydom');
7+
require('@webcomponents/custom-elements');
8+
69
module.exports = {
710
/**
811
* @param {JSX.Element} app
@@ -35,62 +38,48 @@ module.exports = {
3538
}
3639
}
3740

38-
const proto = Object.create(HTMLElement.prototype, {
39-
attachedCallback: {
40-
value: function() {
41-
let webComponentInstance = this;
42-
let mountPoint = webComponentInstance;
43-
44-
if (useShadowDom) {
45-
// Re-assign the webComponentInstance (this) to the newly created shadowRoot
46-
webComponentInstance = webComponentInstance.createShadowRoot();
41+
const proto = class extends HTMLElement {
42+
connectedCallback() {
43+
const webComponentInstance = this;
44+
let mountPoint = webComponentInstance;
4745

48-
// Re-assign the mountPoint to the newly created "div" element
49-
mountPoint = document.createElement('div');
46+
if (useShadowDom) {
47+
// Re-assign the webComponentInstance (this) to the newly created shadowRoot
48+
const shadowRoot = webComponentInstance.attachShadow({ mode: 'open' });
49+
// Re-assign the mountPoint to the newly created "div" element
50+
mountPoint = document.createElement('div');
5051

51-
// Move all of the styles assigned to the react component inside of the shadowRoot.
52-
// By default this is not used, only if the library is explicitly installed
53-
const styles = getStyleElementsFromReactWebComponentStyleLoader();
54-
styles.forEach((style) => {
55-
webComponentInstance.appendChild(style.cloneNode(webComponentInstance));
56-
});
52+
// Move all of the styles assigned to the react component inside of the shadowRoot.
53+
// By default this is not used, only if the library is explicitly installed
54+
const styles = getStyleElementsFromReactWebComponentStyleLoader();
55+
styles.forEach((style) => {
56+
shadowRoot.appendChild(style.cloneNode(shadowRoot));
57+
});
5758

58-
webComponentInstance.appendChild(mountPoint);
59+
shadowRoot.appendChild(mountPoint);
5960

60-
retargetEvents(webComponentInstance);
61-
}
61+
retargetEvents(shadowRoot);
62+
}
6263

63-
ReactDOM.render(app, mountPoint, function () {
64-
appInstance = this;
65-
appInstance.props = extractAttributes(webComponentInstance);
64+
ReactDOM.render(app, mountPoint, function () {
65+
appInstance = this;
66+
appInstance.props = Object.assign(extractAttributes(webComponentInstance), appInstance.props);
6667

67-
callConstructorHook(webComponentInstance);
68-
callLifeCycleHook('attachedCallback');
69-
});
70-
},
71-
},
72-
connectedCallback: {
73-
value: () => {
68+
callConstructorHook(webComponentInstance);
7469
callLifeCycleHook('connectedCallback');
75-
},
76-
},
77-
disconnectedCallback: {
78-
value: () => {
70+
});
71+
}
72+
disconnectedCallback () {
7973
callLifeCycleHook('disconnectedCallback');
80-
},
81-
},
82-
attributeChangedCallback: {
83-
value: (attributeName, oldValue, newValue, namespace) => {
84-
callLifeCycleHook('attributeChangedCallback', [attributeName, oldValue, newValue, namespace]);
85-
},
86-
},
87-
adoptedCallback: {
88-
value: (oldDocument, newDocument) => {
89-
callLifeCycleHook('adoptedCallback', [oldDocument, newDocument]);
90-
},
91-
},
92-
});
74+
}
75+
attributeChangedCallback (attributeName, oldValue, newValue, namespace) {
76+
callLifeCycleHook('attributeChangedCallback', [attributeName, oldValue, newValue, namespace]);
77+
}
78+
adoptedCallback (oldDocument, newDocument) {
79+
callLifeCycleHook('adoptedCallback', [oldDocument, newDocument]);
80+
}
81+
}
9382

94-
document.registerElement(tagName, { prototype: proto });
83+
customElements.define(tagName, proto);
9584
},
9685
};

src/index.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.

0 commit comments

Comments
 (0)