-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
174 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
import { ref, Ref, isRef, watch } from '@vue/runtime-dom' | ||
// import { ref, Ref, isRef, watch } from '@vue/runtime-dom'; | ||
|
||
export default function useCssVar( | ||
prop: string, | ||
refEl?: Ref<HTMLElement | null> | ||
) { | ||
const refVar = ref('') | ||
let el: HTMLElement = document.documentElement | ||
// TODO 打开注释会造成热更新失效,待排查 | ||
// export default function useCssVar(prop: string, refEl?: Ref<HTMLElement | null>) { | ||
// const refVar = ref(''); | ||
// let el: HTMLElement = document.documentElement; | ||
|
||
if (isRef(refEl)) { | ||
watch( | ||
refEl, | ||
() => { | ||
if (refEl.value) { | ||
el = refEl.value as HTMLElement | ||
refVar.value = getComputedStyle(el).getPropertyValue(prop) | ||
} | ||
}, | ||
{ immediate: true } | ||
) | ||
} else { | ||
refVar.value = getComputedStyle(el).getPropertyValue(prop) | ||
} | ||
// if (isRef(refEl)) { | ||
// watch( | ||
// refEl, | ||
// () => { | ||
// if (refEl.value) { | ||
// el = refEl.value as HTMLElement; | ||
// refVar.value = getComputedStyle(el).getPropertyValue(prop); | ||
// } | ||
// }, | ||
// { immediate: true } | ||
// ); | ||
// } else { | ||
// refVar.value = getComputedStyle(el).getPropertyValue(prop); | ||
// } | ||
|
||
watch( | ||
refVar, | ||
val => { | ||
el && el.style.setProperty(prop, val) | ||
}, | ||
{ immediate: true } | ||
) | ||
// watch( | ||
// refVar, | ||
// (val) => { | ||
// el && el.style.setProperty(prop, val); | ||
// }, | ||
// { immediate: true } | ||
// ); | ||
|
||
return refVar | ||
// return refVar; | ||
// } | ||
|
||
export function getCssVar(prop: string, dom = document.documentElement) { | ||
return getComputedStyle(dom).getPropertyValue(prop); | ||
} | ||
|
||
export function setCssVar(prop: string, val: any, dom = document.documentElement) { | ||
dom.style.setProperty(prop, val); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.