Skip to content

Commit

Permalink
fix(useActiveElement/useFocusWithin): replace computedWithControl wit…
Browse files Browse the repository at this point in the history
…h locally tracked ref (vueuse#3815)
  • Loading branch information
jaketig authored and noook committed Mar 26, 2024
1 parent 371e8da commit 815b5a8
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/core/useActiveElement/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computedWithControl } from '@vueuse/shared'
import { ref } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import type { ConfigurableDocumentOrShadowRoot, ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'
Expand Down Expand Up @@ -36,19 +36,21 @@ export function useActiveElement<T extends HTMLElement>(
return element
}

const activeElement = computedWithControl(
() => null,
() => getDeepActiveElement() as T | null | undefined,
)
const activeElement = ref<T | null | undefined>()
const trigger = () => {
activeElement.value = getDeepActiveElement() as T | null | undefined
}

if (window) {
useEventListener(window, 'blur', (event) => {
if (event.relatedTarget !== null)
return
activeElement.trigger()
trigger()
}, true)
useEventListener(window, 'focus', activeElement.trigger, true)
useEventListener(window, 'focus', trigger, true)
}

trigger()

return activeElement
}

0 comments on commit 815b5a8

Please sign in to comment.