Skip to content

Commit e070766

Browse files
Merge pull request #230 from rupeshhh007/staging
feat: add PWA install button
2 parents 4033edf + bdcd0df commit e070766

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

public/papers_logo.png

82.7 KB
Loading

src/components/screens/Info.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import man from "@/assets/man.svg" assert { type: "image/svg" };
44
import man1 from "@/assets/man1.svg" assert { type: "image/svg" };
55
import Link from "next/link";
66
import { Button } from "@/components/ui/button";
7+
import PWAInstallButton from "../ui/PWAInstallButton";
8+
79

810
function Info() {
911
return (
@@ -66,6 +68,9 @@ function Info() {
6668
</Button>
6769
</Link>
6870
</section>
71+
<div className="flex justify-center py-4 z-50">
72+
<PWAInstallButton />
73+
</div>
6974
</>
7075
);
7176
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
"use client";
3+
4+
import { useEffect, useState } from "react";
5+
import Image from "next/image";
6+
import { Download } from "lucide-react";
7+
8+
9+
interface BeforeInstallPromptEvent extends Event {
10+
prompt: () => Promise<void>;
11+
userChoice: Promise<{
12+
outcome: "accepted" | "dismissed";
13+
platform: string;
14+
}>;
15+
}
16+
17+
const PWAInstallButton = () => {
18+
const [deferredPrompt, setDeferredPrompt] = useState<BeforeInstallPromptEvent | null>(null);
19+
const [showInstall, setShowInstall] = useState(false);
20+
21+
useEffect(() => {
22+
const handler = (e: Event) => {
23+
e.preventDefault();
24+
setDeferredPrompt(e as BeforeInstallPromptEvent);
25+
setShowInstall(true);
26+
};
27+
28+
window.addEventListener("beforeinstallprompt", handler);
29+
return () => window.removeEventListener("beforeinstallprompt", handler);
30+
}, []);
31+
32+
const handleInstall = async () => {
33+
if (!deferredPrompt) return;
34+
35+
deferredPrompt.prompt();
36+
const { outcome } = await deferredPrompt.userChoice;
37+
if (outcome === "accepted") {
38+
setShowInstall(false);
39+
setDeferredPrompt(null);
40+
}
41+
};
42+
43+
if (!showInstall) return null;
44+
45+
return (
46+
<div className="md:hidden flex items-center justify-between rounded-full bg-[#2b2343] px-4 py-2 shadow-md text-white w-fit">
47+
<div className="flex items-center gap-3">
48+
<Image src="/papers_logo.png" alt="Papers App" width={32} height={32} />
49+
<span className="font-semibold text-lg">Papers App</span>
50+
</div>
51+
<button
52+
onClick={handleInstall}
53+
className="ml-6 flex items-center gap-2 rounded-full border border-gray-500 bg-[#1e1e24] px-4 py-1 text-sm font-semibold transition hover:bg-[#2b2b30]"
54+
>
55+
<Download className="h-4 w-4" />
56+
Install
57+
</button>
58+
</div>
59+
);
60+
};
61+
62+
export default PWAInstallButton;

0 commit comments

Comments
 (0)