Skip to content

Commit 6513f42

Browse files
authored
Add hash option (#667)
* Add hash option * Avoid unecessary scroll into view-call * Update readme with hash option * Fix typo * Remove nanolocation dep * Default hash option to true
1 parent 65d124b commit 6513f42

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,12 @@ Querystrings (e.g. `?foo=bar`) are ignored when matching routes. An object
284284
containing the key-value mappings exists as `state.query`.
285285

286286
### Hash routing
287-
Using hashes to delimit routes is supported out of the box (e.g. `/foo#bar`).
288-
When a hash is found we also check if there's an available anchor on the same
289-
page, and will scroll the screen to the position. Using both hashes in URLs and
290-
anchor links on the page is generally not recommended.
287+
By default hashes are treated as part of the url when routing. Using hashes to
288+
delimit routes (e.g. `/foo#bar`) can be disabled by setting the `hash`
289+
[option](#app--chooopts) to `false`. Regardless, when a hash is found we also
290+
check if there's an available anchor on the same page, and will scroll the
291+
screen to the position. Using both hashes in URLs and anchor links on the page
292+
is generally not recommended.
291293

292294
### Following links
293295
By default all clicks on `<a>` tags are handled by the router through the
@@ -532,6 +534,9 @@ Initialize a new `choo` instance. `opts` can also contain the following values:
532534
- __opts.cache:__ default: `undefined`. Override default class cache used by
533535
`state.cache`. Can be a a `number` (maximum number of instances in cache,
534536
default `100`) or an `object` with a [nanolru][nanolru]-compatible API.
537+
- __opts.hash:__ default: `true`. Treat hashes in URLs as part of the pathname,
538+
transforming `/foo#bar` to `/foo/bar`. This is useful if the application is
539+
not mounted at the website root.
535540

536541
### `app.use(callback(state, emitter, app))`
537542
Call a function and pass it a `state`, `emitter` and `app`. `emitter` is an instance

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var scrollToAnchor = require('scroll-to-anchor')
22
var documentReady = require('document-ready')
3-
var nanolocation = require('nanolocation')
43
var nanotiming = require('nanotiming')
54
var nanorouter = require('nanorouter')
65
var nanomorph = require('nanomorph')
@@ -39,8 +38,8 @@ function Choo (opts) {
3938
// properties for internal use only
4039
this._historyEnabled = opts.history === undefined ? true : opts.history
4140
this._hrefEnabled = opts.href === undefined ? true : opts.href
41+
this._hashEnabled = opts.hash === undefined ? true : opts.hash
4242
this._hasWindow = typeof window !== 'undefined'
43-
this._createLocation = nanolocation
4443
this._cache = opts.cache
4544
this._loaded = false
4645
this._stores = []
@@ -128,8 +127,11 @@ Choo.prototype.start = function () {
128127
if (self._hrefEnabled) {
129128
nanohref(function (location) {
130129
var href = location.href
131-
var currHref = window.location.href
132-
if (href === currHref) return
130+
var hash = location.hash
131+
if (href === window.location.href) {
132+
if (!this._hashEnabled && hash) scrollToAnchor(hash)
133+
return
134+
}
133135
self.emitter.emit(self._events.PUSHSTATE, href)
134136
})
135137
}
@@ -224,10 +226,12 @@ Choo.prototype.toString = function (location, state) {
224226
Choo.prototype._matchRoute = function (locationOverride) {
225227
var location, queryString
226228
if (locationOverride) {
227-
location = locationOverride.replace(/\?.+$/, '')
229+
location = locationOverride.replace(/\?.+$/, '').replace(/\/$/, '')
230+
if (!this._hashEnabled) location = location.replace(/#.+$/, '')
228231
queryString = locationOverride
229232
} else {
230-
location = this._createLocation()
233+
location = window.location.pathname.replace(/\/$/, '')
234+
if (this._hashEnabled) location += window.location.hash.replace(/^#/, '/')
231235
queryString = window.location.search
232236
}
233237
var matched = this.router.match(location)

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"nanocomponent": "^6.5.0",
4343
"nanohref": "^3.0.0",
4444
"nanohtml": "^1.1.0",
45-
"nanolocation": "^1.0.0",
4645
"nanolru": "^1.0.0",
4746
"nanomorph": "^5.1.2",
4847
"nanoquery": "^1.1.0",

0 commit comments

Comments
 (0)