-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathstyle.ts
41 lines (38 loc) · 1.11 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
import { isString, normalizeStyle } from '@vue/shared'
import { type Style, setStyle as setStyleValue } from '@vue/runtime-dom'
import { warn } from '../warning'
import { recordPropMetadata } from '../componentMetadata'
import { mergeInheritAttr } from './prop'
export function setStyle(el: HTMLElement, value: any, root?: boolean): void {
const prev = recordPropMetadata(
el,
'style',
(value = normalizeStyle(root ? mergeInheritAttr('style', value) : value)),
)
patchStyle(el, prev, value)
}
function patchStyle(el: Element, prev: Style, next: Style) {
const style = (el as HTMLElement).style
const isCssString = isString(next)
if (next && !isCssString) {
if (prev && !isString(prev)) {
for (const key in prev) {
if (next[key] == null) {
setStyleValue(style, key, '', warn)
}
}
}
for (const key in next) {
setStyleValue(style, key, next[key], warn)
}
} else {
if (isCssString) {
// TODO: combine with v-show
if (prev !== next) {
style.cssText = next
}
} else if (prev) {
el.removeAttribute('style')
}
}
}