0.9.1 • Published 5 years ago

pwa-helpers v0.9.1

Weekly downloads
10,530
License
BSD-3-Clause
Repository
github
Last release
5 years ago

pwa-helpers

Build Status Published on npm

Small helper methods or mixins to help build a PWA, and reduce the boilerplate you might have to write. There are many different ways in which you could write these helpers; use these if you want a simple starting point.

Basic helpers

These are vanilla JavaScript methods that can be used regardless of which frameworks or libraries your application is written in.

router.js

Basic router that calls a callback whenever the location is updated.

Example:

import { installRouter } from 'pwa-helpers/router.js';

installRouter((location) => handleNavigation(location));

For example, if you're using this router in a Redux-connected component, you could dispatch an action in the callback:

import { installRouter } from 'pwa-helpers/router.js';
import { navigate } from '../actions/app.js';

installRouter((location) => store.dispatch(navigate(location)));

If you need to force a navigation to a new location programmatically, you can do so by pushing a new state using the History API, and then manually calling the callback with the new location:

window.history.pushState({}, '', '/new-route');
handleNavigation(window.location);

Optionally, you can use the second argument to read the event that caused the navigation. For example, you may want to scroll to top only after a link click.

installRouter((location, event) => {
  // Only scroll to top on link clicks, not popstate events.
  if (event && event.type === 'click') {
    window.scrollTo(0, 0);
  }
  handleNavigation(location);
});

network.js

Utility method that calls a callback whenever the network connectivity of the app changes. The callback should take a boolean parameter (with true meaning the network is offline, and false meaning online)

Example:

import { installOfflineWatcher } from 'pwa-helpers/network.js';

installOfflineWatcher((offline) => {
  console.log('You are ' + offline ? ' offline' : 'online');
});

metadata.js

Utility method that updates the page's open graph and Twitter card metadata. It takes an object as a parameter with the following properties: title | description | url | image.

If the url is not specified, window.location.href will be used; for all other properties, if they aren't specified, then that metadata field will not be set.

Example (in your top level element or document, or in the router callback):

import { updateMetadata } from 'pwa-helpers/metadata.js';

updateMetadata({
  title: 'My App - view 1',
  description: 'This is my sample app',
  url: window.location.href,
  image: '/assets/view1-hero.png'
});

media-query.js

Utility method that calls a callback whenever a media-query matches in response to the viewport size changing. The callback should take a boolean parameter (with true meaning the media query is matched).

Example:

import { installMediaQueryWatcher } from 'pwa-helpers/media-query.js';

installMediaQueryWatcher(`(min-width: 600px)`, (matches) => {
  console.log(matches ? 'wide screen' : 'narrow sreen');
});

Test helpers

Utility methods to be used inside of testing frameworks, to reduce some testing boilerplate.

axe-report.js

This is an axe-core reporter that returns an Error containing every a11y violation for an element. Use this if you want to include axe-core in automated Mocha tests, etc.

Example (in a Mocha test):

import 'axe-core/axe.min.js';
import { axeReport } from 'pwa-helpers/axe-report.js';

describe('button', function() {
  it('is accessible', function() {
    const button = document.createElement('button');
    button.textContent = 'click this';  // Test should fail without this line.
    return axeReport(button);
  });
});

Redux helpers

These utility methods are useful if your application is using Redux for state management.

connect-mixin.js

This is a JavaScript mixin that you can use to connect a Custom Element base class to a Redux store. The stateChanged(state) method will be called when the state is updated.

Example:

import { connect } from 'pwa-helpers/connect-mixin.js';

class MyElement extends connect(store)(HTMLElement) {
  stateChanged(state) {
    this.textContent = state.data.count.toString();
  }
}

lazy-reducer-enhancer.js

A Redux store enhancer that lets you lazy-install reducers after the store has booted up. Use this if your application lazy-loads routes that are connected to a Redux store.

Example:

import { combineReducers } from 'redux';
import { lazyReducerEnhancer } from 'pwa-helpers/lazy-reducer-enhancer.js';
import someReducer from './reducers/someReducer.js';

export const store = createStore(
  (state, action) => state,
  compose(lazyReducerEnhancer(combineReducers))
);

Then, in your page/element, you can lazy load a specific reducer with:

store.addReducers({
  someReducer
});
fundwave-fund-managementzagenzagen-productionplaytwo-core-cms-sections@cpc/algo-views-identity@cpc/redux-env@cpc/redux-storear-store-sdkzagen-zagenworkspace-appm2-app@m2fw/dialog@m2fw/playground-cli@m2fw/router@m2fw/wysiwyg@m2fw/table@m2fw/eximport@m2fw/m2-app@m2fw/menu-tree@m2fw/redux-manager@m2fw/tooltiplexml-eta-teste15lexml-eta-teste26@infinitebrahmanuniverse/nolb-pw@everything-registry/sub-chunk-2497intrastoreintrastore-thunkintervention-pageslit-element-bootstrapmc-booking-wdiget-nawazish@bewweb/uikit-workshop-ecas@ascenso/crca-redux-feedback@ascenso/crca-redux-firebase@ascenso/crca-redux-landbot@ascenso/crca-redux-loading@ascenso/crca-redux-store@ascenso/crca-redux-update-metadata@ascenso/crca-redux-url-parser@chain-lib/cardano-components@blockcerts/blockcerts-verifier@bolt/uikit-workshop@bolt/core@bolt/core-v3.x@conectate/ct-router@dorgtech/daosmind@heyokaysaturday/test-components@hatiolab/things-shell@jsview/coreeta-carinae-1open-wc-lit-html-basicsmy-video-messageo3-sdkmil-offlinepolymer-3-boilerplatemhva-lugares-appmhva-lugares-crudmicrofront_reference_comp1microfront_reference_comp2microfront_reference_comp_litelementmicrofront_reference_storeprompt-gardenpwa-shellpwa-constructionpwa-authschneider-componentsredux-elementrestu-garzon-virtualunity-core-betastore-microappsubmit-contentx-weather@01ht/ht-elements-catalog@amityeko/rnr-admin@amityeko/rnr-client@lexml/lexml-eta@mad-js-dev/custom-uikit-workshop-mad-js-dev@m2-modules/menu-tree@m2-modules/redux-manager@m2-modules/router@m2-modules/tooltip@m2-modules/wysiwyg@m2-modules/dialog@m2-modules/eximport@m2-modules/table@lit-dashboard/builder@lit-dashboard/lit-dashboard@menucloud/mc-booking-portal@menucloud/mc-takeout-portal@menucloudapp/mc-booking-portal@operato/shell@operato/layout@operato/help@operato/context@patternfly/pfe-tools@ppci-mock/allocation-graph@ppci-mock/custom-card@ppci-mock/custom-dialog@ppci-test/redux@ppci/allocation-graph@ppci/device-selector@ppci/generated-together
0.9.1

5 years ago

0.9.0

6 years ago

0.9.0-pre.4

6 years ago

0.9.0-pre.3

6 years ago

0.9.0-pre.2

6 years ago

0.9.0-pre.1

6 years ago

0.8.4

6 years ago

0.8.4-pre.1

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.0

6 years ago