Skip to content

Commit 571df4e

Browse files
fix(web): hide install prompt on desktop
1 parent 7fd7099 commit 571df4e

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

apps/web/components/install-prompt.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,44 @@ interface BeforeInstallPromptEvent extends Event {
88
userChoice: Promise<{ outcome: "accepted" | "dismissed" }>;
99
}
1010

11+
function isMobileInstallSurface() {
12+
if (typeof window === "undefined") return false;
13+
14+
const hasCoarsePointer = window.matchMedia("(pointer: coarse)").matches;
15+
const isNarrowViewport = window.matchMedia("(max-width: 768px)").matches;
16+
const userAgent = navigator.userAgent.toLowerCase();
17+
const isMobileUserAgent = /android|iphone|ipad|ipod|mobile/.test(userAgent);
18+
19+
return (hasCoarsePointer && isNarrowViewport) || isMobileUserAgent;
20+
}
21+
1122
export function InstallPrompt() {
1223
const [deferredPrompt, setDeferredPrompt] =
1324
useState<BeforeInstallPromptEvent | null>(null);
1425
const [dismissed, setDismissed] = useState(false);
26+
const [isMobileSurface, setIsMobileSurface] = useState(false);
1527

1628
const handleBeforeInstall = useCallback((e: Event) => {
29+
if (!isMobileInstallSurface()) {
30+
setDeferredPrompt(null);
31+
return;
32+
}
33+
1734
e.preventDefault();
1835
setDeferredPrompt(e as BeforeInstallPromptEvent);
1936
}, []);
2037

2138
useEffect(() => {
39+
const updateSurface = () => setIsMobileSurface(isMobileInstallSurface());
40+
41+
updateSurface();
42+
window.addEventListener("resize", updateSurface);
2243
window.addEventListener("beforeinstallprompt", handleBeforeInstall);
23-
return () =>
44+
45+
return () => {
46+
window.removeEventListener("resize", updateSurface);
2447
window.removeEventListener("beforeinstallprompt", handleBeforeInstall);
48+
};
2549
}, [handleBeforeInstall]);
2650

2751
const handleInstall = async () => {
@@ -38,7 +62,7 @@ export function InstallPrompt() {
3862
setDismissed(true);
3963
};
4064

41-
if (!deferredPrompt || dismissed) return null;
65+
if (!isMobileSurface || !deferredPrompt || dismissed) return null;
4266

4367
return (
4468
<div

0 commit comments

Comments
 (0)