usePageLeave

Detect when the mouse leaves the page.

https://www.shaktools.com/shak-hooks
Sensor

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { usePageLeave } from "@shak-hooks/usehooks";
Source: packages/react/src/usePageLeave.ts

Signature

signature.ts
export function usePageLeave(onLeave: () => void)

Parameters

NameTypeOptionalDefault
onLeave() => voidNo-

Returns

unknown

Implementation

use-page-leave.ts
1import { useEffect } from "react";
2
3export function usePageLeave(onPageLeave: () => void) {
4 useEffect(() => {
5 const handleMouseLeave = (event: MouseEvent) => {
6 if (event.clientY <= 0) {
7 onPageLeave();
8 }
9 };
10
11 document.addEventListener("mouseleave", handleMouseLeave);
12 return () => {
13 document.removeEventListener("mouseleave", handleMouseLeave);
14 };
15 }, [onPageLeave]);
16}

Advertisement

Google Ads

Usage

example.tsx
1import { usePageLeave } from "@shak-hooks/usehooks";
2
3const result = usePageLeave(undefined);
4// result: unknown
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy