Giter Site home page Giter Site logo

yerkopalma / singleton-router Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 78 KB

:anchor: Tiny singleton router for the browser

Home Page: https://www.npmjs.com/package/singleton-router

License: MIT License

HTML 18.50% JavaScript 81.50%
router routers singleton-router frontend

singleton-router's Introduction

singleton-router


Install

$ npm install --save singleton-router

or

<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/bundle.min.js"></script>

Usage

import SingletonRouter from 'singleton-router'

const router = SingletonRouter()
// each view is a function that returns an htmlElement object
router.addRoute('/', HomeView)
// callback is fired after new element (view) is mounted
router.addRoute('/about', AboutView, () => { console.log('Content mounted to DOM') })
router.addRoute('/user/:id', UserView)
// starting route
router.setRoot('/')
// element to mount router, if not set will mount on body element
router.start('#app')

In your html, any clickeable element with an attribute data-route would navigate to the route specified there. So, for example, an anchor tag like <a data-route="/about">about</a> will navigate to the AboutView. Programatic navigation can be done with the router.goToPath function.

API

const router = SingletonRouter([options])

There is a single function exposed, the function returns the instance of the router. The instance is also saved to the global window object as RouterInstance_. It accepts an optional options object, the availaible options are:

  • onRender(currentView, previousView, cb): By default, the router use the replaceChild function to render the view, you can replace it to add animations, or use some html diffing function to improve performance. Notice that the function is ran inside a requestAnimationFrame call, so don't need to include it yourself. Also, when defined, you get a third parameter, that's the callback defined on each addRoute method.
  • onNavClick(path, element): If provided, is executed after any element with the data-route attribute in it. Useful to mark active links in navigation menus.

router.setStore(store)

Set a store container, like redux for example. This store is passed to each view.

router.addRoute(path, view [, callback])

Add routes to the router. The arguments are:

  • path: A string with the path of the route.
  • view: A function that returns an HtmlElement, the function hast two arguments:
    • params: The value of the params for that route.
    • store: the store object set before by router.setStore.
    • callback: Optional function that runs after the view is rendered to the DOM. Note that if you define a onRender function, then you MUST handle yourself this callback.

router.setRoot(path)

Set a starting route

router.goToPath(path [, title])

Programmatically navigates to a route. Optionally add a title for the history api.

router.start([selector])

Start the router. This function receive a selector to mount the app, if none is provided, it will replace and update the body od the document.

Licencia

MIT © Yerko Palma.

singleton-router's People

Contributors

yerkopalma avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

singleton-router's Issues

Improve bundled size

Actual size (37.1 Kb) is too heavy, should be improved.
router cost

options

  • switch minifyjs for butternut -> ~25.38Kb
  • use vanilla ES5 syntax -> ~24.72Kb
  • separate the standalone version -> bundle ~4.18Kb, web.min ~24.74Kb
  • avoid url-pattern dependency

Test the thing!

Test Suite

  • Singleton Pattern
  • Router
    • popstate
    • setStore
    • addRoute
    • setRoot
    • start
    • onPopState
    • getRoute
    • notFound
    • gotToPath
    • manageState
    • requestStateUpdate
  • Route
    • getParams
    • setParams
    • onStart

Support priority in routes

Support these routes

const router = SingletonRouter()

router.addRoute('/', PostsView)
router.addRoute('/posts', PostsView)
router.addRoute('/posts/:post', PostView)
router.addRoute('/authors', AuthorsView)
router.addRoute('/authors/:author', AuthorView)
router.addRoute('/tags', TagsView)
router.addRoute('/tags/:tag', PostsView)
router.addRoute('/search', SearchView)
router.addRoute('/:year/:month/:day', PostsView)
router.setRoot('/')
router.start('#app')

Right now, the getRoute method, returns the first matched route, not the best matched routes, so I need to define some priority in routes.
The above example would render the PostsView for /posts/awesome-post instead of the PostView

New added links don't manage route

When something like

<a data-route="/new" href="#"></a>

is added, it wont have the event listener for new navigation, so it will have default behaviour

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.