6.0.9 • Published 10 months ago

route-event v6.0.9

Weekly downloads
94
License
MIT
Repository
github
Last release
10 months ago

route event

tests types module semantic versioning license

Simple route event for the browser. This will handle URL changes client-side, so that navigating will not cause a page reload.

Call a function with a path whenever someone clicks a link that is local to the server. Also, use the history API to handle back/forward button clicks.

install

npm i -S route-event

CJS

var Route = require('route-event').default

ESM

import Route from 'route-event'

example

Listen for click events on document.body. If the event is triggered by using the browser's back/forward button, then { popstate } will be true.

import Route from 'route-event'
const onRoute = Route()  // by default listen on document.body

// listen for click events on docuement.body. If the href is local to the
// server, call `onRoute`
var stopListening = onRoute(function onRoute (path, data) {
  console.log(path)
  // => '/example/path'
  console.log(data)
  // => { scrollX: 0, scrollY: 0, popstate: false }

  // handle scroll state like a web browser
  // (restore scroll position on back/forward)
  if (data.popstate) {
      return window.scrollTo(data.scrollX, data.scrollY)
  }

  // if this was a link click (not back button), then scroll to top
  window.scrollTo(0, 0)
})

// programmatically change the location and call the onRoute cb
routeEvent.setRoute('/some/path')

// change the route, but don't call the callbacks
routeEvent.setRoute.push('/abc')

// ...sometime in the future...
// unsubscribe from route events
stopListening()

Pass in an element to listen to, and handle events with a router:

import Route from 'route-event'
import Router from '@substrate-system/routes'

const router = Router()
const routeEvent = Route({
  el: document.getElementById('example')
})

router.addRoute('/', function () {
  console.log('root')
})

routeEvent(function onChange (path, ev) {
  var m = router.match(path)
  m.action()

  // handle scroll state like a web browser
  if (ev.popstate) {
      return window.scrollTo(ev.scrollX, ev.scrollY)
  }
  window.scrollTo(0, 0)
})

API

Listener

Event listeners are functions that take an href and an object with previous scroll position and popstate -- a boolean indicating if this was a link click or back / forward button (true means it was back/forward button).

interface Listener {
  (href:string, data:{
    scrollX:number,
    scrollY:number,
    popstate:boolean
  }):void;
}

Route

Create an instance of the event listener. Optionally take an element to listen to. Return a function that takes a callback that will receive route events. The returned function also has a property setRoute that will prgrammatically change the URL and call any route listeners.

import Route from 'route-event'
function Route (opts:{ el?:HTMLElement } = {}):{
    (cb:Listener):void;
    setRoute:ReturnType<typeof singlePage>
}

setRoute

A property on the returned function so you can programmatically set the URL.

function setRoute (href:string):void
import Route from '@substrate-system/route-event'

const routeEvent = Route()
routeEvent.setRoute('/example')

setRoute.push

Change the route, but don't call the callbacks.

import Route from '@substrate-system/route-event'

const routeEvent = Route()
routeEvent.setRoute.push = function (href:string):void {
6.0.7

12 months ago

6.0.6

12 months ago

6.0.9

10 months ago

6.0.8

11 months ago

6.0.5

1 year ago

6.0.1

1 year ago

6.0.3

1 year ago

6.0.2

1 year ago

5.1.0

2 years ago

5.2.3

2 years ago

5.2.2

2 years ago

5.2.1

2 years ago

5.2.0

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

6.0.0

1 year ago

4.2.27

2 years ago

4.2.23

2 years ago

4.2.24

2 years ago

4.2.25

2 years ago

4.2.20

2 years ago

4.2.18

2 years ago

4.2.17

2 years ago

4.2.16

2 years ago

4.2.14

2 years ago

4.2.15

2 years ago

4.2.7

2 years ago

4.2.6

2 years ago

4.2.9

2 years ago

4.2.8

2 years ago

4.2.10

2 years ago

4.2.2

2 years ago

4.2.11

2 years ago

4.2.5

2 years ago

4.2.12

2 years ago

4.2.4

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.2.13

2 years ago

4.1.8

3 years ago

4.1.9

2 years ago

4.1.10

2 years ago

4.1.7

3 years ago

3.2.2

3 years ago

3.2.4

3 years ago

3.2.3

3 years ago

4.1.4

3 years ago

4.1.3

3 years ago

4.1.6

3 years ago

4.1.5

3 years ago

4.1.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.1.2

3 years ago

4.1.1

3 years ago

3.2.1

3 years ago

3.2.0

4 years ago

3.1.0

5 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.2

9 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago