Skip to content

Commit e7f90b7

Browse files
committed
fix(custom-element): support same direct setup function signature in defineCustomElement
close #11116
1 parent 1224caf commit e7f90b7

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

packages/runtime-dom/__tests__/customElement.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,23 @@ describe('defineCustomElement', () => {
342342
expect(el.maxAge).toBe(50)
343343
expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
344344
})
345+
346+
test('support direct setup function syntax with extra options', () => {
347+
const E = defineCustomElement(
348+
props => {
349+
return () => props.text
350+
},
351+
{
352+
props: {
353+
text: String,
354+
},
355+
},
356+
)
357+
customElements.define('my-el-setup-with-props', E)
358+
container.innerHTML = `<my-el-setup-with-props text="hello"></my-el-setup-with-props>`
359+
const e = container.childNodes[0] as VueElement
360+
expect(e.shadowRoot!.innerHTML).toBe('hello')
361+
})
345362
})
346363

347364
describe('attrs', () => {

packages/runtime-dom/src/apiCustomElement.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ export type VueElementConstructor<P = {}> = {
3838

3939
// overload 1: direct setup function
4040
export function defineCustomElement<Props, RawBindings = object>(
41-
setup: (
42-
props: Readonly<Props>,
43-
ctx: SetupContext,
44-
) => RawBindings | RenderFunction,
41+
setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction,
42+
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs' | 'emits'> & {
43+
props?: (keyof Props)[]
44+
},
45+
): VueElementConstructor<Props>
46+
export function defineCustomElement<Props, RawBindings = object>(
47+
setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction,
48+
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs' | 'emits'> & {
49+
props?: ComponentObjectPropsOptions<Props>
50+
},
4551
): VueElementConstructor<Props>
4652

4753
// overload 2: defineCustomElement with options object, infer props from options
@@ -127,9 +133,13 @@ export function defineCustomElement<P>(
127133
/*! #__NO_SIDE_EFFECTS__ */
128134
export function defineCustomElement(
129135
options: any,
136+
extraOptions?: ComponentOptions,
137+
/**
138+
* @internal
139+
*/
130140
hydrate?: RootHydrateFunction,
131141
): VueElementConstructor {
132-
const Comp = defineComponent(options) as any
142+
const Comp = defineComponent(options, extraOptions) as any
133143
class VueCustomElement extends VueElement {
134144
static def = Comp
135145
constructor(initialProps?: Record<string, any>) {
@@ -141,9 +151,12 @@ export function defineCustomElement(
141151
}
142152

143153
/*! #__NO_SIDE_EFFECTS__ */
144-
export const defineSSRCustomElement = ((options: any) => {
154+
export const defineSSRCustomElement = ((
155+
options: any,
156+
extraOptions?: ComponentOptions,
157+
) => {
145158
// @ts-expect-error
146-
return defineCustomElement(options, hydrate)
159+
return defineCustomElement(options, extraOptions, hydrate)
147160
}) as typeof defineCustomElement
148161

149162
const BaseClass = (

0 commit comments

Comments
 (0)