0.0.11 • Published 5 years ago

coffeekraken-s-router-component v0.0.11

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Coffeekraken s-router-component

Table of content

  1. Demo
  2. Install
  3. Get Started
  4. Javascript API
  5. Handler function
  6. Url parameters
  7. Source parameter
  8. Hooks
  9. Lifecycle
  10. Classes
  11. Sugar Web Components Documentation
  12. Browsers support
  13. Code linting
  14. Contribute
  15. Who are Coffeekraken?
  16. Licence

Install

npm install coffeekraken-s-router-component --save

Get Started

First, import the component into your javascript file like so:

import SRouterComponent from "coffeekraken-s-router-component"

Then, register some routes like so:

SRouterComponent.on("/", async (params, source) => {
  // do something here...
})
  .on("/user/:id", async (params, source) => {
    // do something here...
  })
  .listen() // start listening for routes changes

Then simply use it inside your html like so:

<a href="/user/2" is="s-router">Go to profile page</a>

Handler functions

The handler functions are the main concept of this router implementation. Each route have a "path" that is bound to an "handler function".

Here's an example:

SRouterComponent.on("/path", (params, source) => {
  // I'm the handler function of the /path route
}).listen()

Not found handler

You can register a not found handler easily by doing this:

SRouterComponent
  // your routes...
  .notFound((params, source) => {
    // do something on 404
  })
  .listen()

Url parameters

The router allows you to add some parameters in your routes. These parameters are formated like so : :paramName. Here's an example:

<a href="/user/10" is="s-router">Go to profile page</a>

Consider we have clicked on this link:

SRouterComponent.on("/user/:id", (params, source) => {
  console.log(params.id) // will output `10`
}).listen()

The urls support also the * wildcard that you can use like so : /my/*/route

Source parameter

You have maybe see that the route handler function take two parameters. The params that we've seen before and the source. This source param can be of two different types:

  1. When the route is triggered from a click on a s-router link: Will be the HTMLAnchorElement link in question
  2. When the route is trigerred from any other way: Will be window.history

Hooks

On each routes, you can have some hooks configured. Here's the list of available hooks:

  1. before: Run before the actual route handler function. Need to return a promise. Take as parameters the params and the source
  2. after: Run after the actual route handler function. Take as parameters the params and the source
  3. leave: Run when the route is bein leaved. Can return a promise. Take as parameters the params and the source

Here's an example of use:

SRouterComponent.on(
  "/admin",
  (params, source) => {
    // do something to display the admin here...
  },
  {
    before: (params, source) => {
      if (!isUserLogged()) return false // block the user here is he's not logged in
    },
    after: (params, source) => {
      // do something after the admin route has bein displayed
    },
    leave: (params, source) => {
      // clear some things, etc...
    }
  }
).listen()

Each hooks can be marked as async or return simply a Promise. Be careful, use this if you know what you are doing... Introducing asyncronous tasks inside a router can lead to tricky issues to debug.

Generic hooks

Some generic hooks can also been registered. Generic mean that these hooks will be called on every route change. Here's how to register these hooks:

SRouterComponent.hooks({
  before: (params, source) => {
    // do something before every routes.
    // if return false, will stop every routes to work
  },
  after: (params, source) => {
    // do something after every routes.
  }
})

Change route lifecycle

Here's a schema of a route change in order to understand the lifecycle:

Change route lifecycle

You can see that the /home before is run before the /about leave. This mean that if your /home before hook return false, the previous route which is /about won't have his leave hook called

Classes

To make things easy to work with, the router add some classes on each s-router links during his lifecycle. Here's the list of classes:

  1. active: Added on the links that match exactly the current route
  2. active-within: Added on the links that are "parent" of the current route. Ex: /user is parent of /user/12

Browsers support

IE / EdgeFirefoxChromeSafari
IE11+last 2 versionslast 2 versionslast 2 versions

As browsers are automatically updated, we will keep as reference the last two versions of each but this component can work on older ones as well.

The webcomponent API (custom elements, shadowDOM, etc...) is not supported in some older browsers like IE10, etc... In order to make them work, you will need to integrate the corresponding polyfill.

Code linting

This package uses some code linting rules. Here's the list:

  1. StandardJS for javascript files
  2. Stylelint with stylelint-config-standard for scss files

Your commits will not been accepted if the code style is not respected!

Contribute

This is an open source project and will ever be! You are more that welcomed to contribute to his development and make it more awesome every day. To do so, you have several possibilities:

  1. Share the love ❤️
  2. Declare issues
  3. Fix issues
  4. Add features
  5. Build web component

Who are Coffeekraken

We try to be some cool guys that build some cool tools to make our (and yours hopefully) every day life better.

More on who we are

License

The code is available under the MIT license. This mean that you can use, modify, or do whatever you want with it. This mean also that it is shipped to you for free, so don't be a hater and if you find some issues, etc... feel free to contribute instead of sharing your frustrations on social networks like an asshole...