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

@n0n3br/svelte-hash-spa-router

v0.0.1

Published

A schema based spa (single page application) router built for Svelte3.

Downloads

3

Readme

svelte-hash-spa-router

A schema based spa (single page application) router built for Svelte3.

The package exports 4 constants:

  • Router
  • Link
  • navigate
  • store

Why hash and not history ?

Hash mode is better suited por static single page application, since it doesn't require any server special configuration. Users can share the link or refresh the page and the router won't get lost.

Get started

Look at the example folder in the project github for a real life example including all the features provided

Install

# you can use npm
npm install --save @n0n3br/svelte-hash-spa-router
# or yarn
yarn add @n0n3br/svelte-hash-spa-router

Usage

<!-- App.svelte -->
<script>
  import { Router, Link } from '@n0n3br/svelte-hash-spa-router';
  import Home from './Home.svelte';
  import About from './About.svelte';
  import Redirect from './Redirect.svelte';

  const routes = [
    { name: 'home', path: '/', component: Home},
    { name: 'about', path: '/about', component: About},
    // redirect route won't render because of the beforeRouteEnter guard
    { name: 'redirec', path: '/redirect', component: Redirect, beforeRouteEnter: navigate => navigate('home', {}, {}) }
  ]

  const linkConfig = {
    activeClass: 'is-active', // class applied when the link is active
    class: 'nav-link' // class applied to the a element of the link
  }
</script>
<main>
  <ul>
    <li><Link to='home' config={linkConfig}>Home</Link></li>
    <li><Link to='about' config={linkConfig}>About</Link></li>
    <li><Link to='redirec' config={linkConfig}>Redirect</Link></li>
  </ul>
  <Router {routes} />
</main>

API

Router

Provides the router container that will choose wich component to render based on the browser url.

Properties

| Property | Required | Default Value | Description | | :------: | :------: | :-----------: | :----------------------------------------------------------------------------------------------------------------------------------------------------- | | routes | ✔ | [ ] | An array that describes the routes. Each route is composed of a name, a path (/main for example), a component and an optional beforeRouteEnter guard |

The router will export route (the active route path and name), params (url params with name and value), and query (query params with name and value) to the rendered route. All you need to do is put a export let route, params, query in the script portion of your component.

route

Each individual route in the routes array of the Router component

Properties

| Property | Required | Default Value | Description | | :----------------: | :------: | :-----------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | name | ✔ | '' | A string to name the route. The name can be used in the Link component or in the navigate function | | path | ✔ | '' | A string that describe the path of the route. To insert parameters passed by the url in the path, prepend their name with : (/post/:id for example). You can have as many parameters as you want in the path | | component | ✔ | null | The Svelte component that will be rendered when the route path matches the navigator url | | beforeRouteEnter | | true | A function that can be used as a guard to prevent the route render. The function receives the navigate method, making possible a redirection to another route if needed. If the function returns false, the route will not be rendered. If the function returns the redirect method, the router will render the passed route (for example redirect('main') or redirec('')). Any other returned value will make the route render. |

Link

Provides an easy way to render a link to a route. The rendered element will be an A.

Properties

| Property | Required | Default Value | Description | | :------: | :------: | :---------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------- | | to | ✔ | '' | The name of the route that will be rendered when the link is clicked. | | params | | { } | An object with the URL params that should be applied to the route path. The name of the parameters must match the route definition | | query | | { } | An object with the query params that should be applied to the route path. | | config | | { class: '', activeClass: 'active'} | An object with class and activeClass string parameteres. | | match | | null | A regular expression that can be used along with route path and name to determine if the link should have the active class |

navigate

A method that can be used to do programatic navigation. It can have 3 parameters: to, params and query. Examples:

  • navigate('main') will navigate to the main route
  • navigate('posts', { id: 1 }) will navigate to /posts/1 assuming the route posts has path '/posts/:id'
  • navigate('about',{}, { mode: 'development'}) will navigate to '/about?mode=development'
Properties

| Property | Required | Default Value | Description | | :------: | :------: | :-----------: | :--------------------------------------------------------------------------------------------------------------------------------- | | to | ✔ | '' | The name of the route that will be rendered when the link is clicked. | | params | | { } | An object with the URL params that should be applied to the route path. The name of the parameters must match the route definition | | query | | { } | An object with the query params that should be applied to the route path. |

store

A subscribable Svelte Store without set and update methods. It provides access to 4 parameters:

  • routes: the routes configured in the Router element
  • route: the path and name of the active route
  • params: url params of the current route
  • query: query params of the active route

You can use the store to have access to the router information in any component, including the ones that are not children of the router.

You can subscribe to it's updates like any other Svelte store:

import { store } from "@n0n3br/svelte-hash-spa-router";
let routes, route, params, query;
store.subscribe((state) => {
  ({ router, route, params, query } = state);
});