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