@@ -46,9 +46,9 @@ export interface DefineCustomElementConfig {
46
46
export function defineCustomElement < Props , RawBindings = object > (
47
47
setup : (
48
48
props : Readonly < Props > ,
49
- ctx : SetupContext
49
+ ctx : SetupContext ,
50
50
) => RawBindings | RenderFunction ,
51
- config ?: DefineCustomElementConfig
51
+ config ?: DefineCustomElementConfig ,
52
52
) : VueElementConstructor < Props >
53
53
54
54
// overload 2: object format with no props
@@ -80,7 +80,7 @@ export function defineCustomElement<
80
80
II ,
81
81
S
82
82
> & { styles ?: string [ ] } ,
83
- config ?: DefineCustomElementConfig
83
+ config ?: DefineCustomElementConfig ,
84
84
) : VueElementConstructor < Props >
85
85
86
86
// overload 3: object format with array props declaration
@@ -112,7 +112,7 @@ export function defineCustomElement<
112
112
II ,
113
113
S
114
114
> & { styles ?: string [ ] } ,
115
- config ?: DefineCustomElementConfig
115
+ config ?: DefineCustomElementConfig ,
116
116
) : VueElementConstructor < { [ K in PropNames ] : any } >
117
117
118
118
// overload 4: object format with object props declaration
@@ -144,7 +144,7 @@ export function defineCustomElement<
144
144
II ,
145
145
S
146
146
> & { styles ?: string [ ] } ,
147
- config ?: DefineCustomElementConfig
147
+ config ?: DefineCustomElementConfig ,
148
148
) : VueElementConstructor < ExtractPropTypes < PropsOptions > >
149
149
150
150
// overload 5: defining a custom element from the returned value of
@@ -157,7 +157,7 @@ export function defineCustomElement<P>(
157
157
export function defineCustomElement (
158
158
options : any ,
159
159
config ?: DefineCustomElementConfig ,
160
- hydrate ?: RootHydrateFunction
160
+ hydrate ?: RootHydrateFunction ,
161
161
) : VueElementConstructor {
162
162
const Comp = defineComponent ( options ) as any
163
163
class VueCustomElement extends VueElement {
@@ -173,7 +173,7 @@ export function defineCustomElement(
173
173
/*! #__NO_SIDE_EFFECTS__ */
174
174
export const defineSSRCustomElement = ( (
175
175
options : any ,
176
- config ?: DefineCustomElementConfig
176
+ config ?: DefineCustomElementConfig ,
177
177
) => {
178
178
// @ts -expect-error
179
179
return defineCustomElement ( options , config , hydrate )
@@ -202,14 +202,14 @@ export class VueElement extends BaseClass {
202
202
private _def : InnerComponentDef ,
203
203
private _props : Record < string , any > = { } ,
204
204
private _config : DefineCustomElementConfig = { } ,
205
- hydrate ?: RootHydrateFunction
205
+ hydrate ?: RootHydrateFunction ,
206
206
) {
207
207
super ( )
208
208
this . _config = extend (
209
209
{
210
- shadowRoot : true
210
+ shadowRoot : true ,
211
211
} ,
212
- this . _config
212
+ this . _config ,
213
213
)
214
214
215
215
if ( this . _config . shadowRoot ) {
@@ -219,7 +219,7 @@ export class VueElement extends BaseClass {
219
219
if ( __DEV__ && this . shadowRoot ) {
220
220
warn (
221
221
`Custom element has pre-rendered declarative shadow root but is not ` +
222
- `defined as hydratable. Use \`defineSSRCustomElement\`.`
222
+ `defined as hydratable. Use \`defineSSRCustomElement\`.` ,
223
223
)
224
224
}
225
225
this . attachShadow ( { mode : 'open' } )
@@ -331,7 +331,7 @@ export class VueElement extends BaseClass {
331
331
// replace slot
332
332
if ( ! this . _config . shadowRoot ) {
333
333
this . _slots = Array . from ( this . children ) . map (
334
- n => n . cloneNode ( true ) as Element
334
+ n => n . cloneNode ( true ) as Element ,
335
335
)
336
336
this . replaceChildren ( )
337
337
}
@@ -423,24 +423,30 @@ export class VueElement extends BaseClass {
423
423
}
424
424
425
425
private _createVNode ( ) : VNode < any , any > {
426
- let childs = null
426
+ let childs : Record < string , unknown > | null = null
427
427
// web components without shadow DOM do not supports native slot
428
428
// so, we create a VNode based on the content of child nodes.
429
- // NB: named slots are currently not supported
430
429
if ( ! this . _config . shadowRoot ) {
431
- childs = ( ) => {
432
- const toObj = ( a : NamedNodeMap ) => {
433
- const res : Record < string , string | null > = { }
434
- for ( let i = 0 , l = a . length ; i < l ; i ++ ) {
435
- const attr = a [ i ]
436
- res [ attr . nodeName ] = attr . nodeValue
430
+ if ( this . _slots ) {
431
+ childs = { }
432
+ this . _slots . forEach ( slot => {
433
+ const key =
434
+ slot . slot !== null && slot . slot . length ? slot . slot : 'default'
435
+ if ( childs !== null ) {
436
+ childs [ key ] = ( ) => {
437
+ const toObj = ( a : NamedNodeMap ) => {
438
+ const res : Record < string , string | null > = { }
439
+ for ( let i = 0 , l = a . length ; i < l ; i ++ ) {
440
+ const attr = a [ i ]
441
+ res [ attr . nodeName ] = attr . nodeValue
442
+ }
443
+ return res
444
+ }
445
+ const attrs = slot . attributes ? toObj ( slot . attributes ) : { }
446
+ attrs . innerHTML = slot . innerHTML
447
+ return createVNode ( slot . tagName , attrs )
448
+ }
437
449
}
438
- return res
439
- }
440
- return this . _slots ! . map ( ele => {
441
- const attrs = ele . attributes ? toObj ( ele . attributes ) : { }
442
- attrs . innerHTML = ele . innerHTML
443
- return createVNode ( ele . tagName , attrs , null )
444
450
} )
445
451
}
446
452
}
0 commit comments