-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw-update-toast.html
87 lines (78 loc) · 2.15 KB
/
sw-update-toast.html
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
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../paper-toast/paper-toast.html">
<dom-module id="sw-update-toast">
<template>
<style>
span {
margin-right: 1em;
}
a {
color: var(--primary-color, orange);
text-decoration: none;
}
</style>
<paper-toast id="toast" duration="[[ duration ]]" opened="[[ opened ]]">
<span>[[ message ]]</span>
<a href on-click="_reload">[[ buttonLabel ]]</a>
</paper-toast>
</template>
<script>
/**
* # Service worker update toast
* `<sw-update-toast>` displays a toast requesting the user to reload the page when a source code update is available.
*
* ## Styling
* * `--primary-color`, link color defaults to orange
*
* @customElement
* @polymer
* @demo demo/index.html
*
*/
class SWUpdateToast extends Polymer.Element {
static get is() { return 'sw-update-toast'; }
static get properties() {
return {
/** message to display */
message: {
type: String,
value: "Site update available!",
},
/** label for reload button */
buttonLabel: {
type: String,
value: "Reload",
},
/** duration the toast should be shown, default 0 to persist. */
duration: {
type: Number,
value: 0,
},
/** open the toast */
opened: {
type: Boolean,
value: false,
}
};
}
ready() {
super.ready();
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.getRegistration().then((registration) => {
if (registration) {
registration.addEventListener('updatefound', () => {
this.$.toast.open();
});
}
});
});
}
}
_reload() {
window.location.reload();
}
}
customElements.define(SWUpdateToast.is, SWUpdateToast);
</script>
</dom-module>