sw-fe/public/pwaInstall.js

47 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// src/pwaInstall.js
let deferredPrompt = null;
window.addEventListener('beforeinstallprompt', (e) => {
// Empêche le prompt par défaut
e.preventDefault();
deferredPrompt = e;
console.log('PWA install prompt ready');
});
// Fonction pour déclencher linstall (tu peux lappeler sur un bouton)
export function promptInstall() {
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('PWA installed');
} else {
console.log('PWA dismissed');
}
deferredPrompt = null;
});
}
}
// Ajoute un bouton "Installer lapp" dans ton UI (optionnel)
export function addInstallButton() {
const btn = document.createElement('button');
btn.textContent = 'Installer SocioWire';
btn.style.position = 'fixed';
btn.style.bottom = '20px';
btn.style.right = '20px';
btn.style.padding = '10px 20px';
btn.style.background = '#0ea5e9';
btn.style.color = 'white';
btn.style.border = 'none';
btn.style.borderRadius = '8px';
btn.style.cursor = 'pointer';
btn.style.zIndex = '9999';
btn.onclick = promptInstall;
document.body.appendChild(btn);
}
// Détection si déjà installé (cache)
if (window.matchMedia('(display-mode: standalone)').matches || navigator.standalone) {
console.log('App already installed');
}