# preact-pwa-install

> Adds PWA install capability to your Preact app.

Latest version **0.0.4** (published 2019-04-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install preact-pwa-install
pnpm add preact-pwa-install
yarn add preact-pwa-install
bun add preact-pwa-install
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2019-04-05 |
| First published | 2019-04-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 24.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Tony MacDonell |
| Maintainers | tmtek |

## Links

- npm: https://www.npmjs.com/package/preact-pwa-install
- Repository: https://github.com/tmtek/preact-pwa-install
- Homepage: https://github.com/tmtek/preact-pwa-install#readme
- Issues: https://github.com/tmtek/preact-pwa-install/issues
- npm.io page: https://npm.io/package/preact-pwa-install

## Dependencies (1)

- [@babel/polyfill](https://npm.io/package/@babel/polyfill.md) ^7.4.0

## Recent versions

- 0.0.4 (latest) — 2019-04-05
- 0.1.2 (preactx) — 2019-04-05
- 0.1.1 — 2019-04-04
- 0.0.3 — 2019-04-04
- 0.1.0 — 2019-04-03
- 0.0.2 — 2019-04-01
- 0.0.1 — 2019-04-01

## README

# preact-pwa-install

```
npm i preact-pwa-install
```

Prompt users to install your Preact Progessive Web Application(PWA) as a native application on desktop or mobile. [More information on the requirements for PWAs can be found here](https://developers.google.com/web/fundamentals/app-install-banners/).



### Installable Example Apps

[Preact 8 PWA](https://nifty-allen-800eb0.netlify.com/) ([Source](https://github.com/tmtek/pwa-install-test))

### Capturing the browser prompt

Before getting into the specifics of the tools on offer in this package, it is important to understand that the prompts the the browser hands off to this library can be given very early in the lifecycle of your application.

It is recommended that you call the following function as early as possible in the lifetime of your app:

```javascript
import { awaitInstallPrompt } from 'preact-pwa-install';

/*
The following function call will start listening 
for prompts from the browser. Prompts will be retained 
so that any calls in the future may utilize them.
*/
cancel = awaitInstallPrompt(); 

//cancel() //stops listening to the browser for prompts.

```



## installer HOC

Any component that the `installer` HOC wraps will be provided with the following props:

```javascript
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);
```

## isStandalone

You may import `isStandalone` to check to see if you are running as a PWA at any time:

```javascript
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:

```javascript
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.
*/

```

This function may be called with no arguments, and doing so will just start listening to the browser for prompts. Prompts that are receieved are retained so that additional calls to `awaitInstallPrompt` will be invoked automatically with the retained prompt.

## reset

The reset function exists to be able to clear any retained prompts and to remove all window listeners used to capture prompts from the browser. 

This function primarily for the purposes of testing. It is recommended that you use cancel functions returned from your `awaitInstallPrompt()` calls instead of turning to `reset()`.

---
_Source: https://npm.io/package/preact-pwa-install · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
