Created
December 19, 2019 19:32
-
-
Save Legends/a9d99057c3abb5360fa9d311d42307b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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