forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(onClickOutside): add
bubble
modifier to directive (vueuse#2183)
- Loading branch information
Showing
3 changed files
with
31 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
import type { FunctionDirective } from 'vue-demi' | ||
import { directiveHooks } from '@vueuse/shared' | ||
import type { ObjectDirective } from 'vue-demi' | ||
import { onClickOutside } from '.' | ||
import type { OnClickOutsideHandler, OnClickOutsideOptions } from '.' | ||
|
||
const handler = (): FunctionDirective<any, <E = PointerEvent>(evt: E) => void> => { | ||
let stop = null as unknown as ReturnType<typeof onClickOutside> | ||
return (el, binding) => { | ||
if (stop) { | ||
stop() | ||
stop = onClickOutside(el, binding.value) | ||
return | ||
export const vOnClickOutside: ObjectDirective< | ||
HTMLElement, | ||
OnClickOutsideHandler | [(evt: any) => void, OnClickOutsideOptions] | ||
> = { | ||
[directiveHooks.mounted](el, binding) { | ||
const capture = !binding.modifiers.bubble | ||
if (typeof binding.value === 'function') { | ||
(el as any).__onClickOutside_stop = onClickOutside(el, binding.value, { capture }) | ||
} | ||
stop = onClickOutside(el, binding.value) | ||
} | ||
else { | ||
const [handler, options] = binding.value | ||
;(el as any).__onClickOutside_stop = onClickOutside(el, handler, Object.assign({ capture }, options)) | ||
} | ||
}, | ||
[directiveHooks.unmounted](el) { | ||
(el as any).__onClickOutside_stop() | ||
}, | ||
} | ||
|
||
export const vOnClickOutside = handler() | ||
|
||
// alias | ||
export { vOnClickOutside as VOnClickOutside } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters