usepwastate
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Use Pwa State

package

A composable to manage a progressive web app's install state

Install

npm install usepwastate
# or
yarn add usepwastate

Api doc

Usage

Initialize

In your app state:

import usePwaState from "usepwastate";

const pwa = usePwaState();

function initState() {
  pwa.init();
}

Run the init function as soon as possible: this will trigger the event listeners relative to the app installed state

Listen for changes

A isAppInstalled persistent nanostore atom is available to watch the state changes

function initState() {
  pwa.init();
  const isTouchDevice = 'ontouchstart' in document.documentElement;
  if (isTouchDevice) {
    // the starting state, stored in localstorage from previous sessions
    let isAppInstalled = pwa.isAppInstalled.get() === "true";
    // listen for changes
    pwa.isAppInstalled.listen((v) => {
      isAppInstalled = v === "true";
      // ...
    })
  }
}

Trigger the pwa install prompt

After running init it is possible to trigger the app install prompt if you detect that the app is not installed:

const hasUserAcceptedToInstall = await pwa.install()

Readme

Keywords

none

Package Sidebar

Install

npm i usepwastate

Weekly Downloads

2

Version

0.0.2

License

MIT

Unpacked Size

11.9 kB

Total Files

10

Last publish

Collaborators

  • synw