|
1 | 1 | "use client";
|
2 | 2 |
|
3 | 3 | import "@khmyznikov/pwa-install";
|
4 |
| -import {useEffect} from "react"; |
| 4 | +import { useEffect } from "react"; |
5 | 5 | export default function PWAInstaller({ ...props }) {
|
6 | 6 | return (
|
7 | 7 | // @ts-ignore
|
8 |
| - <pwa-install {...props}></pwa-install> |
| 8 | + <pwa-install id={`pwa-install`} {...props}></pwa-install> |
9 | 9 | );
|
10 | 10 | }
|
11 | 11 |
|
12 | 12 | export type PWAInstallerMethods = {
|
13 | 13 | install: (force?: boolean) => void;
|
14 |
| - addEventListener: (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => void; |
| 14 | + isListening?: boolean; |
| 15 | + showDialog: (force?: boolean) => void; |
| 16 | + addEventListener: ( |
| 17 | + type: string, |
| 18 | + listener: EventListenerOrEventListenerObject, |
| 19 | + options?: boolean | AddEventListenerOptions, |
| 20 | + ) => void; |
15 | 21 | };
|
16 | 22 |
|
17 | 23 | export function usePWAInstaller() {
|
18 | 24 | const getInstallerElement = (): PWAInstallerMethods =>
|
19 |
| - document.getElementsByTagName( |
20 |
| - "pwa-install", |
21 |
| - )[0] as unknown as PWAInstallerMethods; |
| 25 | + document.getElementById("pwa-install") as unknown as PWAInstallerMethods; |
22 | 26 |
|
23 |
| - useEffect(() => { |
24 |
| - const installer = getInstallerElement(); |
25 |
| - if (installer) { |
26 |
| - // register events |
| 27 | + return { |
| 28 | + install: (force?: boolean) => { |
| 29 | + const installer = getInstallerElement(); |
| 30 | + installer?.showDialog(force); |
27 | 31 |
|
28 |
| - installer.addEventListener("pwa-install-success-event", (e) => { |
29 |
| - console.log("[installer] installation success:", e); |
30 |
| - }); |
| 32 | + console.log( |
| 33 | + `[installer] ${force ? "forced" : "prompted"} installation to:`, |
| 34 | + installer, |
| 35 | + ); |
31 | 36 |
|
32 |
| - installer.addEventListener("pwa-install-fail-event", (e) => { |
33 |
| - console.error("[installer] installation failed:", e); |
34 |
| - }); |
| 37 | + if (installer && !installer.isListening) { |
| 38 | + // register events |
| 39 | + installer.isListening = true; |
35 | 40 |
|
36 |
| - installer.addEventListener("pwa-install-available-event", (e) => { |
37 |
| - console.log("[installer] installation available:", e); |
38 |
| - }); |
| 41 | + installer.addEventListener("pwa-install-success-event", (e) => { |
| 42 | + console.log("[installer] installation success:", e); |
| 43 | + }); |
39 | 44 |
|
40 |
| - installer.addEventListener("pwa-user-choice-result-event", (e) => { |
41 |
| - console.log("[installer] user choice result:", e); |
42 |
| - }); |
| 45 | + installer.addEventListener("pwa-install-fail-event", (e) => { |
| 46 | + console.error("[installer] installation failed:", e); |
| 47 | + }); |
43 | 48 |
|
44 |
| - installer.addEventListener("pwa-install-how-to-event", (e) => { |
45 |
| - console.log("[installer] installation how to:", e); |
46 |
| - }); |
| 49 | + installer.addEventListener("pwa-install-available-event", (e) => { |
| 50 | + console.log("[installer] installation available:", e); |
| 51 | + }); |
47 | 52 |
|
48 |
| - installer.addEventListener("pwa-install-gallery-event", (e) => { |
49 |
| - console.log("[installer] installation gallery:", e); |
50 |
| - }); |
51 |
| - } |
52 |
| - }, []); |
| 53 | + installer.addEventListener("pwa-user-choice-result-event", (e) => { |
| 54 | + console.log("[installer] user choice result:", e); |
| 55 | + }); |
53 | 56 |
|
54 |
| - return { |
55 |
| - install: (force?: boolean) => { |
56 |
| - const installer = getInstallerElement(); |
57 |
| - installer?.install(force); |
| 57 | + installer.addEventListener("pwa-install-how-to-event", (e) => { |
| 58 | + console.log("[installer] installation how to:", e); |
| 59 | + }); |
58 | 60 |
|
59 |
| - console.log(`[installer] ${force ? "forced" : "prompted"} installation to:`, installer); |
| 61 | + installer.addEventListener("pwa-install-gallery-event", (e) => { |
| 62 | + console.log("[installer] installation gallery:", e); |
| 63 | + }); |
| 64 | + } |
60 | 65 | },
|
61 | 66 | };
|
62 | 67 | }
|
0 commit comments