@@ -25,18 +25,18 @@ export interface UseCssVarOptions extends ConfigurableWindow {
25
25
* @param options
26
26
*/
27
27
export function useCssVar (
28
- prop : MaybeRefOrGetter < string > ,
28
+ prop : MaybeRefOrGetter < string | null | undefined > ,
29
29
target ?: MaybeElementRef ,
30
30
options : UseCssVarOptions = { } ,
31
31
) {
32
- const { window = defaultWindow , initialValue = '' , observe = false } = options
33
- const variable = ref ( initialValue )
32
+ const { window = defaultWindow , initialValue, observe = false } = options
33
+ const variable = ref < string | null | undefined > ( initialValue )
34
34
const elRef = computed ( ( ) => unrefElement ( target ) || window ?. document ?. documentElement )
35
35
36
36
function updateCssVar ( ) {
37
37
const key = toValue ( prop )
38
38
const el = toValue ( elRef )
39
- if ( el && window ) {
39
+ if ( el && window && key ) {
40
40
const value = window . getComputedStyle ( el ) . getPropertyValue ( key ) ?. trim ( )
41
41
variable . value = value || initialValue
42
42
}
@@ -51,15 +51,24 @@ export function useCssVar(
51
51
52
52
watch (
53
53
[ elRef , ( ) => toValue ( prop ) ] ,
54
- updateCssVar ,
54
+ ( _ , old ) => {
55
+ if ( old [ 0 ] && old [ 1 ] && window )
56
+ window . getComputedStyle ( old [ 0 ] ) . removeProperty ( old [ 1 ] )
Has conversations. Original line has conversations.
57
+ updateCssVar ( )
58
+ } ,
55
59
{ immediate : true } ,
56
60
)
57
61
58
62
watch (
59
63
variable ,
60
64
( val ) => {
61
- if ( elRef . value ?. style )
62
- elRef . value . style . setProperty ( toValue ( prop ) , val )
65
+ const raw_prop = toValue ( prop )
66
+ if ( elRef . value ?. style && raw_prop ) {
67
+ if ( val == null )
68
+ elRef . value . style . removeProperty ( raw_prop )
69
+ else
70
+ elRef . value . style . setProperty ( raw_prop , val )
71
+ }
63
72
} ,
64
73
)
65
74