Skip to content

Commit 01df796

Browse files
committed
fix(cssVar): lose of reactivity, close vueuse#262
1 parent ffb64bd commit 01df796

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

packages/core/useCssVar/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@ export function useCssVar(
1818
if (!window)
1919
return ref('')
2020

21-
const varRef = ref('')
22-
const elRef = ref(unref(el) || window.document.documentElement)
21+
const variable = ref('')
22+
const _el = ref(el || window.document.documentElement)
2323

24-
tryOnMounted(() => {
25-
varRef.value = window.getComputedStyle(elRef.value).getPropertyValue(prop)
26-
})
24+
watch(
25+
_el,
26+
() => {
27+
if (_el.value)
28+
variable.value = window.getComputedStyle(_el.value).getPropertyValue(prop)
29+
},
30+
{ immediate: true },
31+
)
2732

2833
watch(
29-
varRef,
34+
variable,
3035
(val) => {
31-
if (elRef.value?.style)
32-
elRef.value.style.setProperty(prop, val)
36+
if (_el.value?.style)
37+
_el.value.style.setProperty(prop, val)
3338
},
3439
)
3540

36-
return varRef
41+
return variable
3742
}

0 commit comments

Comments
 (0)