From 7c8b12620aad4969b8dc4944d4fc486d16c3033c Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 14 Jun 2024 15:18:51 +0200 Subject: [PATCH] fix(custom-element): support same direct setup function signature in defineCustomElement close #11116 --- .../__tests__/customElement.spec.ts | 17 ++++++++++++ packages/runtime-dom/src/apiCustomElement.ts | 27 ++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/packages/runtime-dom/__tests__/customElement.spec.ts b/packages/runtime-dom/__tests__/customElement.spec.ts index a8bdec77512..276536c2c31 100644 --- a/packages/runtime-dom/__tests__/customElement.spec.ts +++ b/packages/runtime-dom/__tests__/customElement.spec.ts @@ -338,6 +338,23 @@ describe('defineCustomElement', () => { expect(el.maxAge).toBe(50) expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number') }) + + test('support direct setup function syntax with extra options', () => { + const E = defineCustomElement( + props => { + return () => props.text + }, + { + props: { + text: String, + }, + }, + ) + customElements.define('my-el-setup-with-props', E) + container.innerHTML = `` + const e = container.childNodes[0] as VueElement + expect(e.shadowRoot!.innerHTML).toBe('hello') + }) }) describe('attrs', () => { diff --git a/packages/runtime-dom/src/apiCustomElement.ts b/packages/runtime-dom/src/apiCustomElement.ts index 93823027707..d3d8b5b6e73 100644 --- a/packages/runtime-dom/src/apiCustomElement.ts +++ b/packages/runtime-dom/src/apiCustomElement.ts @@ -35,10 +35,16 @@ export type VueElementConstructor

= { // overload 1: direct setup function export function defineCustomElement( - setup: ( - props: Readonly, - ctx: SetupContext, - ) => RawBindings | RenderFunction, + setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction, + options?: Pick & { + props?: (keyof Props)[] + }, +): VueElementConstructor +export function defineCustomElement( + setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction, + options?: Pick & { + props?: ComponentObjectPropsOptions + }, ): VueElementConstructor // overload 2: object format with no props @@ -143,9 +149,13 @@ export function defineCustomElement

( /*! #__NO_SIDE_EFFECTS__ */ export function defineCustomElement( options: any, + extraOptions?: ComponentOptions, + /** + * @internal + */ hydrate?: RootHydrateFunction, ): VueElementConstructor { - const Comp = defineComponent(options) as any + const Comp = defineComponent(options, extraOptions) as any class VueCustomElement extends VueElement { static def = Comp constructor(initialProps?: Record) { @@ -157,9 +167,12 @@ export function defineCustomElement( } /*! #__NO_SIDE_EFFECTS__ */ -export const defineSSRCustomElement = ((options: any) => { +export const defineSSRCustomElement = (( + options: any, + extraOptions?: ComponentOptions, +) => { // @ts-expect-error - return defineCustomElement(options, hydrate) + return defineCustomElement(options, extraOptions, hydrate) }) as typeof defineCustomElement const BaseClass = (