UsePageLeave
guarahooks/use-page-leaveFreeHook
Detects when the user tries to leave the page.
Install it
npx shadcn@latest add https://guarahooks.com/r/use-page-leave.jsonSource
1'use client';23import { useCallback, useEffect, useRef, useState } from 'react';45type PageLeaveOptions = {6 confirmationMessage?: string;7 showConfirmation?: boolean;8};910type PageLeaveResult = {11 isPageLeft: boolean;12 onPageLeave: (callback: () => void) => void;13};1415export function usePageLeave(options: PageLeaveOptions = {}): PageLeaveResult {16 const { confirmationMessage, showConfirmation = false } = options;1718 // Track if user left the page (switched tabs)19 const [isPageLeft, setIsPageLeft] = useState(false);2021 // Store the callback to execute when user leaves22 const callbackRef = useRef<(() => void) | null>(null);2324 // Method to set the callback25 const onPageLeave = useCallback((callback: () => void) => {26 callbackRef.current = callback;27 }, []);2829 // Handle beforeunload event (page close, refresh, navigate away)30 const handleBeforeUnload = useCallback(31 (event: BeforeUnloadEvent) => {32 if (callbackRef.current) {33 callbackRef.current();34 }3536 if (showConfirmation) {37 event.preventDefault();38 // Custom message (may be ignored by modern browsers)39 event.returnValue = confirmationMessage || '';40 return confirmationMessage || '';41 }42 return undefined;43 },44 [showConfirmation, confirmationMessage],45 );4647 // Handle visibility change (tab switch)48 const handleVisibilityChange = useCallback(() => {49 if (document.visibilityState === 'hidden') {50 setIsPageLeft(true);51 if (callbackRef.current) {52 callbackRef.current();53 }54 } else if (document.visibilityState === 'visible') {55 setIsPageLeft(false);56 }57 }, []);5859 useEffect(() => {60 // Skip in non-browser environments to support SSR61 if (typeof window === 'undefined') return undefined;6263 // Add beforeunload event for page close/refresh64 window.addEventListener('beforeunload', handleBeforeUnload);6566 // Add visibilitychange for tab switching67 document.addEventListener('visibilitychange', handleVisibilityChange);6869 return () => {70 window.removeEventListener('beforeunload', handleBeforeUnload);71 document.removeEventListener('visibilitychange', handleVisibilityChange);72 };73 }, [handleBeforeUnload, handleVisibilityChange]);7475 return { isPageLeft, onPageLeave };76}
Files it writes
More from guarahooks
All 100 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| UseAbortControlleruse-abort-controller | guarahooks | Hooks | Free | no deps | |
| UseArrayStateuse-array-state | guarahooks | Hooks | Free | no deps | |
| UseAxiosuse-axios | guarahooks | Hooks | Free | no deps | |
| UseBatteryStatususe-battery-status | guarahooks | Hooks | Free | no deps | |
| UseBetterAuthuse-better-auth | guarahooks | Hooks | Free | no deps | |
| UseClickOutsideuse-click-outside | guarahooks | Hooks | Free | no deps | |
| UseConfirmuse-confirm | guarahooks | Hooks | Free | no deps | |
| UseCookieuse-cookie | guarahooks | Hooks | Free | no deps |
