@symbo.ls/router 2.3.10

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 +59 -0
  2. package/package.json +15 -0
package/index.js ADDED
@@ -0,0 +1,59 @@
1
+ 'use strict'
2
+
3
+ import { merge } from '@domql/utils'
4
+ import { redirectBasedOnAuth } from '@symbo.ls/auth-redirect'
5
+
6
+ export const getActiveRoute = (
7
+ route = window.location.pathname,
8
+ level
9
+ ) => `/${route.split('/')[level + 1]}`
10
+
11
+ export let lastLevel = 0
12
+
13
+ const defaultOptions = {
14
+ level: lastLevel,
15
+ pushState: true,
16
+ scrollToTop: true,
17
+ scrollToNode: false,
18
+ useFragment: false,
19
+ updateState: true
20
+ }
21
+
22
+ export const router = (
23
+ element,
24
+ path,
25
+ state = {},
26
+ options = defaultOptions
27
+ ) => {
28
+ merge(options, defaultOptions)
29
+ lastLevel = options.lastLevel
30
+
31
+ const rootRoute = getActiveRoute(path, 0)
32
+ path = redirectBasedOnAuth(element, element.state, { route: rootRoute })
33
+
34
+ const [pathname, hash] = (path).split('#')
35
+
36
+ const route = getActiveRoute(pathname, options.level)
37
+ const content = element.routes[route] || element.routes['/*']
38
+
39
+ if (content) {
40
+ if (options.pushState) window.history.pushState(state, null, pathname + (hash ? `#${hash}` : ''))
41
+
42
+ element.set({ tag: options.useFragment && 'fragment', extend: content })
43
+ if (options.updateState) element.state.update({ route, hash })
44
+
45
+ const rootNode = element.node
46
+ if (options.scrollToTop) rootNode.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
47
+ if (options.scrollToNode) content.content.node.scrollTo({ behavior: 'smooth', top: 0, left: 0 })
48
+
49
+ if (hash) {
50
+ const activeNode = document.getElementById(hash)
51
+ if (activeNode) {
52
+ const top = activeNode.getBoundingClientRect().top + rootNode.scrollToTopp - 140
53
+ rootNode.scrollTo({ behavior: 'smooth', top, left: 0 })
54
+ }
55
+ }
56
+
57
+ setCookie('route', path)
58
+ }
59
+ }
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@symbo.ls/router",
3
+ "version": "2.3.10",
4
+ "main": "index.js",
5
+ "license": "MIT",
6
+ "scripts": {
7
+ "vpatch": "npm version patch && npm publish --access public"
8
+ },
9
+ "dependencies": {
10
+ "@domql/utils": "latest",
11
+ "@symbo.ls/auth-redirect": "latest"
12
+ },
13
+ "gitHead": "66641a0ee9c94ecbe631b7b92e2018ad18af387b",
14
+ "source": "index.js"
15
+ }