Skip to content

Declarative way of routing for lit-html powered by pwa-helpers, redux and lit-element

License

Notifications You must be signed in to change notification settings

area73/lit-redux-router

 
 

Repository files navigation

Lit Redux Router

Published on webcomponents.org Build Status Coverage Status Known Vulnerabilities

npm version Dependency Status peerDependency Status devDependency Status

Declarative way of routing for lit-html powered by pwa-helpers, redux and lit-element.

A minimal router solution (~1.3 kb Gzipped) based on the routing approach taken by PWA Starter Kit. More info here: https://polymer.github.io/pwa-starter-kit/configuring-and-personalizing/#routing

Install

yarn add lit-redux-router
yarn add lit-html @polymer/lit-element pwa-helpers redux

Usage

First the router needs to connect to a redux store.

import { LitElement, html } from '@polymer/lit-element';
import { connectRouter } from 'lit-redux-router';
import store from './store.js';

connectRouter(store);

lit-route component can render the components when the path attribute matches.

class MyApp extends LitElement {
  render() {
    return html`
      <div class="app-content">
        <lit-route path="/"><h1>Home</h1></lit-route>
        <lit-route path="/about"><h1>About</h1></lit-route>
      </div>
    `;
  }
}
customElements.define('my-app', MyApp);

Ideally all content would be in a component and can be passed to lit-route in a component attribute.

class AppHome extends LitElement {
  render() {
    return html`<h1>Home</h1>`;
  }
}
customElements.define('app-home', AppHome);

class AppAbout extends LitElement {
  render() {
    return html`<h1>About</h1>`;
  }
}
customElements.define('app-about', AppAbout);

class MyApp extends LitElement {
  render() {
    return html`
      <div class="app-content">
        <lit-route path="/" component="app-home"></lit-route>
        <lit-route path="/about" component="app-about"></lit-route>
      </div>
    `;
  }
}
customElements.define('my-app', MyApp);

lit-route can map path variables and inject them in the provided component.

class AppProduct extends LitElement {
  static get properties() {
    return {
      id: String,
    };
  }

  render() {
    return html`<h1>Product with id: ${this.id}</h1>`;
  }
}
customElements.define('app-product', AppProduct);

class MyApp extends LitElement {
  render() {
    return html`
      <div class="app-content">
        <lit-route path="/products/:id" component="app-product"></lit-route>
      </div>
    `;
  }
}
customElements.define('my-app', MyApp);

When no path attribute is provided to lit-route, it will render when no route matches (404)

class MyApp extends LitElement {
  render() {
    return html`
      <div class="app-content">
        <lit-route path="/"><h1>Home</h1></lit-route>
        <lit-route><h1>404 Not found</h1></lit-route>
      </div>
    `;
  }
}
customElements.define('my-app', MyApp);

To trigger navigation without using a link element, the action navigate can be imported and triggered with the wanted path

import { navigate } from 'lit-redux-router';
import store from './store.js';

class MyApp extends LitElement {
  goTo(path) {
    store.dispatch(navigate(path));
  }

  render() {
    return html`
      <div class="app-content">
        <button @click="${() => this.goTo('/about')}">learn more about us</button>
      </div>
    `;
  }
}
customElements.define('my-app', MyApp);

Check a more comprehensive example in https://github.com/fernandopasik/lit-redux-router/blob/master/demo/

Development

Start server with example and watch mode for building the library

yarn start

Run lint and test tasks

yarn test
yarn lint

Build the library

yarn build

Check the full size of the library

yarn size

Built with

  • regexparam - A tiny (299B) utility that converts route patterns into RegExp
  • lit-html - HTML template literals in JavaScript
  • lit-element - An ultra-light custom element base class with a simple but expressive API
  • pwa-helpers - Small helper methods or mixins to help you build web apps
  • Redux - Predictable state container for JavaScript apps

License

MIT (c) 2018 Fernando Pasik

About

Declarative way of routing for lit-html powered by pwa-helpers, redux and lit-element

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 96.7%
  • JavaScript 3.3%