|
| 1 | +import React, { Fragment, useEffect, useState } from "react"; |
| 2 | + |
| 3 | +import ShareIcon from "./components/ShareIcon"; |
| 4 | +import HomeScreenIcon from "./components/HomeScreenIcon"; |
| 5 | + |
| 6 | +import "./style.scss"; |
| 7 | + |
| 8 | +export default ({ delay = 1000, title = undefined, copy = undefined }) => { |
| 9 | + const [hasBeenDismissed, setDismissed] = useState( |
| 10 | + localStorage.getItem("hasSeenPWAPrompt") |
| 11 | + ); |
| 12 | + const [isVisible, setVisibility] = useState(!Boolean(delay)); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + if (!hasBeenDismissed && delay) { |
| 16 | + setTimeout(() => setVisibility(true), delay); |
| 17 | + } |
| 18 | + }, []); |
| 19 | + |
| 20 | + if (!hasBeenDismissed) { |
| 21 | + const userAgent = window.navigator.userAgent.toLowerCase(); |
| 22 | + const isiOS = () => { |
| 23 | + return /iphone|ipad|ipod/.test(userAgent); |
| 24 | + }; |
| 25 | + const isInStandaloneMode = () => |
| 26 | + "standalone" in window.navigator && window.navigator.standalone; |
| 27 | + |
| 28 | + if (isiOS() && !isInStandaloneMode()) { |
| 29 | + const dismissPrompt = () => { |
| 30 | + localStorage.setItem("hasSeenPWAPrompt", true); |
| 31 | + setVisibility(false); |
| 32 | + }; |
| 33 | + |
| 34 | + const isiOS13 = /os 13/.test(userAgent); |
| 35 | + |
| 36 | + const visibilityClass = isVisible ? "visible" : "hidden"; |
| 37 | + const iOSClass = isiOS13 ? "modern" : "legacy"; |
| 38 | + |
| 39 | + return ( |
| 40 | + <Fragment> |
| 41 | + <div |
| 42 | + className={`pwa-prompt-overlay ${visibilityClass} ${iOSClass}`} |
| 43 | + aria-label="Close" |
| 44 | + role="button" |
| 45 | + onClick={dismissPrompt} |
| 46 | + /> |
| 47 | + <div |
| 48 | + className={`pwa-prompt ${visibilityClass} ${iOSClass}`} |
| 49 | + aria-describedby="pwa-prompt-description" |
| 50 | + aria-labelledby="pwa-prompt-title" |
| 51 | + role="dialog" |
| 52 | + > |
| 53 | + <div className="pwa-prompt-header"> |
| 54 | + <p id="pwa-prompt-title" className="pwa-prompt-title"> |
| 55 | + {title || `Add to Home Screen`} |
| 56 | + </p> |
| 57 | + <button className="pwa-prompt-cancel" onClick={dismissPrompt}> |
| 58 | + Cancel |
| 59 | + </button> |
| 60 | + </div> |
| 61 | + <div className="pwa-prompt-body"> |
| 62 | + <div className="pwa-prompt-description"> |
| 63 | + <p id="pwa-prompt-description" className="pwa-prompt-copy"> |
| 64 | + {copy || |
| 65 | + `This website has app functionality. Add it to your home screen |
| 66 | + to use it in fullscreen and while offline.`} |
| 67 | + </p> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + <div className="pwa-prompt-instruction"> |
| 71 | + <div className="pwa-prompt-instruction-step"> |
| 72 | + <ShareIcon className="pwa-prompt-share-icon" modern={isiOS13} /> |
| 73 | + <p className="pwa-prompt-copy bold"> |
| 74 | + 1) Press the 'Share' button |
| 75 | + </p> |
| 76 | + </div> |
| 77 | + <div className="pwa-prompt-instruction-step"> |
| 78 | + <HomeScreenIcon |
| 79 | + className="pwa-prompt-home-icon" |
| 80 | + modern={isiOS13} |
| 81 | + /> |
| 82 | + <p className="pwa-prompt-copy bold"> |
| 83 | + 2) Press 'Add to Home Screen' |
| 84 | + </p> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + </Fragment> |
| 89 | + ); |
| 90 | + } else { |
| 91 | + localStorage.setItem("hasSeenPWAPrompt", true); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return null; |
| 96 | +}; |
0 commit comments