@domql/router 2.3.2 → 2.3.4

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 +18 -10
  2. package/package.json +5 -2
package/index.js CHANGED
@@ -1,40 +1,48 @@
1
1
  'use strict'
2
2
 
3
+ import { merge } from '@domql/utils'
4
+
3
5
  export const getActiveRoute = (
4
6
  route = window.location.pathname, level
5
7
  ) => `/${route.split('/')[level + 1]}`
6
8
 
7
9
  export let lastLevel = 0
8
10
 
11
+ const defaultOptions = {
12
+ level: lastLevel,
13
+ pushState: true,
14
+ scrollToTop: true,
15
+ scrollToNode: false
16
+ }
17
+
9
18
  export const router = (
10
19
  element,
11
20
  path,
12
21
  state = {},
13
- options = {
14
- level: lastLevel,
15
- pushState: true
16
- }
22
+ options = defaultOptions
17
23
  ) => {
18
- const level = lastLevel = options.level
19
- const pushState = options.pushState || options.pushState === undefined
24
+ merge(options, defaultOptions)
25
+ lastLevel = options.lastLevel
20
26
 
21
27
  const [pathname, hash] = (path).split('#')
22
28
 
23
- const route = getActiveRoute(pathname, level)
29
+ const route = getActiveRoute(pathname, options.level)
24
30
  const content = element.routes[route]
25
31
 
26
32
  if (content) {
27
- if (pushState) window.history.pushState(state, null, pathname + (hash ? `#${hash}` : ''))
33
+ if (options.pushState) window.history.pushState(state, null, pathname + (hash ? `#${hash}` : ''))
28
34
 
29
35
  element.set({ extend: content })
30
36
  element.state.update({ route, hash })
31
37
 
32
38
  const rootNode = element.node
33
- rootNode.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
39
+ if (options.scrollToTop) rootNode.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
40
+ if (options.scrollToNode) content.content.node.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
41
+
34
42
  if (hash) {
35
43
  const activeNode = document.getElementById(hash)
36
44
  if (activeNode) {
37
- const top = activeNode.getBoundingClientRect().top + rootNode.scrollTop - 140
45
+ const top = activeNode.getBoundingClientRect().top + rootNode.scrollToTopp - 140
38
46
  rootNode.scrollTo({ behavior: 'smooth', top, left: 0 })
39
47
  }
40
48
  }
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@domql/router",
3
- "version": "2.3.2",
3
+ "version": "2.3.4",
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": "e351985b592e8cb1f25130d9af8759ec9192b17f",
9
+ "dependencies": {
10
+ "@domql/utils": "latest"
11
+ },
12
+ "gitHead": "01460bf7ffc27c47ee985dfb593bff8fc9686ede",
10
13
  "source": "index.js"
11
14
  }