Skip to content

Commit c540701

Browse files
authored
fix(useFocusWithin): make useFocusWhithin match the behavior of the :focus-within (#4134)
1 parent 7f25b3a commit c540701

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type { ComputedRef } from 'vue-demi'
2-
import { computed } from 'vue-demi'
2+
import { computed, ref } from 'vue-demi'
33
import type { MaybeElementRef } from '../unrefElement'
44
import { unrefElement } from '../unrefElement'
55
import { useActiveElement } from '../useActiveElement'
66
import type { ConfigurableWindow } from '../_configurable'
7+
import { defaultWindow } from '../_configurable'
8+
import { useEventListener } from '../useEventListener'
79

810
export interface UseFocusWithinReturn {
911
/**
@@ -12,6 +14,9 @@ export interface UseFocusWithinReturn {
1214
focused: ComputedRef<boolean>
1315
}
1416

17+
const EVENT_FOCUS_IN = 'focusin'
18+
const EVENT_FOCUS_OUT = 'focusout'
19+
1520
/**
1621
* Track if focus is contained within the target element
1722
*
@@ -20,9 +25,18 @@ export interface UseFocusWithinReturn {
2025
* @param options Focus within options
2126
*/
2227
export function useFocusWithin(target: MaybeElementRef, options: ConfigurableWindow = {}): UseFocusWithinReturn {
23-
const activeElement = useActiveElement(options)
28+
const { window = defaultWindow } = options
2429
const targetElement = computed(() => unrefElement(target))
25-
const focused = computed(() => (targetElement.value && activeElement.value) ? targetElement.value.contains(activeElement.value) : false)
30+
const _focused = ref(false)
31+
const focused = computed(() => _focused.value)
32+
const activeElement = useActiveElement(options)
33+
34+
if (!window || !activeElement.value) {
35+
return { focused }
36+
}
37+
38+
useEventListener(targetElement, EVENT_FOCUS_IN, () => _focused.value = true)
39+
useEventListener(targetElement, EVENT_FOCUS_OUT, () => _focused.value = false)
2640

2741
return { focused }
2842
}

0 commit comments

Comments
 (0)