UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

154 lines (153 loc) 5.25 kB
import * as React from "react"; let currentLocation = typeof window !== "undefined" ? window.location.pathname + window.location.search : ""; let isProceeding = false; let pendingNavigation = null; const blockerCallbacks = /* @__PURE__ */new Map(); let listenersSetup = false; function setupListeners() { if (listenersSetup || typeof window === "undefined") return; listenersSetup = true; currentLocation = window.location.pathname + window.location.search; window.addEventListener("popstate", () => { if (isProceeding) return; const nextLocation = window.location.pathname + window.location.search; for (const [, callbacks] of blockerCallbacks) if (callbacks.shouldBlock()) { pendingNavigation = { previousLocation: currentLocation, nextLocation, historyAction: "pop" }; window.history.go(1); callbacks.onBlock(pendingNavigation); return; } currentLocation = nextLocation; }); const originalPushState = window.history.pushState.bind(window.history); const originalReplaceState = window.history.replaceState.bind(window.history); window.history.pushState = function (state, title, url) { if (isProceeding || !url) return originalPushState(state, title, url); const nextLocation = typeof url === "string" ? url : url.toString(); for (const [, callbacks] of blockerCallbacks) if (callbacks.shouldBlock()) { pendingNavigation = { previousLocation: currentLocation, nextLocation, historyAction: "push" }; callbacks.onBlock(pendingNavigation); return; } currentLocation = nextLocation; return originalPushState(state, title, url); }; window.history.replaceState = function (state, title, url) { if (isProceeding || !url) return originalReplaceState(state, title, url); const nextLocation = typeof url === "string" ? url : url.toString(); for (const [, callbacks] of blockerCallbacks) if (callbacks.shouldBlock()) { pendingNavigation = { previousLocation: currentLocation, nextLocation, historyAction: "replace" }; callbacks.onBlock(pendingNavigation); return; } currentLocation = nextLocation; return originalReplaceState(state, title, url); }; window.addEventListener("beforeunload", event => { for (const [, callbacks] of blockerCallbacks) if (callbacks.shouldBlock()) { event.preventDefault(); event.returnValue = ""; return; } }); } function useBlocker(shouldBlock) { const [state, setState] = React.useState("unblocked"); const [blockedLocation, setBlockedLocation] = React.useState(null); const idRef = React.useRef(null); const shouldBlockRef = React.useRef(shouldBlock); shouldBlockRef.current = shouldBlock; React.useEffect(() => { if (typeof window === "undefined") return; setupListeners(); const id = /* @__PURE__ */Symbol("blocker"); idRef.current = id; blockerCallbacks.set(id, { shouldBlock: () => { const block = shouldBlockRef.current; if (typeof block === "function") return block({ currentLocation, nextLocation: pendingNavigation?.nextLocation || "", historyAction: pendingNavigation?.historyAction || "push" }); return block; }, onBlock: pending => { setBlockedLocation(pending.nextLocation); setState("blocked"); }, onProceed: () => { setState("proceeding"); }, onReset: () => { setState("unblocked"); setBlockedLocation(null); } }); return () => { blockerCallbacks.delete(id); }; }, []); const reset = React.useCallback(() => { pendingNavigation = null; setBlockedLocation(null); setState("unblocked"); }, []); const proceed = React.useCallback(() => { if (!pendingNavigation) return; setState("proceeding"); isProceeding = true; const pending = pendingNavigation; pendingNavigation = null; requestAnimationFrame(() => { if (pending.historyAction === "pop") window.history.back();else if (pending.historyAction === "push") window.history.pushState(null, "", pending.nextLocation);else window.history.replaceState(null, "", pending.nextLocation); currentLocation = pending.nextLocation; requestAnimationFrame(() => { isProceeding = false; setBlockedLocation(null); setState("unblocked"); }); }); }, []); if (state === "unblocked") return { state: "unblocked" }; if (state === "proceeding") return { state: "proceeding", location: blockedLocation }; return { state: "blocked", reset, proceed, location: blockedLocation }; } function checkBlocker(nextLocation, historyAction = "push") { if (typeof window === "undefined") return false; if (isProceeding) return false; for (const [, callbacks] of blockerCallbacks) if (callbacks.shouldBlock()) { pendingNavigation = { previousLocation: currentLocation, nextLocation, historyAction }; callbacks.onBlock(pendingNavigation); return true; } return false; } export { checkBlocker, useBlocker }; //# sourceMappingURL=useBlocker.mjs.map