• This repository has been archived on 18/Apr/2023
  • Stars
    star
    440
  • Rank 99,050 (Top 2 %)
  • Language
    TypeScript
  • License
    BSD 3-Clause "New...
  • Created almost 7 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Small helper methods or mixins to help you build web apps.

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
});

More Repositories

1

polymer

Our original Web Component library.
HTML
22,033
star
2

polymer-starter-kit

A starting point for Polymer apps
JavaScript
2,457
star
3

pwa-starter-kit

Starter templates for building full-featured Progressive Web Apps from web components.
JavaScript
2,367
star
4

polymer-bundler

Moved to Polymer/tools monorepo
TypeScript
1,196
star
5

old-docs-site

Old Polymer site. Replaced by these repos: polymer-project.org, polymer-library-docs
HTML
1,022
star
6

shop

The Shop app
JavaScript
990
star
7

designer

Polymer Designer Tool
JavaScript
839
star
8

web-component-tester

Moved to Polymer/tools monorepo
TypeScript
566
star
9

polymer-cli

Moved to Polymer/tools monorepo
JavaScript
498
star
10

tools

Polymer Tools Monorepo
TypeScript
430
star
11

prpl-server

⚠️Maintenance mode⚠️ An HTTP server for Node designed to serve PRPL apps in production.
TypeScript
425
star
12

news

Polymer News (Progress Web App Template)
HTML
263
star
13

polycasts

HTML
220
star
14

polyserve

Moved to Polymer/tools monorepo
TypeScript
192
star
15

polymer-analyzer

Moved to Polymer/tools monorepo
TypeScript
159
star
16

hn-polymer-2

Polymer Hacker News clone
HTML
158
star
17

polymer-modulizer

Moved to https://github.com/Polymer/tools/tree/master/packages/modulizer
HTML
143
star
18

polymer-build

Moved to Polymer/tools monorepo
TypeScript
103
star
19

polymer-decorators

TypeScript decorators for Polymer.
TypeScript
93
star
20

polymer-editor-service

Moved to Polymer/tools monorepo
TypeScript
76
star
21

vscode-plugin

Provides autocompletion, linting, and more for web components.
TypeScript
74
star
22

pwa-starter-kit-hn

TypeScript
70
star
23

lazy-imports

Declarative lazy HTML imports as a behavior.
HTML
63
star
24

polymer-css-build

JavaScript
40
star
25

polymer-linter

Moved to Polymer/tools monorepo
TypeScript
34
star
26

atom-plugin

Provides autocompletion, linting, and more for web components.
JavaScript
32
star
27

polymer-library-docs

Polymer library documentation site.
HTML
23
star
28

gen-typescript-declarations

Moved to Polymer/tools monorepo
TypeScript
21
star
29

wct-local

Moved to Polymer/tools monorepo
TypeScript
21
star
30

dom5

TypeScript
19
star
31

polymer-resin

XSS mitigation for Polymer webcomponents that uses safe html type contracts
JavaScript
18
star
32

wct-sauce

Moved to Polymer/tools monorepo
JavaScript
17
star
33

polymer-project.org

Polymer Project site & blog.
HTML
15
star
34

koa-node-resolve

Koa middleware that transforms Node package specifiers to relative paths
TypeScript
15
star
35

browser-capabilities

Moved to Polymer/tools monorepo
TypeScript
14
star
36

tattoo

Test All The Things Over and Over
TypeScript
12
star
37

koa-esm-transform

Middleware for Koa servers that transforms standard JavaScript modules to AMD modules for use with older browsers that don't support modules natively.
TypeScript
10
star
38

polymer-project-config

Moved to Polymer/tools monorepo
JavaScript
8
star
39

css-select-parse5-adapter

An adapter for the css-select package to allow querying of parse5 generated trees.
TypeScript
8
star
40

polymer-workspaces

Moved to https://github.com/Polymer/tools/tree/master/packages/workspaces
TypeScript
8
star
41

polymer.github.io

HTML
7
star
42

tools-sample-projects

Sample projects that Polymer tools should handle.
HTML
7
star
43

ar-logo

JavaScript
7
star
44

tools-common

Moved to Polymer/tools monorepo
JavaScript
6
star
45

benchmarks

Benchmarks for Polymer project libraries
JavaScript
6
star
46

plylog

A logger for the Polymer CLI toolchain
TypeScript
4
star
47

elements

A collection of elements made by the Polymer team
4
star
48

koa-karma-proxy

Simplified coordination of karma and upstream proxy server using the koa web framework.
TypeScript
4
star
49

mocha-suite-child

Runs mocha test suites defined in HTML documents and include the results as if they are part of the main context.
3
star
50

.allstar

2
star
51

tools-team

Issues for tools in general
2
star
52

polygit

Polygit
TypeScript
1
star
53

.github

1
star