feat(runtime-dom/style): support CSS variables and auto prefixing
This commit is contained in:
parent
34e2725e9b
commit
2b2727e62c
@ -45,4 +45,33 @@ describe(`module style`, () => {
|
|||||||
expect(el.style.getPropertyValue('color')).toBe('red')
|
expect(el.style.getPropertyValue('color')).toBe('red')
|
||||||
expect(el.style.getPropertyValue('margin-right')).toBe('10px')
|
expect(el.style.getPropertyValue('margin-right')).toBe('10px')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// JSDOM doesn't support custom properties on style object so we have to
|
||||||
|
// mock it here.
|
||||||
|
function mockElementWithStyle() {
|
||||||
|
const store: any = {}
|
||||||
|
return {
|
||||||
|
style: {
|
||||||
|
WebkitTransition: '',
|
||||||
|
setProperty(key: string, val: string) {
|
||||||
|
store[key] = val
|
||||||
|
},
|
||||||
|
getPropertyValue(key: string) {
|
||||||
|
return store[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it('CSS custom properties', () => {
|
||||||
|
const el = mockElementWithStyle()
|
||||||
|
patchStyle(el as any, {}, { '--theme': 'red' } as any)
|
||||||
|
expect(el.style.getPropertyValue('--theme')).toBe('red')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('auto vendor prefixing', () => {
|
||||||
|
const el = mockElementWithStyle()
|
||||||
|
patchStyle(el as any, {}, { transition: 'all 1s' })
|
||||||
|
expect(el.style.WebkitTransition).toBe('all 1s')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { isString, hyphenate } from '@vue/shared'
|
import { isString, hyphenate, capitalize } from '@vue/shared'
|
||||||
|
import { camelize } from '@vue/runtime-core'
|
||||||
|
|
||||||
type Style = string | Partial<CSSStyleDeclaration> | null
|
type Style = string | Partial<CSSStyleDeclaration> | null
|
||||||
|
|
||||||
@ -25,16 +26,42 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
|
|||||||
const importantRE = /\s*!important$/
|
const importantRE = /\s*!important$/
|
||||||
|
|
||||||
function setStyle(style: CSSStyleDeclaration, name: string, val: string) {
|
function setStyle(style: CSSStyleDeclaration, name: string, val: string) {
|
||||||
let rawName = hyphenate(name)
|
if (name.startsWith('--')) {
|
||||||
if (importantRE.test(val)) {
|
// custom property definition
|
||||||
style.setProperty(rawName, val.replace(importantRE, ''), 'important')
|
style.setProperty(name, val)
|
||||||
} else {
|
} else {
|
||||||
/**
|
const prefixed = autoPrefix(style, name)
|
||||||
* TODO:
|
if (importantRE.test(val)) {
|
||||||
* 1. support values array created by autoprefixer.
|
// !important
|
||||||
* 2. support css variable, maybe should import 'csstype'.
|
style.setProperty(
|
||||||
* similar to https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L1450
|
hyphenate(prefixed),
|
||||||
*/
|
val.replace(importantRE, ''),
|
||||||
style.setProperty(rawName, val)
|
'important'
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
style[prefixed as any] = val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const prefixes = ['Webkit', 'Moz', 'ms']
|
||||||
|
const prefixCache: Record<string, string> = {}
|
||||||
|
|
||||||
|
function autoPrefix(style: CSSStyleDeclaration, rawName: string): string {
|
||||||
|
const cached = prefixCache[rawName]
|
||||||
|
if (cached) {
|
||||||
|
return cached
|
||||||
|
}
|
||||||
|
let name = camelize(rawName)
|
||||||
|
if (name !== 'filter' && name in style) {
|
||||||
|
return (prefixCache[rawName] = name)
|
||||||
|
}
|
||||||
|
name = capitalize(name)
|
||||||
|
for (let i = 0; i < prefixes.length; i++) {
|
||||||
|
const prefixed = prefixes[i] + name
|
||||||
|
if (prefixed in style) {
|
||||||
|
return (prefixCache[rawName] = prefixed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rawName
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user