Skip to content

Commit

Permalink
fix: πŸ› make sure all paths in usePageLeave return
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 27, 2019
1 parent 33ac91b commit 6655092
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/usePageLeave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import {useEffect} from 'react';

const usePageLeave = (onPageLeave, args = []) => {
useEffect(() => {
if (onPageLeave) {
const handler = (event) => {
event = event ? event : (window.event as any);
const from = event.relatedTarget || event.toElement;
if (!from || (from as any).nodeName === 'HTML') onPageLeave();
};
if (!onPageLeave) return;

document.addEventListener('mouseout', handler);
return () => {
document.removeEventListener('mouseout', handler);
};
}
const handler = (event) => {
event = event ? event : (window.event as any);
const from = event.relatedTarget || event.toElement;
if (!from || (from as any).nodeName === 'HTML') onPageLeave();
};

document.addEventListener('mouseout', handler);
return () => {
document.removeEventListener('mouseout', handler);
};
}, args);
};

Expand Down

0 comments on commit 6655092

Please sign in to comment.