Skip to content

Commit

Permalink
fix(runtime-dom): fix multi vShow impact each other
Browse files Browse the repository at this point in the history
  • Loading branch information
Doctor-wu committed Feb 25, 2024
1 parent e0e0253 commit 0e03778
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/runtime-dom/src/directives/vShow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ interface VShowElement extends HTMLElement {
[vShowOriginalDisplay]: string
}

const resolvedPromise = Promise.resolve()

export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
beforeMount(el, { value }, { transition }) {
el[vShowOriginalDisplay] =
Expand All @@ -25,21 +27,24 @@ export const vShow: ObjectDirective<VShowElement> & { name?: 'show' } = {
updated(el, { value, oldValue }, { transition }) {
if (
!value === !oldValue &&
(el.style.display === el[vShowOriginalDisplay] || !value)
(el.style.display === el[vShowOriginalDisplay] || (!value && transition))
)
return
if (transition) {
if (value) {
transition.beforeEnter(el)
setDisplay(el, true)
postSetDisplay(el, true)
transition.enter(el)
} else {
transition.leave(el, () => {
setDisplay(el, false)
postSetDisplay(el, false)
})
}
} else {
setDisplay(el, value)
// #10038
// when multi vShow be applied to the same element
// and the sync setDisplay will impact other vShow
postSetDisplay(el, value)
}
},
beforeUnmount(el, { value }) {
Expand All @@ -55,6 +60,12 @@ function setDisplay(el: VShowElement, value: unknown): void {
el.style.display = value ? el[vShowOriginalDisplay] : 'none'
}

function postSetDisplay(el: VShowElement, value: unknown): void {
resolvedPromise.then(() => {
setDisplay(el, value)
})
}

// SSR vnode transforms, only used when user includes client-oriented render
// function in SSR
export function initVShowForSSR() {
Expand Down

0 comments on commit 0e03778

Please sign in to comment.