-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpwa-install.js
78 lines (62 loc) · 2.44 KB
/
pwa-install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Detect if the app is already installed
const isAppInstalled = window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone === true;
// DOM elements for the custom install prompt
const installPrompt = document.getElementById('installPrompt');
const installButton = document.createElement('button');
const closeButton = document.createElement('button');
// Add styles and content for buttons
installButton.textContent = 'Install';
installButton.classList.add('styled-button');
installButton.style.backgroundColor = 'var(--accent-light)';
installButton.style.marginRight = '10px';
closeButton.textContent = 'Close';
closeButton.classList.add('styled-button');
closeButton.style.backgroundColor = 'var(--accent-light)';
// Append buttons to the custom prompt
installPrompt.appendChild(installButton);
installPrompt.appendChild(closeButton);
// Variable to hold the deferred install prompt event
let deferredInstallPrompt;
// Function to display the custom install prompt
function showInstallPrompt() {
installPrompt.style.display = 'block'; // Show the custom install prompt
}
// Function to hide the custom install prompt
function hideInstallPrompt() {
installPrompt.style.display = 'none'; // Hide the custom install prompt
}
// Listen for the `beforeinstallprompt` event
window.addEventListener('beforeinstallprompt', (event) => {
// Prevent the default mini-infobar from appearing
event.preventDefault();
// Save the deferred prompt event for later use
deferredInstallPrompt = event;
// If the app is not installed, show the custom prompt
if (!isAppInstalled) {
showInstallPrompt();
}
});
// Handle the install button click
installButton.addEventListener('click', async () => {
if (deferredInstallPrompt) {
// Show the browser's install prompt
deferredInstallPrompt.prompt();
// Wait for the user's response
const choiceResult = await deferredInstallPrompt.userChoice;
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the PWA install prompt');
} else {
console.log('User dismissed the PWA install prompt');
}
// Clear the deferred prompt variable
deferredInstallPrompt = null;
}
// Hide the custom prompt after interaction
hideInstallPrompt();
});
// Handle the close button click
closeButton.addEventListener('click', hideInstallPrompt);
// Optionally, hide the custom install prompt if the app is installed
if (isAppInstalled) {
hideInstallPrompt();
}