mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
More DRY code. Also move non-hooks to separate directories. BREAKING CHANGE: all `create*` factories been moved to `factory` subdirectory and in case direct import should be imported like `react-use/esm/factory/createBreakpoint` BREAKING CHANGE: `comps` directory renamed to `component`
26 lines
583 B
TypeScript
26 lines
583 B
TypeScript
import { useEffect } from 'react';
|
|
import { off, on } from './misc/util';
|
|
|
|
const usePageLeave = (onPageLeave, args = []) => {
|
|
useEffect(() => {
|
|
if (!onPageLeave) {
|
|
return;
|
|
}
|
|
|
|
const handler = (event) => {
|
|
event = event ? event : (window.event as any);
|
|
const from = event.relatedTarget || event.toElement;
|
|
if (!from || (from as any).nodeName === 'HTML') {
|
|
onPageLeave();
|
|
}
|
|
};
|
|
|
|
on(document, 'mouseout', handler);
|
|
return () => {
|
|
off(document, 'mouseout', handler);
|
|
};
|
|
}, args);
|
|
};
|
|
|
|
export default usePageLeave;
|