Skip to content

⬅️ Back to Table of Contents

📄 usePageLeave

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 5
📐 Interfaces 1
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/core/usePageLeave/index.ts

📦 Imports

Name Source
ShallowRef vue
ConfigurableWindow ../_configurable
shallowRef vue
defaultWindow ../_configurable
useEventListener ../useEventListener

Functions

usePageLeave(options: UsePageLeaveOptions): UsePageLeaveReturn

Reactive state to show whether mouse leaves the page.

Parameters:

  • options any: No description

See: https://vueuse.org/usePageLeave

Tags: @__NO_SIDE_EFFECTS__

Raw JSDoc
/**
 * Reactive state to show whether mouse leaves the page.
 *
 * @see https://vueuse.org/usePageLeave
 * @param options
 *
 * @__NO_SIDE_EFFECTS__
 */

Calls:

  • shallowRef (from vue)
  • useEventListener (from ../useEventListener)

Internal Comments:

// @ts-expect-error missing types (x2)

Code
export function usePageLeave(options: UsePageLeaveOptions = {}): UsePageLeaveReturn {
  const { window = defaultWindow } = options
  const isLeft = shallowRef(false)

  const handler = (event: MouseEvent) => {
    if (!window)
      return

    event = event || (window.event as any)
    // @ts-expect-error missing types
    const from = event.relatedTarget || event.toElement
    isLeft.value = !from
  }

  if (window) {
    const listenerOptions = { passive: true }
    useEventListener(window, 'mouseout', handler, listenerOptions)
    useEventListener(window.document, 'mouseleave', handler, listenerOptions)
    useEventListener(window.document, 'mouseenter', handler, listenerOptions)
  }

  return isLeft
}

Internal helpers

Declared inside another function in this file.

handler(event: MouseEvent): void

Parameters:

  • event MouseEvent

Returns: void

Internal Comments:

// @ts-expect-error missing types (x2)

Code
(event: MouseEvent) => {
    if (!window)
      return

    event = event || (window.event as any)
    // @ts-expect-error missing types
    const from = event.relatedTarget || event.toElement
    isLeft.value = !from
  }

Interfaces

UsePageLeaveOptions

Interface Code
export interface UsePageLeaveOptions extends ConfigurableWindow {
}

Type Aliases

UsePageLeaveReturn

type UsePageLeaveReturn = ShallowRef<boolean>;

Generated by Syntax Scribe