-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow static children in shadow: false mode #56
Open
jvdsande
wants to merge
2
commits into
preactjs:master
Choose a base branch
from
jvdsande:features/no-shadow-children
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−13
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { h, cloneElement, render, hydrate } from 'preact'; | ||
import { h, Fragment, cloneElement, render, hydrate } from 'preact'; | ||
|
||
export default function register(Component, tagName, propNames, options) { | ||
function PreactElement() { | ||
const inst = Reflect.construct(HTMLElement, [], PreactElement); | ||
inst._vdomComponent = Component; | ||
inst._root = | ||
options && options.shadow ? inst.attachShadow({ mode: 'open' }) : inst; | ||
inst._shadowEnabled = options && options.shadow; | ||
inst._root = inst._shadowEnabled | ||
? inst.attachShadow({ mode: 'open' }) | ||
: inst; | ||
return inst; | ||
} | ||
|
||
PreactElement.prototype = Object.create(HTMLElement.prototype); | ||
PreactElement.prototype.constructor = PreactElement; | ||
PreactElement.prototype.connectedCallback = connectedCallback; | ||
|
@@ -24,15 +27,21 @@ export default function register(Component, tagName, propNames, options) { | |
propNames.forEach((name) => { | ||
Object.defineProperty(PreactElement.prototype, name, { | ||
get() { | ||
return this._vdom.props[name]; | ||
if (this._vdom) { | ||
return this._vdom.props[name]; | ||
} | ||
|
||
if (!this._props) this._props = {}; | ||
|
||
return this._props[name]; | ||
}, | ||
set(v) { | ||
if (this._vdom) { | ||
this.attributeChangedCallback(name, null, v); | ||
} else { | ||
if (!this._props) this._props = {}; | ||
this._props[name] = v; | ||
this.connectedCallback(); | ||
this._props[toCamelCase(name)] = v; | ||
} | ||
|
||
// Reflect property changes to attributes if the value is a primitive | ||
|
@@ -79,7 +88,7 @@ function connectedCallback() { | |
this._vdom = h( | ||
ContextProvider, | ||
{ ...this._props, context }, | ||
toVdom(this, this._vdomComponent) | ||
toVdom(this, this._vdomComponent, this._shadowEnabled) | ||
); | ||
(this.hasAttribute('hydrate') ? hydrate : render)(this._vdom, this._root); | ||
} | ||
|
@@ -114,24 +123,35 @@ function disconnectedCallback() { | |
* after having fired the event. | ||
*/ | ||
function Slot(props, context) { | ||
const { shadow, addContextListener, removeContextListener, ...rest } = props; | ||
|
||
const ref = (r) => { | ||
if (!r) { | ||
this.ref.removeEventListener('_preact', this._listener); | ||
removeContextListener(this._listener, this.ref); | ||
} else { | ||
this.ref = r; | ||
if (!this._listener) { | ||
this._listener = (event) => { | ||
event.stopPropagation(); | ||
event.detail.context = context; | ||
}; | ||
r.addEventListener('_preact', this._listener); | ||
addContextListener(this._listener, this.ref); | ||
} | ||
} | ||
}; | ||
return h('slot', { ...props, ref }); | ||
|
||
if (!shadow && !this._listener) { | ||
this._listener = (event) => { | ||
event.stopPropagation(); | ||
event.detail.context = context; | ||
}; | ||
addContextListener(this._listener); | ||
} | ||
|
||
return h(shadow ? 'slot' : Fragment, { ...rest, ref }); | ||
} | ||
|
||
function toVdom(element, nodeName) { | ||
function toVdom(element, nodeName, shadow) { | ||
if (element.nodeType === 3) return element.data; | ||
if (element.nodeType !== 1) return null; | ||
let children = [], | ||
|
@@ -147,17 +167,46 @@ function toVdom(element, nodeName) { | |
} | ||
|
||
for (i = cn.length; i--; ) { | ||
const vnode = toVdom(cn[i], null); | ||
const vnode = toVdom(cn[i], null, shadow); | ||
// Move slots correctly | ||
const name = cn[i].slot; | ||
if (name) { | ||
props[name] = h(Slot, { name }, vnode); | ||
props[name] = h( | ||
Slot, | ||
{ | ||
name, | ||
shadow, | ||
addContextListener(listener, element = cn[i]) { | ||
element.addEventListener('_preact', listener); | ||
}, | ||
removeContextListener(listener, element = cn[i]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same Problem here as in 148 |
||
element.removeEventListener('_preact', listener); | ||
}, | ||
}, | ||
vnode | ||
); | ||
} else { | ||
children[i] = vnode; | ||
} | ||
} | ||
|
||
// Only wrap the topmost node with a slot | ||
const wrappedChildren = nodeName ? h(Slot, null, children) : children; | ||
const wrappedProps = { | ||
shadow, | ||
addContextListener(listener, e = element) { | ||
e.addEventListener('_preact', listener); | ||
}, | ||
removeContextListener(listener, e = element) { | ||
e.removeEventListener('_preact', listener); | ||
}, | ||
}; | ||
|
||
const wrappedChildren = nodeName ? h(Slot, wrappedProps, children) : children; | ||
|
||
// Remove all children from the topmost node in non-shadow mode | ||
if (!shadow && nodeName) { | ||
element.innerHTML = ''; | ||
} | ||
|
||
return h(nodeName || element.nodeName.toLowerCase(), props, wrappedChildren); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work as expected. When called via
addContextListener(this._listener);
(see Line 148)element
is undefined.I fixed this by defining a
const childElement = cn[i];
at the beginning of thefor
loop and usingchildElement
instead ofcn[i]
. So in this case the line would be:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can see this seems to be a transpiler (in my case rollup) bug...
Rollup transpiles from
to code like
Which doesn't make any sense....