Skip to content

Commit

Permalink
feat(onClickOutside): allow selector strings for ignore list (#2439)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
sibbng and antfu committed Dec 20, 2022
1 parent 0590c43 commit 7b3db82
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/core/onClickOutside/index.ts
Expand Up @@ -9,7 +9,7 @@ export interface OnClickOutsideOptions extends ConfigurableWindow {
/**
* List of elements that should not trigger the event.
*/
ignore?: MaybeElementRef[]
ignore?: (MaybeElementRef | string)[]
/**
* Use capturing phase for internal event listener.
* @default true
Expand Down Expand Up @@ -48,8 +48,14 @@ export function onClickOutside<T extends OnClickOutsideOptions>(

const shouldIgnore = (event: PointerEvent) => {
return ignore.some((target) => {
const el = unrefElement(target)
return el && (event.target === el || event.composedPath().includes(el))
if (typeof target === 'string') {
return Array.from(window.document.querySelectorAll(target))
.some(el => el === event.target || event.composedPath().includes(el))
}
else {
const el = unrefElement(target)
return el && (event.target === el || event.composedPath().includes(el))
}
})
}

Expand Down

0 comments on commit 7b3db82

Please sign in to comment.