Retrospring/app/javascript/retrospring/controllers/pwa_badge_controller.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

19 lines
480 B
TypeScript
Raw Normal View History

import { Controller } from '@hotwired/stimulus';
export default class extends Controller<HTMLElement> {
isPwa: boolean;
badgeCapable: boolean;
initialize(): void {
this.isPwa = window.matchMedia('(display-mode: standalone)').matches;
this.badgeCapable = "setAppBadge" in navigator;
}
connect(): void {
if (this.isPwa && this.badgeCapable) {
const count = Number.parseInt(this.element.innerText) || 0;
navigator.setAppBadge(count);
}
}
}