1
1
import { Value } from './index'
2
- import isPositiveNumber from './isPositiveNumber'
3
2
4
3
// The actual API can be tracked here: https://github.com/wicg/badging/
5
- // Most likely it will change to: navigator.setAppBadge and navigator.clearAppBadge
4
+ // Most likely it will eventually change to: navigator.setAppBadge and navigator.clearAppBadge
6
5
declare global {
7
6
interface Window {
8
7
ExperimentalBadge ?: {
9
8
set : ( value ?: number ) => void
10
9
clear : ( ) => void
11
10
}
12
11
}
12
+
13
+ interface Navigator {
14
+ setExperimentalAppBadge ?: ( value ?: number ) => void
15
+ clearExperimentalAppBadge ?: ( ) => void
16
+ }
13
17
}
14
18
15
19
let warnedBefore = false
@@ -19,7 +23,7 @@ const warn = () => {
19
23
}
20
24
21
25
// We will only warn the user if the Badging API is not available at all
22
- if ( 'ExperimentalBadge' in window ) {
26
+ if ( 'ExperimentalBadge' in window || 'setExperimentalAppBadge' in navigator ) {
23
27
return
24
28
}
25
29
@@ -34,6 +38,19 @@ const current: { mediaQuery: MediaQueryList | null; value: Value } = {
34
38
value : 0 ,
35
39
}
36
40
41
+ function isVersion1Available ( ) {
42
+ return 'ExperimentalBadge' in window && ! ! window . ExperimentalBadge
43
+ }
44
+
45
+ function isVersion2Available ( ) {
46
+ return (
47
+ 'setExperimentalAppBadge' in navigator &&
48
+ ! ! navigator . setExperimentalAppBadge &&
49
+ 'clearExperimentalAppBadge' in navigator &&
50
+ ! ! navigator . clearExperimentalAppBadge
51
+ )
52
+ }
53
+
37
54
export function isAvailable ( ) {
38
55
if ( ! current . mediaQuery ) {
39
56
current . mediaQuery = window . matchMedia ( '(display-mode: standalone)' )
@@ -46,8 +63,7 @@ export function isAvailable() {
46
63
47
64
return (
48
65
current . mediaQuery . matches &&
49
- 'ExperimentalBadge' in window &&
50
- ! ! window . ExperimentalBadge
66
+ ( isVersion1Available ( ) || isVersion2Available ( ) )
51
67
)
52
68
}
53
69
@@ -61,14 +77,25 @@ export function set(value: Value) {
61
77
62
78
// Sets the badge to contents (an integer), or to "flag" if contents is omitted. If contents is 0, clears the badge for the matching app(s).
63
79
// See details here: https://github.com/WICG/badging/blob/master/explainer.md#the-api
64
- window . ExperimentalBadge ! . set ( value )
65
- return true
80
+ if ( isVersion1Available ( ) ) {
81
+ window . ExperimentalBadge ! . set ( value )
82
+ return true
83
+ } else if ( isVersion2Available ( ) ) {
84
+ navigator . setExperimentalAppBadge ! ( value )
85
+ return true
86
+ }
87
+
88
+ return false
66
89
}
67
90
68
91
export function clear ( ) {
69
92
if ( ! isAvailable ( ) ) {
70
93
return
71
94
}
72
95
73
- window . ExperimentalBadge ! . clear ( )
96
+ if ( isVersion1Available ( ) ) {
97
+ window . ExperimentalBadge ! . clear ( )
98
+ } else if ( isVersion2Available ( ) ) {
99
+ navigator . clearExperimentalAppBadge ! ( )
100
+ }
74
101
}
0 commit comments