Created
January 7, 2024 11:38
-
-
Save GalindoSVQ/a00d3ee8f7de4b9ba256fa69f9925289 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as React from "react"; | |
| React.useEffectEvent = React.experimental_useEffectEvent; | |
| export default function usePageLeave(cb) { | |
| const onLeave = React.useEffectEvent((event) => { | |
| const to = event.relatedTarget || event.toElement; | |
| if (!to || to.nodeName === "HTML") { | |
| cb(); | |
| } | |
| }); | |
| React.useEffect(() => { | |
| document.addEventListener("mouseout", onLeave); | |
| return () => { | |
| document.removeEventListener("mouseout", onLeave); | |
| }; | |
| }, []); | |
| return {}; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackblitz.com/edit/stackblitz-starters-r1g5qt