npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-pwa-install-updated

v1.1.0

Published

Install support for your PWA on several platforms

Downloads

4

Readme

react-pwa-install

npm package npm

Install handling for your React PWA.

The problem this package is trying to solve

Since intalling a PWA (aka Add to home screen) is handled differently by different browsers, it's rather hard to give this option to users conveniently. Currently only Chrome, Edge and Samsung Internet support this feature natively using the BeforeInstallPromptEvent. On iOS devices, FireFox mobile or Opera mobile the user needs to perform some manual steps to install the app to the home screen. Very few know this option even exists.

What this package does

This package provides a simple way to add a custom PWA installing capability to your app on several platforms, while not trying to do too much in terms of when and how or how often the install prompt is shown to the user. This is application dependent and should be left for the app developer to define.

  • If a user is visiting your PWA from a browser that supports the native BeforeInstallPromptEvent, then this method will be used for the installation. ( Currently: Chrome - mobile and desktop, Edge - mobile, Samsung internet )
  • If a user is visiting your PWA from a browser that supports "manual installation", then instructions will be shown to them. ( Currently iOS devices, FireFox - mobile, Opera - mobile).

Demo

To view the demo visit: https://zoltangy.github.io/react-pwa-install-demo/ (view source code).

Installation

To use the package, you also need to install @material-ui/core.

$ npm i --save react-pwa-install @material-ui/core

Prerequisites

Make sure that your PWA is installable. You can use Lighthouse from Chrome DevTools to run a PWA Audit.

Please also read this guide on best practices to promote the installation of your app.

Usage

  1. Import ReactPWAInstallProvider and useReactPWAInstall
  2. Wrap your App with the ReactPWAInstallProvider.
  3. Use the supported and isInstalled (desctructed from useReactPWAInstall) functions to determine whether to show your Install button (or banner,popup,etc...). Generally, the install button should only be shown if supported returns true and isInstalled returns false. (You should add the app specific criterias on top of this: amount of time spent on the site, certain engagement, etc.)
  4. Call pwaInstall (desctructed from useReactPWAInstall) when the user clicked on the install button. See the list of options below. You might want to handle rejection (or possible rejection) here, and not prompt the user again for a certain period of time or until a new meaningful engagement.
import React from "react";
import ReactDOM from "react-dom";
import ReactPWAInstallProvider, { useReactPWAInstall } from "react-pwa-install";
import myLogo from "img/logo.png";

function App() {
  const { pwaInstall, supported, isInstalled } = useReactPWAInstall();

  const handleClick = () => {
    pwaInstall({
      title: "Install Web App",
      logo: myLogo,
      features: (
        <ul>
          <li>Cool feature 1</li>
          <li>Cool feature 2</li>
          <li>Even cooler feature</li>
          <li>Works offline</li>
        </ul>
      ),
      description: "This is a very good app that does a lot of useful stuff. ",
    })
      .then(() => alert("App installed successfully or instructions for install shown"))
      .catch(() => alert("User opted out from installing"));
  };

  return (
    <div>
      {supported() && !isInstalled() && (
        <button type="button" onClick={handleClick}>
          Install App
        </button>
      )}
    </div>
  );
}

ReactDOM.render(
  <ReactPWAInstallProvider enableLogging>
    <App />
  </ReactPWAInstallProvider>,
  document.querySelector("#root")
);

API

  • ReactPWAInstallProvider: Context provider, required. For debug purposes, the enableLogging property can be used.
  • useReactPWAInstall: React hook that provides pwaInstall, supported, isInstalled
  • supported: Helper function to decide if the install button should be shown to the user. Returns true in 2 cases:
    • the beforeinstallprompt event is supported, and it has fired
    • manual installation is supported
  • isInstalled: Helper function to decide if the install button should be shown to the user. Returns true if the user is currently visiting the site from the installed 'standalone' app.
  • pwaInstall: Will open the installation dialog according to the user's platform. See below for options. This function returns a promise. The promise is rejected only if the user cancels the native installation process. The promise is fulfilled when the native installation was successful or when the manual installation steps were shown.

Options for pwaInstall

The following options are available to customize the install dialog:

| Prop | type | Default Value | Description | | ------------------------ | --------------- | --------------- | --------------------------------------------------------- | | title (optional) | string | Install Web App | The header of the dialog window | | logo (optional) | string | - | Path to an image representing the app, typically the icon | | features (optional) | React.ReactNode | - | A list of features, see "Usage" for an example | | description (optional) | string | - | Description of the app |

Customization

To customize the colors and fonts of the dialog, you can wrap the ReactPWAInstallProvider with a Material-UI ThemeProvider. See https://material-ui.com/customization/theming/ for more info.