Skip to content

Commit 7c8b126

Browse files
committedJun 14, 2024··
fix(custom-element): support same direct setup function signature in defineCustomElement
close #11116
1 parent 3e89a0d commit 7c8b126

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
@@ -338,6 +338,23 @@ describe('defineCustomElement', () => {
338338
expect(el.maxAge).toBe(50)
339339
expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
340340
})
341+
342+
test('support direct setup function syntax with extra options', () => {
343+
const E = defineCustomElement(
344+
props => {
345+
return () => props.text
346+
},
347+
{
348+
props: {
349+
text: String,
350+
},
351+
},
352+
)
353+
customElements.define('my-el-setup-with-props', E)
354+
container.innerHTML = `<my-el-setup-with-props text="hello"></my-el-setup-with-props>`
355+
const e = container.childNodes[0] as VueElement
356+
expect(e.shadowRoot!.innerHTML).toBe('hello')
357+
})
341358
})
342359

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

‎packages/runtime-dom/src/apiCustomElement.ts

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

3636
// overload 1: direct setup function
3737
export function defineCustomElement<Props, RawBindings = object>(
38-
setup: (
39-
props: Readonly<Props>,
40-
ctx: SetupContext,
41-
) => RawBindings | RenderFunction,
38+
setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction,
39+
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs' | 'emits'> & {
40+
props?: (keyof Props)[]
41+
},
42+
): VueElementConstructor<Props>
43+
export function defineCustomElement<Props, RawBindings = object>(
44+
setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction,
45+
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs' | 'emits'> & {
46+
props?: ComponentObjectPropsOptions<Props>
47+
},
4248
): VueElementConstructor<Props>
4349

4450
// overload 2: object format with no props
@@ -143,9 +149,13 @@ export function defineCustomElement<P>(
143149
/*! #__NO_SIDE_EFFECTS__ */
144150
export function defineCustomElement(
145151
options: any,
152+
extraOptions?: ComponentOptions,
153+
/**
154+
* @internal
155+
*/
146156
hydrate?: RootHydrateFunction,
147157
): VueElementConstructor {
148-
const Comp = defineComponent(options) as any
158+
const Comp = defineComponent(options, extraOptions) as any
149159
class VueCustomElement extends VueElement {
150160
static def = Comp
151161
constructor(initialProps?: Record<string, any>) {
@@ -157,9 +167,12 @@ export function defineCustomElement(
157167
}
158168

159169
/*! #__NO_SIDE_EFFECTS__ */
160-
export const defineSSRCustomElement = ((options: any) => {
170+
export const defineSSRCustomElement = ((
171+
options: any,
172+
extraOptions?: ComponentOptions,
173+
) => {
161174
// @ts-expect-error
162-
return defineCustomElement(options, hydrate)
175+
return defineCustomElement(options, extraOptions, hydrate)
163176
}) as typeof defineCustomElement
164177

165178
const BaseClass = (

0 commit comments

Comments
 (0)
Please sign in to comment.