β¬ οΈ Back to Table of Contents
π useCssVar¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 2 |
| π¦ Imports | 10 |
| π’ Vue Composition API | 3 |
| π Interfaces | 1 |
π Table of Contents¶
π οΈ File Location:¶
π packages/core/useCssVar/index.ts
π¦ Imports¶
| Name | Source |
|---|---|
MaybeRefOrGetter |
vue |
ConfigurableWindow |
../_configurable |
MaybeElementRef |
../unrefElement |
computed |
vue |
shallowRef |
vue |
toValue |
vue |
watch |
vue |
defaultWindow |
../_configurable |
unrefElement |
../unrefElement |
useMutationObserver |
../useMutationObserver |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
computed |
computed | none | none |
watch |
watch | none | none |
watch |
watch | none | none |
Functions¶
useCssVar(prop: MaybeRefOrGetter<string | null | undefiβ¦, target: MaybeElementRef, options: UseCssVarOptions): any¶
Manipulate CSS variables.
Parameters:
propany: No descriptiontargetany: No descriptionoptionsany: No description
See: https://vueuse.org/useCssVar
Raw JSDoc
Calls:
shallowRef (from vue)computed (from vue)unrefElement (from ../unrefElement)toValue (from vue)window.getComputedStyle(el).getPropertyValue(key)?.trimuseMutationObserver (from ../useMutationObserver)watch (from vue)old[0].style.removePropertyupdateCssVarel.style.removePropertyel.style.setProperty
Code
export function useCssVar(
prop: MaybeRefOrGetter<string | null | undefined>,
target?: MaybeElementRef,
options: UseCssVarOptions = {},
) {
const { window = defaultWindow, initialValue, observe = false } = options
const variable = shallowRef(initialValue)
const elRef = computed(() => unrefElement(target) || window?.document?.documentElement)
function updateCssVar() {
const key = toValue(prop)
const el = toValue(elRef)
if (el && window && key) {
const value = window.getComputedStyle(el).getPropertyValue(key)?.trim()
variable.value = value || variable.value || initialValue
}
}
if (observe) {
useMutationObserver(elRef, updateCssVar, {
attributeFilter: ['style', 'class'],
window,
})
}
watch(
[elRef, () => toValue(prop)],
(_, old) => {
if (old[0] && old[1])
old[0].style.removeProperty(old[1])
updateCssVar()
},
{ immediate: true },
)
watch(
[variable, elRef],
([val, el]) => {
const raw_prop = toValue(prop)
if (el?.style && raw_prop) {
if (val == null)
el.style.removeProperty(raw_prop)
else
el.style.setProperty(raw_prop, val)
}
},
{ immediate: true },
)
return variable
}
Internal helpers¶
Declared inside another function in this file.
updateCssVar(): void¶
Returns: void
Calls:
toValue (from vue)window.getComputedStyle(el).getPropertyValue(key)?.trim
Code
Interfaces¶
UseCssVarOptions¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
initialValue |
string |
β | not shown |
observe |
boolean |
β | not shown |
Generated by Syntax Scribe