All Projects → yoshuawuyts → Sheet Router

yoshuawuyts / Sheet Router

Licence: mit
fast, modular client-side router

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Sheet Router

Navaid
A navigation aid (aka, router) for the browser in 850 bytes~!
Stars: ✭ 648 (+195.89%)
Mutual labels:  router, history, browser
Frontexpress
An Express.js-Style router for the front-end
Stars: ✭ 263 (+20.09%)
Mutual labels:  router, history, browser
Front End Resource Collection
前后端日常总结
Stars: ✭ 82 (-62.56%)
Mutual labels:  browser, html5
Battery.js
A tiny wrapper for the HTML5 Battery Status API.
Stars: ✭ 111 (-49.32%)
Mutual labels:  browser, html5
Foxify
The fast, easy to use & typescript ready web framework for Node.js
Stars: ✭ 138 (-36.99%)
Mutual labels:  router, fast
Ip Kvm Interface
DIY IP-KVM for Remote Desktop Access
Stars: ✭ 62 (-71.69%)
Mutual labels:  browser, html5
Route
原生 js 实现的轻量级路由,且页面跳转间有缓存功能。
Stars: ✭ 68 (-68.95%)
Mutual labels:  router, history
Skia Wasm Port
Port of the Skia drawing library to wasm, for use in javascript (node & browser)
Stars: ✭ 131 (-40.18%)
Mutual labels:  browser, html5
Lion
Lion is a fast HTTP router for building modern scalable modular REST APIs in Go
Stars: ✭ 750 (+242.47%)
Mutual labels:  router, fast
Layerjs
layerJS: Javascript UI composition framework
Stars: ✭ 1,825 (+733.33%)
Mutual labels:  composition, html5
Lazy
Kule Lazy4 / CSS Framework
Stars: ✭ 147 (-32.88%)
Mutual labels:  browser, html5
Redux Saga Router
A router for Redux Saga
Stars: ✭ 153 (-30.14%)
Mutual labels:  router, history
Svelte Store Router
Store-based router for Svelte
Stars: ✭ 54 (-75.34%)
Mutual labels:  router, browser
Minipaint
online image editor
Stars: ✭ 1,014 (+363.01%)
Mutual labels:  browser, html5
Noduino
JavaScript and Node.js Framework for controlling Arduino with HTML and WebSockets
Stars: ✭ 1,202 (+448.86%)
Mutual labels:  browser, html5
Bugz
🐛 Composable User Agent Detection using Ramda
Stars: ✭ 15 (-93.15%)
Mutual labels:  composition, browser
Fast
Develop, build, deploy, redeploy, and teardown frontend projects fast.
Stars: ✭ 126 (-42.47%)
Mutual labels:  fast, html5
Webworldwind
The NASA WorldWind Javascript SDK (WebWW) includes the library and examples for creating geo-browser web applications and for embedding a 3D globe in HTML5 web pages.
Stars: ✭ 628 (+186.76%)
Mutual labels:  browser, html5
Redux First History
🎉 Redux First History - Redux history binding support react-router - @reach/router - wouter
Stars: ✭ 163 (-25.57%)
Mutual labels:  router, history
React Border Wrapper
A wrapper for placing elements along div borders.
Stars: ✭ 147 (-32.88%)
Mutual labels:  browser, html5

sheet-router stability

npm version build status test coverage downloads js-standard-style

sheet-router is a fast, modular client-side router. It enables view composition and is tuned for performance by statically declaring routes in a radix-trie. Weighs 1.5KB minified and gzipped.

Installation

$ npm install sheet-router

Features

  • View composition through functions
  • Tuned for performance by generating a radix-trie
  • Not bound to any framework
  • Minimal dependencies and tiny code size
  • HTML5 history support
  • Catch and handle <a href=""> links

Usage

sheet-router tries to make routing understandable and pleasant to work with. It does so by using a lisp-like structure which is internally compiled to an efficient data structure. Here each route takes either an array of children or a callback, which are then translated to paths that take callbacks

const sheetRouter = require('sheet-router')
const html = require('bel')

// default to `/404` if no path matches
const router = sheetRouter({ default: '/404' }, [
  ['/', (params) => html`<div>Welcome to router land!</div>`],
  ['/:username', (params) => html`<div>${params.username}</div>`, [
    ['/orgs', (params) => html`<div>${params.username}'s orgs!</div>`]
  ]],
  ['/404', (params) => html`<div>Oh no, path not found!</div>`],
])

router('/hughsk/orgs')

history

Interacting with the browser history is a common action, sheet-router supports this out of the box. When the forwards or backwards buttons in the browser are clicked, or history.back / history.go are called sheet-router will update accordingly.

const history = require('sheet-router/history')
history(function (href) {
  router(href)
  console.log('history changed: ' + href)
})

hash

Interacting with hash changes is often a common fallback scenario for those who don't have support for browser history. Whenever a hashchange event is triggered, sheet-router will trigger an update as seen below. However in order to match hash prefixed routes, the hash-match module can be used to normalize routes (ex: #/foo becomes /foo).

