UNPKG

use-pwa

Version:

React hook for PWA installation detection and handling

107 lines (104 loc) 3.24 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { default: () => usePwa, usePwa: () => usePwa }); module.exports = __toCommonJS(index_exports); // src/hooks/use-pwa.ts var import_react = require("react"); var capturedEvent = null; if (typeof window !== "undefined") { window.addEventListener("beforeinstallprompt", (event) => { event.preventDefault(); capturedEvent = event; }); } function usePwa() { const promptEvent = (0, import_react.useRef)(capturedEvent); const [canInstall, setCanInstall] = (0, import_react.useState)(false); const [isInstalled, setIsInstalled] = (0, import_react.useState)(false); const [isSupported, setIsSupported] = (0, import_react.useState)(false); const install = (0, import_react.useCallback)(async () => { if (!promptEvent.current) { return void 0; } await promptEvent.current.prompt(); const choice = await promptEvent.current.userChoice; if (choice.outcome === "accepted") { setCanInstall(false); promptEvent.current = null; capturedEvent = null; } return choice; }, []); (0, import_react.useEffect)(() => { if (capturedEvent) { promptEvent.current = capturedEvent; setCanInstall(true); } const handleBeforeInstallPrompt = (event) => { event.preventDefault(); promptEvent.current = event; capturedEvent = event; setCanInstall(true); }; window.addEventListener("beforeinstallprompt", handleBeforeInstallPrompt); return () => { window.removeEventListener( "beforeinstallprompt", handleBeforeInstallPrompt ); }; }, []); (0, import_react.useEffect)(() => { if (document.referrer.includes("android-app://")) { setIsInstalled(true); return; } const displayModes = ["fullscreen", "standalone", "minimal-ui"]; const isDisplayModePwa = displayModes.some( (mode) => window.matchMedia(`(display-mode: ${mode})`).matches ); if (isDisplayModePwa) { setIsInstalled(true); return; } if (navigator.standalone) { setIsInstalled(true); } }, []); (0, import_react.useEffect)(() => { if ("BeforeInstallPromptEvent" in window) { setIsSupported(true); } }, []); return { canInstall, install, isInstalled, isSupported }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { usePwa });