/**
 * Much of this has ben adopted from the cgood folks at @vueuse/core
 */ import { unref , watch } from "vue" ; import { isString , noop } from "@chakra-ui/utils" ; import { Fn , MaybeBaseRef , defaultWindow , tryOnScopeDispose , } from "@chakra-ui/vue-utils" ; interface InferEventTarget < Events > { addEventListener ( event , fn ? , options ? ) : any ; removeEventListener ( event , fn ? , options ? ) : any ; } export type WindowEventName = keyof WindowEventMap ; export type DocumentEventName = keyof DocumentEventMap ; export type GeneralEventListener < E = Event > = { ( evt ) : void ; } ; param event param listener param options export function useEventListener < E extends keyof WindowEventMap > ( event , listener , options ? ) ; param target param event param listener param options export function useEventListener < E extends keyof WindowEventMap > ( target , event , listener , options ? ) ; param target param event param listener param options export function useEventListener < Names extends string , EventType = Event > ( target , event , listener , options ? ) ; param target param event param listener param options export function useEventListener < EventType = Event > ( target , event , listener , options ? ) ; export function useEventListener ( ... args ) { let target ; let event ; let listener ; let options ; if ( isString ( args [ 0 ] ) ) { ; [ event , listener , options ] = args ; target = defaultWindow ; } else { ; [ target , event , listener , options ] = args ; } if ( ! target ) return noop ; let cleanup = noop ; const stopWatch = watch ( ( ) => unref ( target ) , ( el ) => { cleanup ( ) ; if ( ! el ) return ; el . addEventListener ( event , listener , options ) ; cleanup = ( ) => { el . removeEventListener ( event , listener , options ) ; cleanup = noop ; } ; } , { immediate : true , flush : "post" } ) ; const stop = ( ) => { stopWatch ( ) ; cleanup ( ) ; } ; tryOnScopeDispose ( stop ) ; return stop ; }