Skip to content

Commit 0cd98c3

Browse files
committedAug 3, 2020
fix(runtime-dom): style binding multi value support
fix #1759
1 parent f6afe70 commit 0cd98c3

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed
 

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

+12
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ describe(`runtime-dom: style patching`, () => {
7474
const store: any = {}
7575
return {
7676
style: {
77+
display: '',
7778
WebkitTransition: '',
7879
setProperty(key: string, val: string) {
7980
store[key] = val
@@ -96,4 +97,15 @@ describe(`runtime-dom: style patching`, () => {
9697
patchProp(el as any, 'style', {}, { transition: 'all 1s' })
9798
expect(el.style.WebkitTransition).toBe('all 1s')
9899
})
100+
101+
it('multiple values', () => {
102+
const el = mockElementWithStyle()
103+
patchProp(
104+
el as any,
105+
'style',
106+
{},
107+
{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }
108+
)
109+
expect(el.style.display).toBe('flex')
110+
})
99111
})

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

+24-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { isString, hyphenate, capitalize } from '@vue/shared'
1+
import { isString, hyphenate, capitalize, isArray } from '@vue/shared'
22
import { camelize } from '@vue/runtime-core'
33

4-
type Style = string | Partial<CSSStyleDeclaration> | null
4+
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
@@ -13,7 +13,7 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
1313
}
1414
} else {
1515
for (const key in next) {
16-
setStyle(style, key, next[key] as string)
16+
setStyle(style, key, next[key])
1717
}
1818
if (prev && !isString(prev)) {
1919
for (const key in prev) {
@@ -27,21 +27,29 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
2727

2828
const importantRE = /\s*!important$/
2929

30-
function setStyle(style: CSSStyleDeclaration, name: string, val: string) {
31-
if (name.startsWith('--')) {
32-
// custom property definition
33-
style.setProperty(name, val)
30+
function setStyle(
31+
style: CSSStyleDeclaration,
32+
name: string,
33+
val: string | string[]
34+
) {
35+
if (isArray(val)) {
36+
val.forEach(v => setStyle(style, name, v))
3437
} else {
35-
const prefixed = autoPrefix(style, name)
36-
if (importantRE.test(val)) {
37-
// !important
38-
style.setProperty(
39-
hyphenate(prefixed),
40-
val.replace(importantRE, ''),
41-
'important'
42-
)
38+
if (name.startsWith('--')) {
39+
// custom property definition
40+
style.setProperty(name, val)
4341
} else {
44-
style[prefixed as any] = val
42+
const prefixed = autoPrefix(style, name)
43+
if (importantRE.test(val)) {
44+
// !important
45+
style.setProperty(
46+
hyphenate(prefixed),
47+
val.replace(importantRE, ''),
48+
'important'
49+
)
50+
} else {
51+
style[prefixed as any] = val
52+
}
4553
}
4654
}
4755
}

0 commit comments

Comments
 (0)
Please sign in to comment.