-
Notifications
You must be signed in to change notification settings - Fork 5
/
PWAPrompt.vue
127 lines (113 loc) · 2.7 KB
/
PWAPrompt.vue
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<template>
<transition name="fade-up-in" appear>
<div
v-if="shown"
class="pwa-prompt"
:class="[{ shown }]"
>
Add app to home screen?
<button class="install-button" @click="installPWA">
Install!
</button>
<button class="close-button" @click="dismissPrompt">
<span aria-hidden="true">×</span>
<!-- Accessible text, so screen readers don't just read a symbol -->
<span class="sr">Dismiss without installing</span>
</button>
</div>
</transition>
</template>
<script>
export default {
data: () => ({
installEvent: undefined,
shown: false,
}),
beforeMount() {
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault()
this.installEvent = e
this.shown = true
})
},
methods: {
installPWA() {
this.installEvent.prompt()
this.installEvent.userChoice.then((choice) => {
this.dismissPrompt() // Hide the banner once the user's clicked
if (choice.outcome === 'accepted') {
// Do something additional if the user chose to install
} else {
// Do something additional if the user declined
}
})
},
dismissPrompt() {
this.shown = false
}
}
}
</script>
<style scoped lang="scss">
// If repurposing this component, you'll want to update the CSS variables.
.pwa-prompt {
position: fixed;
font-size: 1.25rem;
z-index: 20;
line-height: 1;
bottom: 0;
left: 0;
width: 100vw;
padding: 1rem;
display: flex;
align-items: center;
justify-content: center;
height: 4rem;
background: var(--dark);
color: var(--background);
transform: translateY(0);
margin: 0;
.install-button {
font-size: inherit;
margin: 0 0 0 0.5rem;
padding: 0.25em 0.5em;
background-color: var(--bright);
border: 0;
border-radius: 4px;
line-height: 1;
text-transform: uppercase;
font-weight: 600;
}
.close-button {
position: absolute;
right: 0;
top: -.25rem;
font-size: 3rem;
background: transparent;
border: 0;
padding: 0 0.75rem;
height: 100%;
line-height: 1;
}
// For the accessible text. Most projects have their own .sr-only class or similar in the base styles.
.sr {
position: absolute;
width: 1px;
height: 1px;
left: -100vw;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
border: 0;
}
}
.fade-up-in-enter-active, .fade-up-in-leave-active {
transition: opacity 1s cubic-bezier(0.165, 0.84, 0.44, 1), transform 1s cubic-bezier(0.165, 0.84, 0.44, 1);
transform: translateY(0);
}
.fade-up-in-enter, .fade-up-in-leave-to {
opacity: 0;
transform: translateY(4rem);
}
</style>