sw-fe/public/pwaInstall.js

39 lines
1.1 KiB
JavaScript

// src/pwaInstall.js
let deferredPrompt = null;
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
console.log('PWA install prompt ready');
});
export function promptInstall() {
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice.then((choice) => {
console.log('Install outcome:', choice.outcome);
deferredPrompt = null;
});
} else {
console.log('No install prompt available');
}
}
// Fonction pour ajouter le bouton dans TopBar (on l'appelle depuis TopBar.jsx)
export function addPwaInstallButton(container) {
const btn = document.createElement('button');
btn.innerHTML = '<i class="fa-solid fa-download"></i> Installer'; // icône + texte
btn.style.marginLeft = '10px';
btn.style.padding = '6px 12px';
btn.style.fontSize = '12px';
btn.style.background = '#0ea5e9';
btn.style.color = 'white';
btn.style.border = 'none';
btn.style.borderRadius = '999px';
btn.style.cursor = 'pointer';
btn.style.display = 'flex';
btn.style.alignItems = 'center';
btn.style.gap = '6px';
btn.onclick = promptInstall;
container.appendChild(btn);
}