-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
124 additions
and
57,212 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
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 was deleted.
Oops, something went wrong.
Empty file.
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,61 @@ | ||
import { each, isElement } from "lodash-es"; | ||
import { onMounted, onUnmounted, watch } from "vue"; | ||
import type { ComputedRef, Ref, WatchStopHandle } from "vue"; | ||
import type { TooltipProps } from "./types"; | ||
|
||
export function useEvenstToTiggerNode( | ||
props: TooltipProps & { virtualTriggering?: boolean }, | ||
triggerNode: ComputedRef<HTMLElement | undefined>, | ||
events: Ref<Record<string, EventListener>>, | ||
closeMethod: () => void | ||
) { | ||
let watchEventsStopHandle: WatchStopHandle | void; | ||
let watchTriggerNodeStopHandle: WatchStopHandle | void; | ||
|
||
const _eventHandleMap = new Map(); | ||
|
||
const _bindEventToVirtualTiggerNode = () => { | ||
const el = triggerNode.value; | ||
isElement(el) && | ||
each(events.value, (fn, event) => { | ||
_eventHandleMap.set(event, fn); | ||
el?.addEventListener(event as keyof HTMLElementEventMap, fn); | ||
}); | ||
}; | ||
const _unbindEventToVirtualTiggerNode = () => { | ||
const el = triggerNode.value; | ||
isElement(el) && | ||
each( | ||
["mouseenter", "click", "contextmenu"], | ||
(key) => | ||
_eventHandleMap.has(key) && | ||
el?.removeEventListener(key, _eventHandleMap.get(key)) | ||
); | ||
}; | ||
|
||
onMounted(() => { | ||
watchTriggerNodeStopHandle = watch( | ||
triggerNode, | ||
() => props.virtualTriggering && _bindEventToVirtualTiggerNode(), | ||
{ immediate: true } | ||
); | ||
|
||
watchEventsStopHandle = watch( | ||
events, | ||
() => { | ||
if (!props.virtualTriggering) return; | ||
_unbindEventToVirtualTiggerNode(); | ||
_bindEventToVirtualTiggerNode(); | ||
closeMethod(); | ||
}, | ||
{ deep: true } | ||
); | ||
}); | ||
|
||
onUnmounted(() => { | ||
watchTriggerNodeStopHandle?.(); | ||
watchEventsStopHandle?.(); | ||
}); | ||
} | ||
|
||
export default useEvenstToTiggerNode; |
Oops, something went wrong.