1.0.0 • Published 4 years ago

add-to-homescreen-react v1.0.0

Weekly downloads
-
License
LGPLv3
Repository
github
Last release
4 years ago

add-to-homescreen-react

LGPLv3 license

A React component providing add-to-home-screen functionality for different platforms and browsers.

What is it?

add-to-homescreen-react allows you to easily inform your user that your React application is installable on the home screen of your mobile phone or on your desktop. It provides a highly configurable React component named AddToHomeScreen that supports different browsers and platforms. Where possible, browser's native add-to-homescreen functionality is used. Where not, a guidance for the user can be shown.

This library is based on the add-to-homescreen project of Chris Love.

Usage

First import the component:

import AddToHomeScreen from 'add-to-homescreen-react';

Then add the component to your app:

<AddToHomeScreen />

This is the simplest way to use this component. There is a wide range of configuration options available you can use to customize the behaviour and design of the component. See section Configuration for the Add-to-home-screen module for a description of all available configuration parameters.

Configuration for the Add-to-home-screen module

Configuration is defined in the addToHomeScreenConfiguration.json file. The following parameters exist and can be customized:

Configuration parameterDescriptionDefault Setting
appIdThe id of the application. It is used as key for the local storage entry. It is recommended to define it specifically for your application.add-to-homescreen-react
debugIndicates the platform that should be simulated for debugging purposes. Overrides browser checks. See section Browser specific prompt dialog configuration for the list of supported platform keys.false
activateLoggingActivate logging to JS console for the module. Defaults to true when debug is not false.false
isModalPrevent further actions until the message is closed.false
isMandatoryUser can't proceed without adding the app to the home screen.false
startAutomaticallyShow the message automatically.true
skipFirstVisitShow message only to returning visitors (i.e. skip the first time the user visits).false
minPageViewsShow message only after minimum number of page views.0
startDelayShow the message after that many seconds from page load.1
lifespanShow the message for that many seconds.15
displayPaceMinutes before the message is shown again (0: display every time).1440 (= 24 hours)
mustShowCustomPromptShow custom prompt for browsers with native add-to-home-screen support too.false
maxDisplayCountAbsolute maximum number of times the message will be shown to the user (0: no limit).0
validLocationList of pages where the message will be shown (array of regular expressions).[]
onInitA function being executed on initialization of the module.null
onShowA function being executed when the message is shown.null
onAddA function being executed when the application is launched the first time from the home screen (guesstimate).null
onInstallA function being executed when 'Install' button has been clicked.null
onCancelA function being executed when 'Cancel' button has been clicked.null
showClassesCSS classes to be added for all supported browsers and platforms to the HTML element specified by the ath-container CSS class (see section Customizing add-to-homescreen prompt for details)['animated', 'd-flex']
showClassCSS class that ensures that an element is shown. At least, it must define the display CSS property.d-flex
hideClassCSS class that ensures that an element is hidden. At least, it must define the display CSS property and set it to hide.d-none
customCriteriaA hook to provide either a custom method or a simple true (= always) or false (= never) value to control when it prompts.null (the same as true)
customPromptContentAllows customization of the custom prompt dialog's content. See section Custom Prompt Content.{}
customPromptElementsAllows definition of your own CSS class for all HTML elements of the custom prompt dialog. See section Customizing add-to-homescreen prompt.
customPromptPlatformDependenciesSee section Browser specific prompt dialog configuration.

Custom Prompt Content

The customPromptContent configuration parameter allows to globally define (for all platforms) the content of the custom prompt dialog. It contains the following customizable parameters:

Configuration parameterDescriptionDefault Setting
titleThe title of the prompt dialog.Install application?
srcThe URL for the logo shown in the prompt dialog. If it is null no logo is shown in the dialog.null
cancelMsgThe text of the prompt dialog's cancel button.Not Now
installMsgThe text of the prompt dialog's install button.Install
guidanceCancelMsgThe text of the guidance dialog's cancel button.Close

Please note that the messages can be defined per platform too. See section Browser specific prompt dialog configuration to learn how.

Browser specific prompt dialog configuration

Some platforms support a native add-to-home-screen dialog, others not. For the second ones the add-to-home-screen custom dialog shows a guide (one or more images) that illustrates how the app can be added to the home screen. The customPromptPlatformDependencies parameter allows to customize this guide. It contains a configuration entry for each of the supported platforms. The following platforms are supported:

PlatformKey
Nativenative
Edge Browseredge
Chromium-based Browserschromium
iPhoneiphone
iPadipad
Firefox Browserfirefox
Samsung Phonessamsung
Opera Browseropera

The configuration entries for each platform contain the following customizable parameters:

Configuration parameterDescription
targetUrlThe URL to a page with full instructions. Can be undefined. If it is defined the current page is replace by the page this URL points to.
showClassesCSS classes to be added to the HTML element specified by the ath-container CSS class (see section Customizing add-to-homescreen prompt for details).
imagesAn array of image definitions. These images represent the installation guide for the user and are shown as soon as the user clicks the install button of the custom prompt dialog.
actionA set of platform-specific button labels for the prompt and guidance dialog.

