Skip to content

Commit

Permalink
Merge pull request #102 from PrefectHQ/moved-watcher-after-init
Browse files Browse the repository at this point in the history
chore: fixed watcher before init error
  • Loading branch information
stackoverfloweth authored Sep 13, 2022
2 parents 78261c1 + 9b8bb50 commit 38eada3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@prefecthq/vue-compositions",
"private": false,
"version": "0.1.46",
"version": "0.1.47",
"description": "A collection of reusable vue compositions.",
"main": "index.ts",
"scripts": {
Expand Down
22 changes: 11 additions & 11 deletions src/useComputedStyle/useComputedStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ export function useComputedStyle(element: Element | Ref<Element | undefined>): T
const elementRef = ref(element)
const style = reactive({})

watch(elementRef, element => {
if (element && globalExists('window')) {
Object.assign(style, window.getComputedStyle(element, null))

mutationObserver.disconnect()
mutationObserver.observe(elementRef, { attributes: true })
resizeObserver.disconnect()
resizeObserver.observe(elementRef)
}
}, { immediate: true })

function handleMutation([entry]: MutationRecord[]): void {
if (nodeIsElement(entry.target) && globalExists('window')) {
Object.assign(style, window.getComputedStyle(entry.target, null))
Expand All @@ -33,6 +22,17 @@ export function useComputedStyle(element: Element | Ref<Element | undefined>): T
}
const resizeObserver = useResizeObserver(handleResize)

watch(elementRef, element => {
if (element && globalExists('window')) {
Object.assign(style, window.getComputedStyle(element, null))

mutationObserver.disconnect()
mutationObserver.observe(elementRef, { attributes: true, childList: true })
resizeObserver.disconnect()
resizeObserver.observe(elementRef)
}
}, { immediate: true })

return toRefs(style)
}

Expand Down

0 comments on commit 38eada3

Please sign in to comment.