diff --git a/packages/runtime-dom/__tests__/customElement.spec.ts b/packages/runtime-dom/__tests__/customElement.spec.ts index 12d6f722057..d8aad9c0af1 100644 --- a/packages/runtime-dom/__tests__/customElement.spec.ts +++ b/packages/runtime-dom/__tests__/customElement.spec.ts @@ -342,6 +342,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 fa1b7170288..97a84ee918d 100644 --- a/packages/runtime-dom/src/apiCustomElement.ts +++ b/packages/runtime-dom/src/apiCustomElement.ts @@ -38,10 +38,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: defineCustomElement with options object, infer props from options @@ -127,9 +133,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) { @@ -141,9 +151,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 = (