1.0.4 • Published 3 years ago

@dotmind/react-use-pwa v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Menu

✋ Disclaimer

This package help to prompt to install and manage Progressive Web App (PWA) installed with React hooks.

Please check if your app have the required criteria before use : web.dev/install-criteria

Want to make a good PWA ? Read this

SSR support ✅

💻 Installation

yarn add @dotmind/react-use-pwa

or

npm i @dotmind/react-use-pwa --save

👷‍♂️ How it's work

usePwa usage

import { useEffect, useCallback  } from 'react';
import { usePwa } from '@dotmind/react-use-pwa';

const App = () => {
  const {
    installPrompt,
    isInstalled,
    isStandalone,
    isOffline,
    canInstall,
  } = usePwa();

  const handleInstallPrompt = useCallback(() => {
    if (canInstall) {
      installPrompt();
    }
  }, [canInstall, installPrompt]);

  if (isOffline) {
    return <p>Please check your network 📶</p>;
  }

  if (!isInstalled || !isStandalone) {
    return (
      <button onClick={handleInstallPrompt}>
        <span>Hey install our app 👋</span>
      </button>
    );
  }

  return (
    <h1>Welcome to our new app 🚀</h1>
  );
};
descriptiontype
installPromptShow install prompt() => Promise<void>
isInstalledIs app installed on deviceboolean
isStandaloneIs app run in standalone modeboolean
isOfflineIs app run in offline modeboolean
canInstallDevice can install appboolean
userChoicePrompt user choice'accepted' \| 'dismissed' \| 'unknow'

usePwaAppSize usage

Choose app launching width and height (only in desktop standalone mode).

import { usePwaAppSize } from '@dotmind/react-use-pwa';

const App = () => {
  usePwaAppSize(400, 560);

  return <AppProvider />;
};
argumentsdescriptionrequireddefault value
widthApp widthfalse800
heightApp heightfalse800
optionsApp optionsfalse{ fixed: false }

App options

optiondescriptiontype
fixedUser can't resize app width & heightboolean

⚡️ Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

🔐 License

MIT