UNPKG

react-pwa-hazhtech

Version:

A React library for Progressive Web App (PWA) functionality with install prompts and status tracking

3 lines 2.89 kB
import {createContext,useContext,useState,useEffect,useCallback}from'react';import {jsx}from'react/jsx-runtime';var i=createContext({isInstalled:false,isInstallable:false,promptInstall:async()=>null,installPromptEvent:null,isStandalone:false}),E=({children:d,onInstallPromptAvailable:a,onAppInstalled:l})=>{let[n,r]=useState(null),[f,p]=useState(false),[P,c]=useState(false),[w,v]=useState(false);useEffect(()=>{let e=()=>{let m=window.matchMedia("(display-mode: standalone)").matches||window.navigator.standalone||document.referrer.includes("android-app://");v(m),p(m);};e();let t=window.matchMedia("(display-mode: standalone)"),o=()=>e();return t.addEventListener?t.addEventListener("change",o):t.addListener(o),()=>{t.removeEventListener?t.removeEventListener("change",o):t.removeListener(o);}},[]),useEffect(()=>{let e=o=>{o.preventDefault(),r(o),c(true),a?.(o);},t=()=>{p(true),c(false),r(null),l?.();};return window.addEventListener("beforeinstallprompt",e),window.addEventListener("appinstalled",t),()=>{window.removeEventListener("beforeinstallprompt",e),window.removeEventListener("appinstalled",t);}},[a,l]);let I=useCallback(async()=>{if(!n)return console.warn("Install prompt is not available. Make sure the app meets PWA criteria."),null;try{await n.prompt();let e=await n.userChoice;return e.outcome==="accepted"&&(p(!0),c(!1)),r(null),e}catch(e){throw console.error("Error during install prompt:",e),e}},[n]);return jsx(i.Provider,{value:{isInstalled:f,isInstallable:P,promptInstall:I,installPromptEvent:n,isStandalone:w},children:d})},W=async()=>typeof window>"u"?(console.warn("triggerPWAInstall: Not in browser environment"),null):window.matchMedia("(display-mode: standalone)").matches||window.navigator.standalone||document.referrer.includes("android-app://")?(console.warn("triggerPWAInstall: App is already installed"),null):new Promise(a=>{let l=async n=>{n.preventDefault();try{await n.prompt();let r=await n.userChoice;a(r);}catch(r){console.error("Error during install prompt:",r),a(null);}};window.deferredPrompt?l(window.deferredPrompt):(window.addEventListener("beforeinstallprompt",l,{once:true}),setTimeout(()=>{window.removeEventListener("beforeinstallprompt",l),console.warn("triggerPWAInstall: Install prompt not available within timeout"),a(null);},5e3));}),b=()=>typeof window>"u"?false:!(window.matchMedia("(display-mode: standalone)").matches||window.navigator.standalone||document.referrer.includes("android-app://"))&&(window.deferredPrompt!==void 0||"serviceWorker"in navigator),y=()=>typeof window>"u"?false:window.matchMedia("(display-mode: standalone)").matches||window.navigator.standalone||document.referrer.includes("android-app://");var C=()=>useContext(i); export{i as PWAContext,E as PWAProvider,b as isPWAInstallAvailable,y as isPWAMode,W as triggerPWAInstall,C as usePWA};//# sourceMappingURL=index.mjs.map //# sourceMappingURL=index.mjs.map