Skip to content

⬅️ Back to Table of Contents

📄 index.ts

📊 Analysis Summary

Metric Count
🔧 Functions 4
📦 Imports 13
📊 Variables & Constants 2
🟢 Vue Composition API 1
📐 Interfaces 2
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/core/useEventListener/index.ts

📦 Imports

Name Source
Arrayable @vueuse/shared
Fn @vueuse/shared
MaybeRef vue
MaybeRefOrGetter vue
isObject @vueuse/shared
toArray @vueuse/shared
tryOnScopeDispose @vueuse/shared
watchImmediate @vueuse/shared
computed vue
toValue vue
unref vue
defaultWindow ../_configurable
unrefElement ../unrefElement

Variables & Constants

Name Type Kind Value Exported
cleanups Function[] const []
optionsClone any const isObject(raw_options) ? { ...raw_options } : raw_options

Vue Composition API

Name Type Reactive Variables Composables
computed computed none none

Functions

useEventListener(event: MaybeRefOrGetter<Arrayable<E>>, listener: MaybeRef<Arrayable<(this: Window, ev: WindowEventMap[E]) => any>>, options: MaybeRefOrGetter<boolean | AddEventListenerOptions>): Fn

Code
export function useEventListener<E extends keyof WindowEventMap>(
  event: MaybeRefOrGetter<Arrayable<E>>,
  listener: MaybeRef<Arrayable<(this: Window, ev: WindowEventMap[E]) => any>>,
  options?: MaybeRefOrGetter<boolean | AddEventListenerOptions>
): Fn
  • JSDoc:

    /**
     * Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
     *
     * Overload 1: Omitted Window target
     *
     * @see https://vueuse.org/useEventListener
     * @param event
     * @param listener
     * @param options
     */
    

  • Parameters:

  • event: MaybeRefOrGetter<Arrayable<E>>
  • listener: MaybeRef<Arrayable<(this: Window, ev: WindowEventMap[E]) => any>>
  • options: MaybeRefOrGetter<boolean | AddEventListenerOptions>
  • Return Type: Fn

cleanup(): void

Code
() => {
    cleanups.forEach(fn => fn())
    cleanups.length = 0
  }
  • Return Type: void
  • Calls:
  • cleanups.forEach
  • fn

register(el: EventTarget, event: string, listener: any, options: boolean | AddEventListenerOptions | undefined): () => void

Code
(
    el: EventTarget,
    event: string,
    listener: any,
    options: boolean | AddEventListenerOptions | undefined,
  ) => {
    el.addEventListener(event, listener, options)
    return () => el.removeEventListener(event, listener, options)
  }
  • Parameters:
  • el: EventTarget
  • event: string
  • listener: any
  • options: boolean | AddEventListenerOptions | undefined
  • Return Type: () => void
  • Calls:
  • el.addEventListener
  • el.removeEventListener

stop(): void

Code
() => {
    stopWatch()
    cleanup()
  }
  • Return Type: void
  • Calls:
  • stopWatch
  • cleanup

Interfaces

InferEventTarget<Events>

Interface Code
interface InferEventTarget<Events> {
  addEventListener: (event: Events, fn?: any, options?: any) => any
  removeEventListener: (event: Events, fn?: any, options?: any) => any
}

Properties

Name Type Optional Description
addEventListener (event: Events, fn?: any, options?: any) => any
removeEventListener (event: Events, fn?: any, options?: any) => any

GeneralEventListener<E = Event>

Interface Code
export interface GeneralEventListener<E = Event> {
  (evt: E): void
}

Type Aliases

WindowEventName

type WindowEventName = keyof WindowEventMap;

DocumentEventName

type DocumentEventName = keyof DocumentEventMap;