npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lit-redux-router

v0.22.0

Published

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

Downloads

713

Readme

Lit Redux Router

Gzip Bundle Size Build Status Coverage Status Known Vulnerabilities

All Contributors npm version npm downloads Published on webcomponents.org

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

A minimal router solution (~1.7 kb Gzipped) that consist in using lit-route elements and connecting them to the store.

The routing approach is based on the PWA Starter Kit.

Install

Install this library and its peer dependencies

npm i lit-redux-router lit pwa-helpers redux

Usage

Firstly ensure that the redux store has been created with the lazyReducerEnhancer, which allows for reducers to be installed lazily after initial store setup.

import { createStore, compose, combineReducers } from 'redux';
import { reducer } from './reducer';
import { lazyReducerEnhancer } from 'pwa-helpers';

export const store = createStore(reducer, compose(lazyReducerEnhancer(combineReducers)));

Then the router needs to connect to a redux store.

import { LitElement, html } from 'lit';
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. The corresponding active lit-route element will reflect the active attribute.

class MyApp extends LitElement {
  render() {
    return html`
      <div class="app-content">
        <lit-route path="/" active><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 through 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);

To lazy load a component on route change and optionally show a loading component while waiting for the import to resolve

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

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

class MyLoading extends LitElement {
  render() {
    return html`
      <style>
        h1 {
          margin-top: 0;
          margin-bottom: 16px;
        }
      </style>
      <h1>Loading...</h1>
    `;
  }
}

customElements.define('my-loading', MyLoading);

The window will scroll to top by default, to disable add the attribute scrollDisable

<lit-route path="/whatever" component="my-whatever" scrollDisable></lit-route>

To scroll to the route element on load, you can set the scrollIntoViewOptions object in the attribute .scrollOpt

<lit-route
  path="/whatever"
  component="my-whatever"
  .scrollOpt="${{behavior: 'smooth', block:'end', inline:'nearest'}}"
></lit-route>

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

Development

Start server with example and watch mode for building the library

npm run start

Run lint and test tasks

npm run test
npm run lint

Build the library

npm run build

Check the full size of the library

npm run size

Built with

  • regexparam - A tiny utility that converts route patterns into RegExp
  • lit - Lit is a simple library for building fast, lightweight web components
  • pwa-helpers - Small helper methods or mixins to help you build web apps
  • Redux - Predictable state container for JavaScript apps

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT (c) 2018 Fernando Pasik