0.0.4 • Published 5 years ago
preact-pwa-install v0.0.4
preact-pwa-install
Prompt users to install your Preact Progessive Web Application(PWA) as a native application on desktop or mobile. Details on requirements can be found here.
Preact 8:
npm i preact-pwa-install
Preact X:
npm i preact-pwa-install@preactx
Installation Example Apps
- Preact 8 PWA (Source)
- Preact X PWA(Source)
Preact X Hook
useInstaller
The useInstaller
hook is available for you to use in your components:
import { h } from 'preact';
import useInstaller from 'preact-pwa-install';
export default function InstallButton(){
const { installPrompt, isStandalone } = useInstaller();
return installPrompt && <a href="#" onclick={installPrompt}>Install as PWA</a>
|| isStandalone && 'PWA is installed!';
}
Preact 8 HOC
installer
Any component that the installer
HOC wraps will be provided with the following props:
import { h } from 'preact';
import { installer } from 'preact-pwa-install';
function InstallButton({ isStandalone, installPrompt }){
return installPrompt && <a href="#" onclick={installPrompt}>Install as PWA</a>
|| isStandalone && 'PWA is installed!';
}
export default installer()(InstallButton);
Functions
isStandalone
You may import isStandalone
to check to see if you are running as a PWA at any time:
import { isStandalone } from 'preact-pwa-install';
let isStandalone = isStandalone(); //true/false
awaitInstallPrompt
The awaitInstallPrompt
function allows you to listen to the browser for permission to prompt the user to install your app. Permission comes in the form of a prompt
function you may call at any time in the future:
import { awaitInstallPrompt } from 'preact-pwa-install';
let cancel = awaitInstallPrompt(prompt =>
prompt().then(success => console.log(
success && 'Successfully installed app as PWA.'
|| 'User abandoned install.'
))
//On a successful install, listening will be stopped automatically.
);
/*
cancel() at any time in the future to stop listening for prompts.
*/