@domql/router 2.3.1 → 2.3.3

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 +16 -6
  2. package/package.json +5 -2
package/index.js CHANGED
@@ -1,32 +1,42 @@
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
+ scrollTo: true
15
+ }
16
+
9
17
  export const router = (
10
18
  element,
11
19
  path,
12
20
  state = {},
13
- level = 0,
14
- pushState = true
21
+ options = defaultOptions
15
22
  ) => {
16
- lastLevel = level
23
+ merge(options, defaultOptions)
24
+ lastLevel = options.lastLevel
25
+
17
26
  const [pathname, hash] = (path).split('#')
18
27
 
19
- const route = getActiveRoute(pathname, level)
28
+ const route = getActiveRoute(pathname, options.level)
20
29
  const content = element.routes[route]
21
30
 
22
31
  if (content) {
23
- if (pushState) window.history.pushState(state, null, pathname + (hash ? `#${hash}` : ''))
32
+ if (options.pushState) window.history.pushState(state, null, pathname + (hash ? `#${hash}` : ''))
24
33
 
25
34
  element.set({ extend: content })
26
35
  element.state.update({ route, hash })
27
36
 
28
37
  const rootNode = element.node
29
- rootNode.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
38
+ if (options.scrollTo) rootNode.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
39
+
30
40
  if (hash) {
31
41
  const activeNode = document.getElementById(hash)
32
42
  if (activeNode) {
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@domql/router",
3
- "version": "2.3.1",
3
+ "version": "2.3.3",
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": "edb3ba3e6d1fc8d7c4ec9d48e01c195bb20fceba",
9
+ "dependencies": {
10
+ "@domql/utils": "latest"
11
+ },
12
+ "gitHead": "1f87a922baa3f51543923135ecff36d6f699e9a6",
10
13
  "source": "index.js"
11
14
  }