Skip to content

Commit 41ab512

Browse files
authored
Add close button to PWA prompt (#1430)
Fix #1392
1 parent c5d6e36 commit 41ab512

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/components/InstallPWA.astro

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
---
2-
32
---
43
<div id="install-pwa" class="hidden">
5-
<p>Add website to home screen and get easy access to the schedule!</p>
6-
<button id="install-btn" class="button-link font-bold text-lg px-4 py-4 rounded-lg inline-flex items-center justify-center leading-4 transition-colors duration-200 not-prose border text-white
7-
bg-primary hover:bg-primary-hover border-transparent">Add to home screen</button>
4+
<div class="flex flex-row items-center justify-between gap-4">
5+
<div class="grid w-full">
6+
<p>Add website to home screen and get easy access to the schedule!</p>
7+
<button id="install-btn" class="button-link font-bold text-lg px-4 py-4 rounded-lg inline-flex items-center justify-center leading-4 transition-colors duration-200 not-prose border text-white m-5
8+
bg-primary hover:bg-primary-hover border-transparent">Add to home screen</button>
9+
</div>
10+
<button id="close-btn" class="text-white hover:text-gray-300 transition-colors duration-200 p-2" aria-label="Close">
11+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
12+
<line x1="18" y1="6" x2="6" y2="18"></line>
13+
<line x1="6" y1="6" x2="18" y2="18"></line>
14+
</svg>
15+
</button>
16+
</div>
817
</div>
9-
1018
<style>
1119
#install-pwa {
1220
position: fixed;
@@ -20,36 +28,44 @@
2028
transition: all 0.3s ease;
2129
z-index: 200;
2230
}
23-
2431
#install-pwa.hidden {
2532
transform: translateY(100%);
2633
opacity: 0;
2734
}
2835
</style>
29-
3036
<script>
3137
function showInstallPrompt() {
3238
const installBanner = document.getElementById('install-pwa') as HTMLElement;
3339
installBanner.classList.remove('hidden');
3440
}
3541

36-
const installBtn = document.getElementById('install-btn') as HTMLElement;
37-
installBtn.addEventListener('click', () => {
42+
function hideInstallPrompt() {
43+
const installBanner = document.getElementById('install-pwa') as HTMLElement;
44+
installBanner.classList.add('hidden');
45+
}
46+
47+
const installBtn = document.getElementById('install-btn') as HTMLElement;
48+
const closeBtn = document.getElementById('close-btn') as HTMLElement;
49+
50+
installBtn.addEventListener('click', () => {
3851
if (deferredPrompt) {
3952
deferredPrompt.prompt();
4053
deferredPrompt.userChoice.then((choiceResult: any) => {
4154
console.log(choiceResult.outcome);
4255
deferredPrompt = null;
56+
hideInstallPrompt();
4357
});
4458
}
4559
});
4660

47-
let deferredPrompt: any;
61+
closeBtn.addEventListener('click', () => {
62+
hideInstallPrompt();
63+
});
4864

65+
let deferredPrompt: any;
4966
window.addEventListener('beforeinstallprompt', (e) => {
5067
e.preventDefault();
5168
deferredPrompt = e;
52-
5369
showInstallPrompt();
5470
});
5571
</script>

0 commit comments

Comments
 (0)