-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(drawer,dialog): detect click outside more precisely (#157)
Co-authored-by: JD Solanki <[email protected]>
- Loading branch information
1 parent
7a0a1b9
commit d6e181e
Showing
3 changed files
with
41 additions
and
7 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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { MaybeElementRef } from '@vueuse/core' | ||
|
||
export function onClickSameTarget(elRef: MaybeElementRef, handler: (e: MouseEvent) => void) { | ||
let isMouseDownOnTarget = false | ||
let isMouseUpOnTarget = false | ||
|
||
// refer to this https://javascript.info/mouse-events-basics | ||
// events fired in the order: mousedown -> mouseup -> click | ||
const onClick = (e: MouseEvent) => { | ||
if (isMouseDownOnTarget && isMouseUpOnTarget) | ||
handler(e) | ||
|
||
isMouseDownOnTarget = isMouseUpOnTarget = false | ||
} | ||
|
||
const onMousedown = (e: MouseEvent) => { | ||
isMouseDownOnTarget = e.target === e.currentTarget | ||
} | ||
const onMouseup = (e: MouseEvent) => { | ||
isMouseUpOnTarget = e.target === e.currentTarget | ||
} | ||
|
||
const cleanup = [ | ||
useEventListener(elRef as Element, 'click', onClick), | ||
useEventListener(elRef as Element, 'mousedown', onMousedown), | ||
useEventListener(elRef as Element, 'mouseup', onMouseup), | ||
] | ||
|
||
const stop = () => cleanup.forEach(fn => fn()) | ||
|
||
return stop | ||
} |