1- import { h , cloneElement , render , hydrate } from 'preact' ;
1+ import { h , cloneElement , render , hydrate , Fragment } from 'preact' ;
22
33/**
44 * @typedef {import('./index.d.ts').PreactCustomElement } PreactCustomElement
@@ -26,7 +26,9 @@ export default function register(Component, tagName, propNames, options) {
2626 }
2727 PreactElement . prototype = Object . create ( HTMLElement . prototype ) ;
2828 PreactElement . prototype . constructor = PreactElement ;
29- PreactElement . prototype . connectedCallback = connectedCallback ;
29+ PreactElement . prototype . connectedCallback = function ( ) {
30+ connectedCallback . call ( this , options ) ;
31+ } ;
3032 PreactElement . prototype . attributeChangedCallback = attributeChangedCallback ;
3133 PreactElement . prototype . disconnectedCallback = disconnectedCallback ;
3234
@@ -89,7 +91,7 @@ function ContextProvider(props) {
8991/**
9092 * @this {PreactCustomElement}
9193 */
92- function connectedCallback ( ) {
94+ function connectedCallback ( options ) {
9395 // Obtain a reference to the previous context by pinging the nearest
9496 // higher up node that was rendered with Preact. If one Preact component
9597 // higher up receives our ping, it will set the `detail` property of
@@ -106,7 +108,7 @@ function connectedCallback() {
106108 this . _vdom = h (
107109 ContextProvider ,
108110 { ...this . _props , context } ,
109- toVdom ( this , this . _vdomComponent )
111+ toVdom ( this , this . _vdomComponent , options )
110112 ) ;
111113 ( this . hasAttribute ( 'hydrate' ) ? hydrate : render ) ( this . _vdom , this . _root ) ;
112114}
@@ -170,10 +172,11 @@ function Slot(props, context) {
170172 }
171173 }
172174 } ;
173- return h ( 'slot' , { ...props , ref } ) ;
175+ const { useFragment, ...rest } = props ;
176+ return h ( useFragment ? Fragment : 'slot' , { ...rest , ref } ) ;
174177}
175178
176- function toVdom ( element , nodeName ) {
179+ function toVdom ( element , nodeName , options ) {
177180 if ( element . nodeType === 3 ) return element . data ;
178181 if ( element . nodeType !== 1 ) return null ;
179182 let children = [ ] ,
@@ -189,7 +192,7 @@ function toVdom(element, nodeName) {
189192 }
190193
191194 for ( i = cn . length ; i -- ; ) {
192- const vnode = toVdom ( cn [ i ] , null ) ;
195+ const vnode = toVdom ( cn [ i ] , null , options ) ;
193196 // Move slots correctly
194197 const name = cn [ i ] . slot ;
195198 if ( name ) {
@@ -199,7 +202,15 @@ function toVdom(element, nodeName) {
199202 }
200203 }
201204
205+ const shadow = ! ! ( options && options . shadow ) ;
206+
202207 // Only wrap the topmost node with a slot
203- const wrappedChildren = nodeName ? h ( Slot , null , children ) : children ;
208+ const wrappedChildren = nodeName
209+ ? h ( Slot , { useFragment : ! shadow } , children )
210+ : children ;
211+
212+ if ( ! shadow && nodeName ) {
213+ element . innerHTML = '' ;
214+ }
204215 return h ( nodeName || element . nodeName . toLowerCase ( ) , props , wrappedChildren ) ;
205216}
0 commit comments