You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: resolve WebComponent constructor within h() #704
@Hotell I am trying to figure out how Preact could actually resolve this itself - how would it know the given function isn't a Component or pure functional Component?
I can't remember if I've suggested it before, but would it be feasible to do something like this in skate?
There'd be two options there - the first is to provide that functionality via your re-exported h(), instead exporting a wrapper:
import{createElement}from'preact';// (in 8.x createElement is an alias of h, using it here to avoid name conflict)exportfunctionh(nodeName, ...args){if(typeofnodeName==='function'&&typeofnodeName.is==='string'){nodeName=nodeName.is;}returncreateElement(nodeName, ...args);}
Option 2 would be to use preact's hooks to inject the Web Component constructor swapping into anything using Preact, even non-skate things:
import{options}from'preact';letoldHook=options.vnode;options.vnode=vnode=>{// this is super cheap because it mutates VNodes in-place and doesn't change shapeif(typeofvnode.nodeName==='function'&&typeofvnode.nodeName.is==='string'){vnode.nodeName=vnode.nodeName.is;}if(oldHook)oldHook(vnode);}
both solutions look good although that one where you check for is may be a weak assumption
@Hotell I am trying to figure out how Preact could actually resolve this itself - how would it know the given function isn't a Component or pure functional Component?
I messed with this a bit today and got it working at the diff level while only changing a few lines of code. Problem is, we need the new Babel because the version Preact is currently on doesn't transpile classes correctly to extend natives. Babel is being upgraded in #683 and I have all the code ready to go for this change.
I am trying to figure out how Preact could actually resolve this itself - how would it know the given function isn't a Component or pure functional Component?
Basically vnodeName.prototype instanceof HTMLElement in the typeof vnodeName === 'function' check.
Custom Elements also inherit from Node so vnodeName.ELEMENT_NODE === 1 should also work since HTMLElement is not available in the global namespace of ex. Node.js so it might throw when using the renderToString module.
Is HTMLElement the only class Custom Elements can extend?
Custom Elements also inherit from Node so vnodeName.ELEMENT_NODE === 1 should also work since HTMLElement is not available in the global namespace of ex. Node.js so it might throw when using the renderToString module.
This would also be a good approach. Happy to explore that option.
Is HTMLElement the only class Custom Elements can extend?
It's the furthest up it can extend. Custom elements can extend other custom elements, though.
Activity
developit commentedon May 25, 2017
@Hotell I am trying to figure out how Preact could actually resolve this itself - how would it know the given function isn't a Component or pure functional Component?
I can't remember if I've suggested it before, but would it be feasible to do something like this in skate?
There'd be two options there - the first is to provide that functionality via your re-exported
h(), instead exporting a wrapper:Option 2 would be to use preact's hooks to inject the Web Component constructor swapping into anything using Preact, even non-skate things:
Hotell commentedon May 29, 2017
both solutions look good although that one where you check for
ismay be a weak assumptionchecking prototype of the element I guess ?
cc @treshugart
treshugart commentedon May 30, 2017
I messed with this a bit today and got it working at the diff level while only changing a few lines of code. Problem is, we need the new Babel because the version Preact is currently on doesn't transpile classes correctly to extend natives. Babel is being upgraded in #683 and I have all the code ready to go for this change.
treshugart commentedon May 31, 2017
💥 #715 💥
Hotell commentedon May 31, 2017
Sweeeeeeet Christmassss !
treshugart commentedon Jun 1, 2017
@developit see PR, but re:
Basically
vnodeName.prototype instanceof HTMLElementin thetypeof vnodeName === 'function'check.thysultan commentedon Jun 1, 2017
Custom Elements also inherit from Node so
vnodeName.ELEMENT_NODE === 1should also work sinceHTMLElementis not available in the global namespace of ex. Node.js so it might throw when using therenderToStringmodule.Is
HTMLElementthe only class Custom Elements can extend?treshugart commentedon Jun 1, 2017
This would also be a good approach. Happy to explore that option.
It's the furthest up it can extend. Custom elements can extend other custom elements, though.
marvinhagemeister commentedon Oct 15, 2022
Closing as it's not something we're going to do. Since the time this issue was created there have been new jsx functions in
jsx-runtimetoo and I file like the better approach for turning Preact components into custom elements is: https://github.com/preactjs/preact-custom-element .