@pyreon/router 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +90 -0
- package/lib/analysis/index.js.html +5406 -0
- package/lib/index.js +830 -0
- package/lib/index.js.map +1 -0
- package/lib/types/index.d.ts +690 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index2.d.ts +322 -0
- package/lib/types/index2.d.ts.map +1 -0
- package/package.json +49 -0
- package/src/components.tsx +307 -0
- package/src/index.ts +78 -0
- package/src/loader.ts +97 -0
- package/src/match.ts +264 -0
- package/src/router.ts +451 -0
- package/src/scroll.ts +55 -0
- package/src/tests/router.test.ts +3294 -0
- package/src/tests/setup.ts +3 -0
- package/src/types.ts +242 -0
package/src/scroll.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ResolvedRoute, RouterOptions } from "./types"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Scroll restoration manager.
|
|
5
|
+
*
|
|
6
|
+
* Saves scroll position before each navigation and restores it when
|
|
7
|
+
* navigating back to a previously visited path.
|
|
8
|
+
*/
|
|
9
|
+
export class ScrollManager {
|
|
10
|
+
private readonly _positions = new Map<string, number>()
|
|
11
|
+
private readonly _behavior: RouterOptions["scrollBehavior"]
|
|
12
|
+
|
|
13
|
+
constructor(behavior: RouterOptions["scrollBehavior"] = "top") {
|
|
14
|
+
this._behavior = behavior
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Call before navigating away — saves current scroll position for `fromPath` */
|
|
18
|
+
save(fromPath: string): void {
|
|
19
|
+
// save/restore are only called from browser navigation paths (guarded by caller)
|
|
20
|
+
this._positions.set(fromPath, window.scrollY)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Call after navigation is committed — applies scroll behavior */
|
|
24
|
+
restore(to: ResolvedRoute, from: ResolvedRoute): void {
|
|
25
|
+
const behavior = (to.meta.scrollBehavior as typeof this._behavior) ?? this._behavior ?? "top"
|
|
26
|
+
|
|
27
|
+
if (typeof behavior === "function") {
|
|
28
|
+
const saved = this._positions.get(to.path) ?? null
|
|
29
|
+
const result = behavior(to, from, saved)
|
|
30
|
+
this._applyResult(result, to.path)
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
this._applyResult(behavior, to.path)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private _applyResult(result: "top" | "restore" | "none" | number, toPath: string): void {
|
|
38
|
+
if (result === "none") return
|
|
39
|
+
if (result === "top" || result === undefined) {
|
|
40
|
+
window.scrollTo({ top: 0, behavior: "instant" as ScrollBehavior })
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
if (result === "restore") {
|
|
44
|
+
const saved = this._positions.get(toPath) ?? 0
|
|
45
|
+
window.scrollTo({ top: saved, behavior: "instant" as ScrollBehavior })
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
// At this point result must be a number (all string cases handled above)
|
|
49
|
+
window.scrollTo({ top: result as number, behavior: "instant" as ScrollBehavior })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getSavedPosition(path: string): number | null {
|
|
53
|
+
return this._positions.get(path) ?? null
|
|
54
|
+
}
|
|
55
|
+
}
|