Skip to content

Commit 3f38d59

Browse files
committed
fix(runtime-dom): fix behavior regression for v-show + style display binding
fix #4768
1 parent 3ca8317 commit 3f38d59

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ describe(`runtime-dom: style patching`, () => {
6868
expect(el.style.width).toBe('0px')
6969
})
7070

71+
it('should remove style attribute on falsy value', () => {
72+
const el = document.createElement('div')
73+
el.style.cssText = 'color: red;'
74+
patchProp(el as any, 'style', {}, null)
75+
expect(el.hasAttribute('style')).toBe(false)
76+
expect(el.style.cssText).toBe('')
77+
})
78+
7179
// JSDOM doesn't support custom properties on style object so we have to
7280
// mock it here.
7381
function mockElementWithStyle() {

packages/runtime-dom/src/modules/style.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ type Style = string | Record<string, string | string[]> | null
55

66
export function patchStyle(el: Element, prev: Style, next: Style) {
77
const style = (el as HTMLElement).style
8-
const currentDisplay = style.display
9-
if (!next) {
10-
el.removeAttribute('style')
11-
} else if (isString(next)) {
12-
if (prev !== next) {
13-
style.cssText = next
14-
}
15-
} else {
8+
const isCssString = isString(next)
9+
if (next && !isCssString) {
1610
for (const key in next) {
1711
setStyle(style, key, next[key])
1812
}
@@ -23,12 +17,21 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
2317
}
2418
}
2519
}
26-
}
27-
// indicates that the `display` of the element is controlled by `v-show`,
28-
// so we always keep the current `display` value regardless of the `style` value,
29-
// thus handing over control to `v-show`.
30-
if ('_vod' in el) {
31-
style.display = currentDisplay
20+
} else {
21+
const currentDisplay = style.display
22+
if (isCssString) {
23+
if (prev !== next) {
24+
style.cssText = next as string
25+
}
26+
} else if (prev) {
27+
el.removeAttribute('style')
28+
}
29+
// indicates that the `display` of the element is controlled by `v-show`,
30+
// so we always keep the current `display` value regardless of the `style`
31+
// value, thus handing over control to `v-show`.
32+
if ('_vod' in el) {
33+
style.display = currentDisplay
34+
}
3235
}
3336
}
3437

0 commit comments

Comments
 (0)