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

pure-spa

v1.0.4

Published

Lit based SPA router container

Downloads

7

Readme

PureSPA

A routing Lit container (Light DOM) Base Class for modern Web Component-based SPA apps.

Introduction

PureSPA is a Lit Web Component that renders your SPA pages based on an extensible JavaScript configuration file that contains the routes and each route's responsible logic (the SPA pages).

Main Ingredients

Because PureSPA uses relatively new Browser APIs, polyfills are provided for functionality that is not yet available in some browsers, like Safari and Firefox.

Getting started

  npm i pure-spa --savedev

Create a class that extends PureSPA, and return a router config in the static config property:

import { PureSPA } from "pure-spa";
import { config } from "./my-app-config.js";

customElements.define(
  "my-app",
  class MyApp extends PureSPA {
    /**
     * Set app.config structure
     */
    static get config() {
      return config;
    }
  }
);

Sample config: my-app-config.js

import { PageAbout } from "./pages/page-about";
import { PageHome } from "./pages/page-home";

export const config = {
  routes: {
    "/": {
      name: "Home",
      run: PageHome,
    },
    "/about": {
      run: PageAbout,
    },
  },
};

Rendering the base SPA structure

PureSPA's Lit render() method renders a route based on the page a user navigates to.

import { config } from "./my-app-config";
import { PureSPA } from "pure-spa";
import { html } from "lit";
import { ref, createRef } from "lit/directives/ref.js";

customElements.define(
  "my-app",
  class MyApp extends PureSPA {
    #h1 = createRef();

    static get config() {
      return config;
    }

    render() {
      return html`
        <header>
          <h1 ${ref(this.#h1)}></h1>
        </header>
        <main>${super.render()}</main>
      `;
    }

    firstUpdated() {
      super.firstUpdated();

      this.on("routecomplete", () => {
        this.#h1.value.textContent = this.activeRoute.name;
      });
    }
  }
);

Nested routes

Sub routes are declared as a routes nesting on a route:

 "/program": {
    run: PageProgram,
    routes: {
      "/activation": {
        run: PageProgramActivation,
      },
    },

Capturing route data

In the case of an URLPattern (RegExp) capturing, the captured data will be passed to a Lit property in the page component:

import { PageAbout } from "./pages/page-about";
import { PageHome } from "./pages/page-home";
import { PageProfile } from "./pages/page-profile";

export const config = {
  routes: {
    "/": {
      name: "Home",
      run: PageHome,
    },
    "/about": {
      run: PageAbout,
    },
    "/profile": {
      run: PageProfile,
      routes: {
        "/:selector": {},
      },
    },
  },
};

In this case, the PageProfile class can retrieve the captured route data using the Lit static properties, using routeOrigin:

  static get properties() {
    return {
      profile: { type: Object },
      selector: { type: String, attribute: true, routeOrigin: 'pathname' },
      loading: { type: Boolean },
    };
  }

As you can see in the example above, if you use this subroute syntax, the parent route's configured component will also be triggered for the sub route.