Skip to content

Commit

Permalink
feat(useCssVars): remove property on null/undefined (#3821)
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <noreply@github.com>
  • Loading branch information
ferferga committed Jul 17, 2024
1 parent efe4df8 commit fe19c74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
12 changes: 12 additions & 0 deletions packages/core/useCssVar/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ describe('useCssVar', () => {
expect(variable.value).toBe('red')
})

it('should handle null and undefined', () => {
const el = document.createElement('div')
const property = '---color'
const variable = useCssVar(property, el)

expect(window?.getComputedStyle(el).getPropertyValue('--color')).toBe('')
variable.value = 'red'
setTimeout(() => {
expect(window?.getComputedStyle(el).getPropertyValue('--color')).toBe('red')
}, 100)
})

it('should work observe', async () => {
const window = defaultWindow
const el = document.createElement('div')
Expand Down
23 changes: 16 additions & 7 deletions packages/core/useCssVar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ export interface UseCssVarOptions extends ConfigurableWindow {
* @param options
*/
export function useCssVar(
prop: MaybeRefOrGetter<string>,
prop: MaybeRefOrGetter<string | null | undefined>,
target?: MaybeElementRef,
options: UseCssVarOptions = {},
) {
const { window = defaultWindow, initialValue = '', observe = false } = options
const variable = ref(initialValue)
const { window = defaultWindow, initialValue, observe = false } = options
const variable = ref<string | null | undefined>(initialValue)
const elRef = computed(() => unrefElement(target) || window?.document?.documentElement)

function updateCssVar() {
const key = toValue(prop)
const el = toValue(elRef)
if (el && window) {
if (el && window && key) {
const value = window.getComputedStyle(el).getPropertyValue(key)?.trim()
variable.value = value || initialValue
}
Expand All @@ -51,15 +51,24 @@ export function useCssVar(

watch(
[elRef, () => toValue(prop)],
updateCssVar,
(_, old) => {
if (old[0] && old[1] && window)
window.getComputedStyle(old[0]).removeProperty(old[1])

This comment has been minimized.

Copy link
@CWSpear

CWSpear Aug 22, 2024

removeProperty causes an error, because you can't remove the property if it's readonly.

Even the demo is currently broken: https://vueuse.org/core/useCssVar/

image

This comment has been minimized.

Copy link
@CWSpear

CWSpear Aug 22, 2024

See #4156 & #4166

updateCssVar()
},
{ immediate: true },
)

watch(
variable,
(val) => {
if (elRef.value?.style)
elRef.value.style.setProperty(toValue(prop), val)
const raw_prop = toValue(prop)
if (elRef.value?.style && raw_prop) {
if (val == null)
elRef.value.style.removeProperty(raw_prop)
else
elRef.value.style.setProperty(raw_prop, val)
}
},
)

Expand Down

0 comments on commit fe19c74

Please sign in to comment.