An image definition consists of the following configuration parameters:

Configuration parameterDescription
srcThe URL to the image.
altThe alternative text for the image.

The action message definition consists of the following configuration parameters:

Configuration parameterDescription
okThe label for the prompt dialog's install button.
cancelThe label for the prompt dialog's cancel button.
guidanceCancelThe label for the guidance dialog's cancel button.

Here is an example for a complete platform entry:

"iphone": {
  targetUrl: undefined,
  showClasses: ["iphone-wrapper", "animated", "fadeIn", "d-block"],
  images: [
    {
      src: "/images/addToHomeScreen/iphone.png",
      alt: "Tap the Share Icon and select Add to home screen entry"
    }
  ],
  action: {
    ok: "Ok",
    cancel: "Cancel",
    guidanceCancel: "Dismiss"
  }
}

Customizing add-to-homescreen prompt

Per default, the AddToHomeScreen component brings a default styling based on a predefined set of CSS classes. You can change this styling by defining your own CSS rules for these classes. If you need more changes or want a complete redesign of the custom prompt dialog, it may be helpful to define a complete new CSS rule set based on your own CSS classes. The customPromptElements configuration parameter allows you to define CSS classes for all HTML elements of the add-to-homescreen custom prompt. It consists of a set of configuration parameters defining these classes. For a better understanding of these parameters, here is the HTML structure of the custom prompt dialog:

<div class="{container}">
    <div class="{banner}">
      <div class="{logoCell}>
        <img class="{logo}"/>
      </div>
      <div class="{titleCell}">
        <div class="{title}"/>
      </div>
      <div class="{cancelButtonCell}">
        <button class="{cancelButton}"/>
      </div>
      <div class="{installButtonCell}">
        <button class="{installButton}"/>
      </div>
    </div>
    <div class="{guidance}">
      <div class="{guidanceImageCell}"/>
      <div class="{cancelButtonCell}">
        <button class="{guidanceCancelButton}"/>
      </div>
    </div>
</div>

The values in brackets ({}) are the configuration keys you can use within the customPromptElements parameter to define or change the CSS classes. The following table describes these keys and their default value.

keyDescriptionDefault CSS class
containerThe <div> element wrapping the whole prompt dialog.ath-container
bannerThe <div> element that defines the prompt dialog. If it is shown the guidance dialog is hidden.ath-banner
logoCellThe cell element that wraps the logo of the prompt dialog.ath-logo-cell
logoThe image tag for the logo (if there is one provided by the configuration) has this CSS class.ath-prompt-logo
titleCellThe cell element that wraps the banner text of the prompt dialog.ath-title-cell
titleThe <div> element containing the banner text.ath-banner-title
cancelButtonCellThe cell element that wraps the 'Cancel' button of the prompt and the guidance dialog.ath-cancel-cell
cancelButtonThe <button> element for the 'Cancel' button of the prompt dialog has this CSS class.btn-cancel
installButtonCellThe cell element that wraps the 'Install' button of the prompt dialog.ath-install-cell
installButtonThe <button> element for the 'Install' button of the prompt dialog has this CSS class.btn-install
guidanceThe <div> element that defines the guidance dialog. If it is shown the prompt dialog is hidden.ath-guidance
guidanceImageCellThe cell element that wraps the guidance image(s). The images are added to this cell dynamically from configuration.ath-guidance-image-cell
guidanceCancelButtonThe <button> element for the 'Cancel' button of the guidance dialog has this CSS class.btn-cancel

Please note, that each of these keys accepts only one CSS class. This is necessary to enable the AddToHomeScreen component to use this CSS class as accessor to the corresponding HTML element. For your CSS ruleset you can add further CSS classes to the elements by the corresponding AddOns-keys that exist for each of the CSS class key in the table above. Here is an overview of these AddOns-Keys and their default values:

keyDefault value
containerAddOnsbanner-bottom-center
bannerAddOns''
logoCellAddOnsath-banner-cell
logoAddOns''
titleCellAddOnsath-banner-cell
titleAddOns''
cancelButtonCellAddOnsath-banner-cell
cancelButtonAddOnsbtn btn-link
installButtonCellAddOnsath-banner-cell
installButtonAddOnsbtn btn-success
guidanceAddOns''
guidanceImageCellAddOnsath-banner-cell
guidanceCancelButtonAddOnsbtn btn-link

There is another configuration key named showClasses that can be used to define CSS classes that are added dynamically to the prompt dialog wrapper (the container) whenever it is shown. This can be done globally (see section Configuration for the Add-to-home-screen module) or per supported browser or platform (see section Browser specific prompt dialog configuration).

Licence

Copyright (c) 2014 Matteo Spinelli, http://cubiq.org/

Copyright (c) 2019 Chris Love, http://love2dev.com/

Copyright (c) 2019 Axel Springer Ideas Engineering GmbH, https://ideas-engineering.io/

This software is subject to the provisions of LPGLv3.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.0.0

4 years ago