Skip to content

Commit c26e0e6

Browse files
authored
Merge pull request #14 from jaulz/fix/use-badge-v2
fix: support Badgin API version 2
2 parents e637d0d + f6666f4 commit c26e0e6

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/badging.ts

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { Value } from './index'
2-
import isPositiveNumber from './isPositiveNumber'
32

43
// 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
65
declare global {
76
interface Window {
87
ExperimentalBadge?: {
98
set: (value?: number) => void
109
clear: () => void
1110
}
1211
}
12+
13+
interface Navigator {
14+
setExperimentalAppBadge?: (value?: number) => void
15+
clearExperimentalAppBadge?: () => void
16+
}
1317
}
1418

1519
let warnedBefore = false
@@ -19,7 +23,7 @@ const warn = () => {
1923
}
2024

2125
// 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) {
2327
return
2428
}
2529

@@ -34,6 +38,19 @@ const current: { mediaQuery: MediaQueryList | null; value: Value } = {
3438
value: 0,
3539
}
3640

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+
3754
export function isAvailable() {
3855
if (!current.mediaQuery) {
3956
current.mediaQuery = window.matchMedia('(display-mode: standalone)')
@@ -46,8 +63,7 @@ export function isAvailable() {
4663

4764
return (
4865
current.mediaQuery.matches &&
49-
'ExperimentalBadge' in window &&
50-
!!window.ExperimentalBadge
66+
(isVersion1Available() || isVersion2Available())
5167
)
5268
}
5369

@@ -61,14 +77,25 @@ export function set(value: Value) {
6177

6278
// 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).
6379
// 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
6689
}
6790

6891
export function clear() {
6992
if (!isAvailable()) {
7093
return
7194
}
7295

73-
window.ExperimentalBadge!.clear()
96+
if (isVersion1Available()) {
97+
window.ExperimentalBadge!.clear()
98+
} else if (isVersion2Available()) {
99+
navigator.clearExperimentalAppBadge!()
100+
}
74101
}

0 commit comments

Comments
 (0)