const hash = require('sheet-router/hash')
const match = require('hash-match')
hash(function (href) {
  router(match(href))
  console.log('hash location changed: ' + href)
})

href

In HTML links are represented with <a href=""> style tags. Sheet-router can be smart about these and handle them globally. This way there's no need to attach specific listeners to each link and static HTML templates can be upgraded seemlessly to include single-page routing.

const href = require('sheet-router/href')
href(function (href) {
  router(href)
  console.log('link was clicked: ' + href)
})

You can ignore specific links that you do not want to process through routing by adding the data-no-routing attribute.

<a href="/my-external-link" data-no-routing>Non routed link</a>
<a href="/another-external-link" data-no-routing="true">Not routed either</a>

Also, if you pass an optional root node reference as a second argument to href, it will never intercept clicks outside that node. This is useful when your app is confined to a widget in a larger document.

href(function (href) {
  router(href)
  console.log('link was clicked: ' + href)
}, document.getElementById("app-root"))

qs

Sometimes query strings must be decoded. In order to do this, the ./qs.js file is included.

const qs = require('./qs')
qs('https://www.npmjs.com/search?q=query+string')
// => { q: 'query+string' }

walk

Sometimes it's necessary to walk the trie to apply transformations. In order to access the raw callback and prevent unnecessary function calls we need to disable the default thunking mechanism by passing { thunk: false }:

const sheetRouter = require('sheet-router')
const walk = require('sheet-router/walk')

const router = sheetRouter({ thunk: false }, [
  ['/multiply', (x, y) => x * y],
  ['/divide', (x, y) => x / y]
])

walk(router, (route, cb) => {
  const y = 2
  return function (params, x) {
    return cb(x, y)
  }
})

router('/multiply', 4)
// => 8
router('/divide', 8)
// => 4

We could change our transformed function to be thunked by changing walk to return a function, and setting { thunk: 'match' } so only the match function thunks. This is pretty advanced stuff, so don't sweat it too much - but it's super useful to create performant frameworks!

const router = sheetRouter({ thunk: 'match' }, [
  ['/foo', (x, y) => x * y],
  ['/bar', (x, y) => x / y]
])

walk(router, (route, cb) => {
  const y = 2
  return function (params) {
    return function (x) {
      return cb(x, y)
    }
  }
})

router('/multiply', 4)
// => 8
router('/multiply', 4)
// => 8 (but this time around this is computed faster)
router('/divide', 8)
// => 4

create-location

Sometimes you want to mirror the browser location API inside an object to use inside a framework. The hard part is to compute the new href from a set of changes. create-location provides an API to do just that:

const createLocation = require('sheet-router/create-location')

document.location = '/foo/bar#hey?beep=boop'
var location = createLocation()
// => {
//    pathname: '/',
//    hash: '#hey',
//    search: { beep: 'boop' },
//    href: '/foo/bar#hey?beep=boop'
//  }

const hashPatch = { hash: '#oh-no' }
var location = createLocation(location, hashPatch)
// => {
//    pathname: '/',
//    hash: '#oh-no',
//    search: { beep: 'boop' },
//    href: '/foo/bar#oh-no?beep=boop'
//  }

const uriPatch = '/hey/hello'
var location = createLocation(location, uriPatch)
// => {
//    pathname: '/hey/hello',
//    hash: '',
//    search: { },
//    href: '/hey/hello'
//  }

virtual-dom example

const render = require('virtual-dom/create-element')
const sheetRouter = require('sheet-router')
const h = require('virtual-dom/h')
const hyperx = require('hyperx')

const html = hyperx(h)

const router = sheetRouter([
  ['/foo/bar', (params, h, state) => html`<div>hello world!</div>`]
])

const node = render(router('/foo/bar', h, { name: 'Jane' }))
document.body.appendChild(node)
<body>
  <div>hello world</div>
</body>

react example

const sheetRouter = require('sheet-router')
const render = require('react-dom')
const hyperx = require('hyperx')
const react = require('react')

const html = hyperx(react.createElement)

const router = sheetRouter([
  ['/foo/bar', (params, h, state) => html`<div>hello world!</div>`]
])

render(router('/foo', react.createElement, { name: 'Jane' }), document.body)
<body>
  <div>hello world</div>
</body>

API

router = sheetRouter(opts?, [routes])

Create a new router from a nested array. Takes an optional options object as the first argument. Options are:

  • opts.default: defaults to '/404', default path to use if no paths match
  • opts.thunk: defaults to true. Toggle if callbacks should be thunked or not. Can be set to 'match' to only have the returned router.match() function expect thunks to exist. Useful to write a custom walk function that creates a different signature

router(path, [,...])

Match a route on the router. Takes a path and an arbitrary list of arguments that are then passed to the matched routes. Cleans urls to only match the pathname.

history(cb(href))

Call a callback to handle html5 pushsState history.

href(cb(href))

Call a callback to handle <a > clicks.

See Also

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].