Skip to content

Commit 8ce7220

Browse files
Merge pull request #27 from preactjs/shadow-option
Add support for `shadow` option
2 parents d980274 + 01bcdce commit 8ce7220

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { h, cloneElement, render, hydrate } from 'preact';
22

3-
export default function register(Component, tagName, propNames) {
3+
export default function register(Component, tagName, propNames, options) {
44
function PreactElement() {
55
const inst = Reflect.construct(HTMLElement, [], PreactElement);
66
inst._vdomComponent = Component;
7+
inst._root = options && options.shadow ? inst.attachShadow({ mode: 'open' }) : inst;
78
return inst;
89
}
910
PreactElement.prototype = Object.create(HTMLElement.prototype);
@@ -21,19 +22,19 @@ export default function register(Component, tagName, propNames) {
2122

2223
function connectedCallback() {
2324
this._vdom = toVdom(this, this._vdomComponent);
24-
(this.hasAttribute('hydrate') ? hydrate : render)(this._vdom, this);
25+
(this.hasAttribute('hydrate') ? hydrate : render)(this._vdom, this._root);
2526
}
2627

2728
function attributeChangedCallback(name, oldValue, newValue) {
2829
if (!this._vdom) return;
2930
const props = {};
3031
props[name] = newValue;
3132
this._vdom = cloneElement(this._vdom, props);
32-
render(this._vdom, this);
33+
render(this._vdom, this._root);
3334
}
3435

3536
function detachedCallback() {
36-
render(this._vdom = null, this);
37+
render(this._vdom = null, this._root);
3738
}
3839

3940
function toVdom(element, nodeName) {

0 commit comments

Comments
 (0)