Skip to content

Commit

Permalink
fix(onClickOutside): call handler if click event is fired by a keyp…
Browse files Browse the repository at this point in the history
…ress (#2426)
  • Loading branch information
sibbng committed Nov 13, 2022
1 parent 6886ed1 commit ff96dc7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/core/onClickOutside/index.ts
@@ -1,5 +1,4 @@
import type { Fn } from '@vueuse/shared'
import { ref } from 'vue-demi'
import type { MaybeElementRef } from '../unrefElement'
import { unrefElement } from '../unrefElement'
import { useEventListener } from '../useEventListener'
Expand Down Expand Up @@ -43,7 +42,7 @@ export function onClickOutside<T extends OnClickOutsideOptions>(
if (!window)
return

const shouldListen = ref(true)
let shouldListen = true

let fallback: number

Expand All @@ -52,9 +51,14 @@ export function onClickOutside<T extends OnClickOutsideOptions>(

const el = unrefElement(target)

if (!el || el === event.target || event.composedPath().includes(el) || !shouldListen.value)
if (!el || el === event.target || event.composedPath().includes(el))
return

if (!shouldListen) {
shouldListen = true
return
}

handler(event)
}

Expand All @@ -69,7 +73,8 @@ export function onClickOutside<T extends OnClickOutsideOptions>(
useEventListener(window, 'click', listener, { passive: true, capture }),
useEventListener(window, 'pointerdown', (e) => {
const el = unrefElement(target)
shouldListen.value = !!el && !e.composedPath().includes(el) && !shouldIgnore(e)
if (el)
shouldListen = !e.composedPath().includes(el) && !shouldIgnore(e)
}, { passive: true }),
useEventListener(window, 'pointerup', (e) => {
if (e.button === 0) {
Expand Down

0 comments on commit ff96dc7

Please sign in to comment.