fix(custom-element): support early-set domProps for async custom elements

close #11081
close #11082
This commit is contained in:
Evan You 2024-08-08 16:26:48 +08:00
parent 9b531d5716
commit a07e7bf553
No known key found for this signature in database
GPG Key ID: 00E9AB7A6704CE0A
3 changed files with 51 additions and 5 deletions

View File

@ -1206,4 +1206,39 @@ describe('defineCustomElement', () => {
'hello', 'hello',
) )
}) })
// #11081
test('Props can be casted when mounting custom elements in component rendering functions', async () => {
const E = defineCustomElement(
defineAsyncComponent(() =>
Promise.resolve({
props: ['fooValue'],
setup(props) {
expect(props.fooValue).toBe('fooValue')
return () => h('div', props.fooValue)
},
}),
),
)
customElements.define('my-el-async-4', E)
const R = defineComponent({
setup() {
const fooValue = ref('fooValue')
return () => {
return h('div', null, [
h('my-el-async-4', {
fooValue: fooValue.value,
}),
])
}
},
})
const app = createApp(R)
app.mount(container)
await new Promise(r => setTimeout(r))
const e = container.querySelector('my-el-async-4') as VueElement
expect(e.shadowRoot!.innerHTML).toBe(`<div>fooValue</div>`)
app.unmount()
})
}) })

View File

@ -200,6 +200,7 @@ export class VueElement
extends BaseClass extends BaseClass
implements ComponentCustomElementInterface implements ComponentCustomElementInterface
{ {
_isVueCE = true
/** /**
* @internal * @internal
*/ */
@ -208,6 +209,10 @@ export class VueElement
* @internal * @internal
*/ */
_app: App | null = null _app: App | null = null
/**
* @internal
*/
_root: Element | ShadowRoot
/** /**
* @internal * @internal
*/ */
@ -228,10 +233,6 @@ export class VueElement
*/ */
private _childStyles?: Map<string, HTMLStyleElement[]> private _childStyles?: Map<string, HTMLStyleElement[]>
private _ob?: MutationObserver | null = null private _ob?: MutationObserver | null = null
/**
* @internal
*/
public _root: Element | ShadowRoot
private _slots?: Record<string, Node[]> private _slots?: Record<string, Node[]>
constructor( constructor(

View File

@ -5,6 +5,7 @@ import { patchDOMProp } from './modules/props'
import { patchEvent } from './modules/events' import { patchEvent } from './modules/events'
import { isFunction, isModelListener, isOn, isString } from '@vue/shared' import { isFunction, isModelListener, isOn, isString } from '@vue/shared'
import type { RendererOptions } from '@vue/runtime-core' import type { RendererOptions } from '@vue/runtime-core'
import type { VueElement } from './apiCustomElement'
const isNativeOn = (key: string) => const isNativeOn = (key: string) =>
key.charCodeAt(0) === 111 /* o */ && key.charCodeAt(0) === 111 /* o */ &&
@ -127,5 +128,14 @@ function shouldSetAsProp(
return false return false
} }
return key in el if (key in el) {
return true
}
// #11081 force set props for possible async custom element
if ((el as VueElement)._isVueCE && (/[A-Z]/.test(key) || !isString(value))) {
return true
}
return false
} }