feat: add don't remind me again option to install prompt for PWA

This commit is contained in:
Linus Rath
2026-04-10 14:20:48 +02:00
parent 734155c939
commit 89d580b90d
+17
View File
@@ -8,12 +8,16 @@ interface BeforeInstallPromptEvent extends Event {
userChoice: Promise<{ outcome: "accepted" | "dismissed" }>; userChoice: Promise<{ outcome: "accepted" | "dismissed" }>;
} }
const DISMISSED_KEY = "pwa-install-dismissed";
export function PWAInstallPrompt() { export function PWAInstallPrompt() {
const [deferredPrompt, setDeferredPrompt] = const [deferredPrompt, setDeferredPrompt] =
useState<BeforeInstallPromptEvent | null>(null); useState<BeforeInstallPromptEvent | null>(null);
const [showPrompt, setShowPrompt] = useState(false); const [showPrompt, setShowPrompt] = useState(false);
useEffect(() => { useEffect(() => {
if (localStorage.getItem(DISMISSED_KEY)) return;
const handler = (e: Event) => { const handler = (e: Event) => {
e.preventDefault(); e.preventDefault();
setDeferredPrompt(e as BeforeInstallPromptEvent); setDeferredPrompt(e as BeforeInstallPromptEvent);
@@ -43,6 +47,11 @@ export function PWAInstallPrompt() {
setShowPrompt(false); setShowPrompt(false);
}; };
const handleDismissForever = () => {
localStorage.setItem(DISMISSED_KEY, "1");
setShowPrompt(false);
};
if (!showPrompt || !deferredPrompt) { if (!showPrompt || !deferredPrompt) {
return null; return null;
} }
@@ -69,6 +78,7 @@ export function PWAInstallPrompt() {
<X className="w-4 h-4" /> <X className="w-4 h-4" />
</button> </button>
</div> </div>
<div className="flex flex-col gap-2">
<div className="flex gap-2"> <div className="flex gap-2">
<button <button
onClick={handleDismiss} onClick={handleDismiss}
@@ -83,6 +93,13 @@ export function PWAInstallPrompt() {
Install Install
</button> </button>
</div> </div>
<button
onClick={handleDismissForever}
className="w-full text-xs text-neutral-400 hover:text-neutral-600 dark:hover:text-neutral-300 transition-colors text-center"
>
Don&apos;t remind me again
</button>
</div>
</div> </div>
); );
} }