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 (#3815)
  • Loading branch information
jaketig committed Feb 27, 2024
1 parent 74e86b5 commit b142638
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/core/useActiveElement/index.ts
@@ -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 b142638

Please sign in to comment.