Spath
Capture anchor clicks and do a pushState instead of navigating
Example
Why
Most SPA's need to capture default anchor behaviour and do a pushState, this module encaptulates that logic.
Getting started
npm i --save spath
Exposes individually or wrapped
const pushStateAnchors = require('spath/pushStateAnchors')
const setPath = require('spath/setPath')
/* Capture all anchor clicks and instead to a pushState */
document.addEventListener('click', pushStateAnchors())
/* Custom capture logic */
document.addEventListener('click', pushStateAnchors(href => {
return href.startsWith(window.location.origin)
}))
/* To programmatically pushState */
setPath('/some-path')
Detecting path change
on-url-change can be used to detect when the page's URL has changed.
Developing
git clone git@github.com:markwylde/spath.git
cd spath
npm install
npm run start
Then goto: http://localhost:8080
License
This project is licensed under the terms of the MIT license.