-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathstyle.ts
64 lines (62 loc) · 1.85 KB
/
style.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { isString } from '@vue/shared'
import { warn } from '@vue/runtime-core'
import { type Style, setStyle } from '@vue/runtime-shared'
import {
type VShowElement,
vShowHidden,
vShowOriginalDisplay,
} from '../directives/vShow'
import { CSS_VAR_TEXT } from '../helpers/useCssVars'
const displayRE = /(^|;)\s*display\s*:/
export function patchStyle(el: Element, prev: Style, next: Style): void {
const style = (el as HTMLElement).style
const isCssString = isString(next)
let hasControlledDisplay = false
if (next && !isCssString) {
if (prev) {
if (!isString(prev)) {
for (const key in prev) {
if (next[key] == null) {
setStyle(style, key, '', warn)
}
}
} else {
for (const prevStyle of prev.split(';')) {
const key = prevStyle.slice(0, prevStyle.indexOf(':')).trim()
if (next[key] == null) {
setStyle(style, key, '', warn)
}
}
}
}
for (const key in next) {
if (key === 'display') {
hasControlledDisplay = true
}
setStyle(style, key, next[key], warn)
}
} else {
if (isCssString) {
if (prev !== next) {
// #9821
const cssVarText = (style as any)[CSS_VAR_TEXT]
if (cssVarText) {
;(next as string) += ';' + cssVarText
}
style.cssText = next as string
hasControlledDisplay = displayRE.test(next)
}
} else if (prev) {
el.removeAttribute('style')
}
}
// indicates the element also has `v-show`.
if (vShowOriginalDisplay in el) {
// make v-show respect the current v-bind style display when shown
el[vShowOriginalDisplay] = hasControlledDisplay ? style.display : ''
// if v-show is in hidden state, v-show has higher priority
if ((el as VShowElement)[vShowHidden]) {
style.display = 'none'
}
}
}