Skip to content

Commit 5b76843

Browse files
committed
feat(runtime-dom): support passing initial props to custom element constructor
1 parent 7a7e1d8 commit 5b76843

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ describe('defineCustomElement', () => {
1717

1818
describe('mounting/unmount', () => {
1919
const E = defineCustomElement({
20-
render: () => h('div', 'hello')
20+
props: {
21+
msg: {
22+
type: String,
23+
default: 'hello'
24+
}
25+
},
26+
render() {
27+
return h('div', this.msg)
28+
}
2129
})
2230
customElements.define('my-element', E)
2331

@@ -30,13 +38,13 @@ describe('defineCustomElement', () => {
3038
})
3139

3240
test('should work w/ manual instantiation', () => {
33-
const e = new E()
41+
const e = new E({ msg: 'inline' })
3442
// should lazy init
3543
expect(e._instance).toBe(null)
3644
// should initialize on connect
3745
container.appendChild(e)
3846
expect(e._instance).toBeTruthy()
39-
expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
47+
expect(e.shadowRoot!.innerHTML).toBe(`<div>inline</div>`)
4048
})
4149

4250
test('should unmount on remove', async () => {

packages/runtime-dom/src/apiCustomElement.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import {
2323
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
2424
import { hydrate, render } from '.'
2525

26-
type VueElementConstructor<P = {}> = {
27-
new (): VueElement & P
26+
export type VueElementConstructor<P = {}> = {
27+
new (initialProps?: Record<string, any>): VueElement & P
2828
}
2929

3030
// defineCustomElement provides the same type inference as defineComponent
@@ -134,8 +134,8 @@ export function defineCustomElement(
134134
static get observedAttributes() {
135135
return attrKeys
136136
}
137-
constructor() {
138-
super(Comp, attrKeys, propKeys, hydate)
137+
constructor(initialProps?: Record<string, any>) {
138+
super(Comp, initialProps, attrKeys, propKeys, hydate)
139139
}
140140
}
141141

@@ -163,10 +163,6 @@ const BaseClass = (
163163
) as typeof HTMLElement
164164

165165
export class VueElement extends BaseClass {
166-
/**
167-
* @internal
168-
*/
169-
_props: Record<string, any> = {}
170166
/**
171167
* @internal
172168
*/
@@ -178,6 +174,7 @@ export class VueElement extends BaseClass {
178174

179175
constructor(
180176
private _def: ComponentOptions & { styles?: string[] },
177+
private _props: Record<string, any> = {},
181178
private _attrKeys: string[],
182179
private _propKeys: string[],
183180
hydrate?: RootHydrateFunction

packages/runtime-dom/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ function normalizeContainer(
198198
export {
199199
defineCustomElement,
200200
defineSSRCustomElement,
201-
VueElement
201+
VueElement,
202+
VueElementConstructor
202203
} from './apiCustomElement'
203204

204205
// SFC CSS utilities

0 commit comments

Comments
 (0)