{"version":3,"sources":["../../src/use-before-unload/index.ts"],"sourcesContent":["import { useEventListener } from '../use-event-listener'\nimport { useLatest } from '../use-latest'\nimport { useStableFn } from '../use-stable-fn'\n\nimport type { Noop } from '../utils/basic'\n\nexport interface UseBeforeUnloadOptions {\n  /**\n   * Whether to prevent the default behavior of the event, which leads to showing a browser-native message\n   *\n   * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event\n   *\n   * @defaultValue false\n   */\n  preventDefault?: boolean\n}\n\n/**\n * A callback function to run before the page is unloaded\n *\n * @param {BeforeUnloadEvent} event - `BeforeUnloadEvent`, the event object\n * @returns {void} `void`\n */\nexport type UseBeforeUnloadCallback = (event: BeforeUnloadEvent) => void\n\n/**\n *\n * A React Hook to perform actions **before the page is unloaded** (e.g., closing, refreshing the page) and optionally trigger a browser-native confirmation dialog.\n *\n * set `preventDefault` to `true` to show the confirm leave dialog.\n *\n * @param {UseBeforeUnloadCallback} callback - `UseBeforeUnloadCallback`, the callback function to run before the page is unloaded, see {@link UseBeforeUnloadCallback}\n * @param {UseBeforeUnloadOptions} [options={}] - `UseBeforeUnloadOptions`, options to configure the hook, see {@link UseBeforeUnloadOptions}\n * @returns {Noop} `Noop`, a function that clear the event listener, see {@link Noop}\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event | BeforeUnloadEvent - MDN}\n */\nexport function useBeforeUnload(callback: UseBeforeUnloadCallback, options: UseBeforeUnloadOptions = {}): Noop {\n  const { preventDefault = false } = options\n\n  const latest = useLatest({ preventDefault, callback })\n\n  const handler = useStableFn((e: BeforeUnloadEvent) => {\n    const { preventDefault, callback } = latest.current\n\n    // Modern browsers need to call `preventDefault` to show a custom dialog\n    preventDefault && e.preventDefault()\n\n    // Legacy support, e.g. Chrome/Edge < 119, need to set `returnValue` to show a custom dialog\n    if (preventDefault) {\n      e.returnValue = true\n    }\n\n    callback(e)\n\n    // When using `onbeforeunload`, you must return a truthy string like \"Are you sure you want to leave this page?\"\n    // But in `beforeunload` event, you no longer need to return a string\n    // return true\n  })\n\n  return useEventListener(() => window, 'beforeunload', handler, { capture: true })\n}\n"],"names":["useBeforeUnload","callback","options","preventDefault","latest","useLatest","handler","useStableFn","e","current","returnValue","useEventListener","window","capture"],"mappings":";;;;+BAoCgBA;;;eAAAA;;;gCApCiB;yBACP;2BACE;AAkCrB,SAASA,gBAAgBC,QAAiC;QAAEC,UAAAA,iEAAkC,CAAC;IACpG,8BAAmCA,QAA3BC,gBAAAA,sDAAiB;IAEzB,IAAMC,SAASC,IAAAA,oBAAS,EAAC;QAAEF,gBAAAA;QAAgBF,UAAAA;IAAS;IAEpD,IAAMK,UAAUC,IAAAA,wBAAW,EAAC,SAACC;QAC3B,IAAqCJ,kBAAAA,OAAOK,OAAO,EAA3CN,iBAA6BC,gBAA7BD,gBAAgBF,aAAaG,gBAAbH;QAExB,wEAAwE;QACxEE,kBAAkBK,EAAEL,cAAc;QAElC,4FAA4F;QAC5F,IAAIA,gBAAgB;YAClBK,EAAEE,WAAW,GAAG;QAClB;QAEAT,WAASO;IAET,gHAAgH;IAChH,qEAAqE;IACrE,cAAc;IAChB;IAEA,OAAOG,IAAAA,kCAAgB,EAAC;eAAMC;OAAQ,gBAAgBN,SAAS;QAAEO,SAAS;IAAK;AACjF"}