@domql/router 2.0.8 → 2.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +27 -5
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,18 +1,40 @@
1
1
  'use strict'
2
2
 
3
- export const splitRoute = (route = window.location.pathname) => route.slice(1).split('/')
3
+ export const splitRoute = (route = window.location.pathname) => route.split('/')
4
4
 
5
5
  export const router = (rootElement, path, state = {}, level = 0, pushState = true) => {
6
6
  const route = path || window.location.pathname
7
+
8
+ if (path.slice(0, 1) === '#') {
9
+ window.location.hash = path
10
+ return
11
+ }
12
+
7
13
  const routes = splitRoute(route)
14
+ let currentRoute = routes[level + 1]
8
15
 
9
- const content = rootElement.routes[`/${routes[level]}`]
16
+ const hasHash = currentRoute.split('#')
17
+ if (hasHash[1]) currentRoute = hasHash[0]
18
+
19
+ const content = rootElement.routes[`/${currentRoute}`]
10
20
 
11
21
  if (content) {
12
- if (pushState) window.history.pushState(state, null, route)
22
+ currentRoute = `/${currentRoute}`
23
+ if (hasHash[1]) currentRoute += `#${hasHash[1]}`
24
+ if (pushState) window.history.pushState(state, null, currentRoute)
25
+
26
+ const rootNode = rootElement.node
27
+ rootNode.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
28
+ if (hasHash[1]) {
29
+ const activeNode = document.getElementById(hasHash[1])
30
+ if (activeNode) {
31
+ const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - 140
32
+ rootNode.scrollTo({ behavior: 'smooth', top, left: 0 })
33
+ }
34
+ }
35
+
13
36
  rootElement.set({ proto: content })
14
- .node.scrollIntoView({ behavior: 'smooth' })
15
37
  }
16
38
 
17
- if (level === 0) rootElement.state.update({ activePage: routes[level] })
39
+ if (level === 0) rootElement.state.update({ currentRoute })
18
40
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@domql/router",
3
- "version": "2.0.8",
3
+ "version": "2.2.5",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
7
  "vpatch": "npm version patch && npm publish --access public"
8
8
  },
9
- "gitHead": "b60c46e45c462d00976902d6cdbea6fd5504542b",
9
+ "gitHead": "c275ad11c795e7abd68cce00d0c758905c4e4f18",
10
10
  "source": "index.js"
11
11
  }