Skip to content

Instantly share code, notes, and snippets.

@Legends
Created December 19, 2019 19:32
Show Gist options
  • Select an option

  • Save Legends/a9d99057c3abb5360fa9d311d42307b6 to your computer and use it in GitHub Desktop.

Select an option

Save Legends/a9d99057c3abb5360fa9d311d42307b6 to your computer and use it in GitHub Desktop.
export function installPrompt() {
let deferredPrompt;
const addBtn = document.querySelector('.install-button');
addBtn.style.display = 'none';
window.addEventListener('beforeinstallprompt', (e) => {
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
// Update UI to notify the user they can add to home screen
addBtn.style.display = 'inline-block';
addBtn.addEventListener('click', (e) => {
// hide our user interface that shows our A2HS button
addBtn.style.display = 'none';
// Show the prompt
deferredPrompt.prompt();
// Wait for the user to respond to the prompt
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the A2HS prompt');
} else {
console.log('User dismissed the A2HS prompt');
}
deferredPrompt = null;
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment