await nextTick()
expect($div.style.display).toEqual('')
})
+
+ // #10151
+ test('should respect the display value when v-show value is true', async () => {
+ const isVisible = ref(false)
+ const useDisplayStyle = ref(true)
+ const compStyle = ref({
+ display: 'none',
+ })
+ const withoutDisplayStyle = {
+ margin: '10px',
+ }
+
+ const Component = {
+ setup() {
+ return () => {
+ return withVShow(
+ h('div', {
+ style: useDisplayStyle.value
+ ? compStyle.value
+ : withoutDisplayStyle,
+ }),
+ isVisible.value,
+ )
+ }
+ },
+ }
+ render(h(Component), root)
+
+ const $div = root.children[0]
+
+ expect($div.style.display).toEqual('none')
+
+ isVisible.value = true
+ await nextTick()
+ expect($div.style.display).toEqual('none')
+
+ compStyle.value.display = 'block'
+ await nextTick()
+ expect($div.style.display).toEqual('block')
+
+ compStyle.value.display = 'inline-block'
+ await nextTick()
+ expect($div.style.display).toEqual('inline-block')
+
+ isVisible.value = false
+ await nextTick()
+ expect($div.style.display).toEqual('none')
+
+ isVisible.value = true
+ await nextTick()
+ expect($div.style.display).toEqual('inline-block')
+
+ useDisplayStyle.value = false
+ await nextTick()
+ expect($div.style.display).toEqual('')
+ expect(getComputedStyle($div).display).toEqual('block')
+
+ isVisible.value = false
+ await nextTick()
+ expect($div.style.display).toEqual('none')
+
+ isVisible.value = true
+ await nextTick()
+ expect($div.style.display).toEqual('')
+ })
+
+ // #10294
+ test('should record display by vShowOldKey only when display exists in style', async () => {
+ const isVisible = ref(false)
+ const style = ref({
+ margin: '10px',
+ })
+
+ const Component = {
+ setup() {
+ return () => {
+ return withVShow(
+ h('div', {
+ style: style.value,
+ }),
+ isVisible.value,
+ )
+ }
+ },
+ }
+ render(h(Component), root)
+ const $div = root.children[0]
+
+ expect($div.style.display).toEqual('none')
+
+ style.value.margin = '20px'
+ await nextTick()
+ expect($div.style.display).toEqual('none')
+
+ isVisible.value = true
+ await nextTick()
+ expect($div.style.display).toEqual('')
+ })
})
type Style = string | Record<string, string | string[]> | null
+const displayRE = /(^|;)\s*display\s*:/
+
export function patchStyle(el: Element, prev: Style, next: Style) {
const style = (el as HTMLElement).style
- const currentDisplay = style.display
const isCssString = isString(next)
+ const currentDisplay = style.display
+ let hasControlledDisplay = false
if (next && !isCssString) {
if (prev && !isString(prev)) {
for (const key in prev) {
}
}
for (const key in next) {
+ if (key === 'display') {
+ hasControlledDisplay = true
+ }
setStyle(style, key, next[key])
}
} else {
;(next as string) += ';' + cssVarText
}
style.cssText = next as string
+ hasControlledDisplay = displayRE.test(next)
}
} else if (prev) {
el.removeAttribute('style')
// so we always keep the current `display` value regardless of the `style`
// value, thus handing over control to `v-show`.
if (vShowOldKey in el) {
+ el[vShowOldKey] = hasControlledDisplay ? style.display : ''
style.display = currentDisplay
}
}