@yesstudio/nuxt-composables
    Preparing search index...

    Function useEventListener

    Adds event listeners to an EventTarget and manages their lifecycle within the current effect scope.

    const target = ref<EventTarget | null>(null)

    useEventListener(target, 'custom-event', (event) => {
    console.log('Custom event triggered', event)
    })

    The type of the event.

    The event target to attach the event listener to.

    The event or events to listen for.

    The listener or listeners to handle the event.

    Optional configuration options.

    A function to remove the event listeners.

    • Adds event listeners to a Window and manages their lifecycle within the current effect scope.

      Type Parameters

      Parameters

      • target: Window

        The window to attach the event listener to.

      • event: MaybeArray<T>

        The event or events to listen for.

      • listener: MaybeArray<UseWindowEventListener<T>>

        The listener or listeners to handle the event.

      • Optionaloptions: MaybeRefOrGetter<boolean | AddEventListenerOptions>

        Optional configuration options.

      Returns () => void

      A function to remove the event listeners.

      useEventListener(window, 'resize', (event) => {
      console.log('Window resized', event)
      })
    • Adds event listeners to a Document and manages their lifecycle within the current effect scope.

      Type Parameters

      Parameters

      Returns () => void

      A function to remove the event listeners.

      useEventListener(document, 'scroll', (event) => {
      console.log('Document scrolled', event)
      })
    • Adds event listeners to an ComponentPublicInstance and manages their lifecycle within the current effect scope.

      Type Parameters

      • T extends
            | "error"
            | "abort"
            | "animationcancel"
            | "animationend"
            | "animationiteration"
            | "animationstart"
            | "auxclick"
            | "beforeinput"
            | "beforematch"
            | "beforetoggle"
            | "blur"
            | "cancel"
            | "canplay"
            | "canplaythrough"
            | "change"
            | "click"
            | "close"
            | "compositionend"
            | "compositionstart"
            | "compositionupdate"
            | "contextlost"
            | "contextmenu"
            | "contextrestored"
            | "copy"
            | "cuechange"
            | "cut"
            | "dblclick"
            | "drag"
            | "dragend"
            | "dragenter"
            | "dragleave"
            | "dragover"
            | "dragstart"
            | "drop"
            | "durationchange"
            | "emptied"
            | "ended"
            | "focus"
            | "focusin"
            | "focusout"
            | "formdata"
            | "gotpointercapture"
            | "input"
            | "invalid"
            | "keydown"
            | "keypress"
            | "keyup"
            | "load"
            | "loadeddata"
            | "loadedmetadata"
            | "loadstart"
            | "lostpointercapture"
            | "mousedown"
            | "mouseenter"
            | "mouseleave"
            | "mousemove"
            | "mouseout"
            | "mouseover"
            | "mouseup"
            | "paste"
            | "pause"
            | "play"
            | "playing"
            | "pointercancel"
            | "pointerdown"
            | "pointerenter"
            | "pointerleave"
            | "pointermove"
            | "pointerout"
            | "pointerover"
            | "pointerrawupdate"
            | "pointerup"
            | "progress"
            | "ratechange"
            | "reset"
            | "resize"
            | "scroll"
            | "scrollend"
            | "securitypolicyviolation"
            | "seeked"
            | "seeking"
            | "select"
            | "selectionchange"
            | "selectstart"
            | "slotchange"
            | "stalled"
            | "submit"
            | "suspend"
            | "timeupdate"
            | "toggle"
            | "touchcancel"
            | "touchend"
            | "touchmove"
            | "touchstart"
            | "transitioncancel"
            | "transitionend"
            | "transitionrun"
            | "transitionstart"
            | "volumechange"
            | "waiting"
            | "webkitanimationend"
            | "webkitanimationiteration"
            | "webkitanimationstart"
            | "webkittransitionend"
            | "wheel"
            | "fullscreenchange"
            | "fullscreenerror"

        The type of the event.

      Parameters

      Returns () => void

      A function to remove the event listeners.

      const component = useTemplateRef<ComponentPublicInstance>('component')

      useEventListener(component, 'click', (event) => {
      console.log('ComponentPublicInstance clicked', event)
      })
    • Adds event listeners to an HTMLElement and manages their lifecycle within the current effect scope.

      Type Parameters

      Parameters

      Returns () => void

      A function to remove the event listeners.

      const element = useTemplateRef<HTMLElement>('element')

      useEventListener(element, 'click', (event) => {
      console.log('HTMLElement clicked', event)
      })
    • Adds event listeners to an SVGElement and manages their lifecycle within the current effect scope.

      Type Parameters

      Parameters

      Returns () => void

      A function to remove the event listeners.

      const element = useTemplateRef<SVGElement>('element')

      useEventListener(element, 'click', (event) => {
      console.log('SVGElement clicked', event)
      })
    • Adds event listeners to an EventTarget and manages their lifecycle within the current effect scope.

      Type Parameters

      • T extends Event

        The type of the event.

      Parameters

      Returns () => void

      A function to remove the event listeners.

      const target = ref<EventTarget | null>(null)

      useEventListener(target, 'custom-event', (event) => {
      console.log('Custom event triggered', event)
      })