Skip to content

Commit 352bc88

Browse files
committed
fix(custom-element): avoid triggering mutationObserver when relecting props
close #12214 close #12215
1 parent 10ab8c0 commit 352bc88

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Diff for: packages/runtime-dom/__tests__/customElement.spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,38 @@ describe('defineCustomElement', () => {
396396
expect(e.value).toBe('hi')
397397
})
398398

399+
// #12214
400+
test('Boolean prop with default true', async () => {
401+
const E = defineCustomElement({
402+
props: {
403+
foo: {
404+
type: Boolean,
405+
default: true,
406+
},
407+
},
408+
render() {
409+
return String(this.foo)
410+
},
411+
})
412+
customElements.define('my-el-default-true', E)
413+
container.innerHTML = `<my-el-default-true></my-el-default-true>`
414+
const e = container.childNodes[0] as HTMLElement & { foo: any },
415+
shadowRoot = e.shadowRoot as ShadowRoot
416+
expect(shadowRoot.innerHTML).toBe('true')
417+
e.foo = undefined
418+
await nextTick()
419+
expect(shadowRoot.innerHTML).toBe('true')
420+
e.foo = false
421+
await nextTick()
422+
expect(shadowRoot.innerHTML).toBe('false')
423+
e.foo = null
424+
await nextTick()
425+
expect(shadowRoot.innerHTML).toBe('null')
426+
e.foo = ''
427+
await nextTick()
428+
expect(shadowRoot.innerHTML).toBe('true')
429+
})
430+
399431
test('support direct setup function syntax with extra options', () => {
400432
const E = defineCustomElement(
401433
props => {

Diff for: packages/runtime-dom/src/apiCustomElement.ts

+3
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,16 @@ export class VueElement
505505
}
506506
// reflect
507507
if (shouldReflect) {
508+
const ob = this._ob
509+
ob && ob.disconnect()
508510
if (val === true) {
509511
this.setAttribute(hyphenate(key), '')
510512
} else if (typeof val === 'string' || typeof val === 'number') {
511513
this.setAttribute(hyphenate(key), val + '')
512514
} else if (!val) {
513515
this.removeAttribute(hyphenate(key))
514516
}
517+
ob && ob.observe(this, { attributes: true })
515518
}
516519
}
517520
}

0 commit comments

Comments
 (0)