betal-fe 4.2.0 → 4.3.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/dist/betal-fe.js +25 -1
- package/package.json +1 -1
package/dist/betal-fe.js
CHANGED
|
@@ -530,6 +530,7 @@ class HashRouter {
|
|
|
530
530
|
#dispatcher = new Dispatcher();
|
|
531
531
|
#subscriptions = new WeakMap();
|
|
532
532
|
#subscriberFns = new Set();
|
|
533
|
+
#scrollBehavior = 'top';
|
|
533
534
|
get matchedRoute() {
|
|
534
535
|
return this.#matchedRoute;
|
|
535
536
|
}
|
|
@@ -549,9 +550,12 @@ class HashRouter {
|
|
|
549
550
|
return hash.slice(1);
|
|
550
551
|
}
|
|
551
552
|
#onPopState = () => this.#matchCurrentRoute();
|
|
552
|
-
constructor(routes = []) {
|
|
553
|
+
constructor(routes = [], options = {}) {
|
|
553
554
|
assert(Array.isArray(routes), "Routes must be an array");
|
|
554
555
|
this.#matchers = routes.map(makeRouteMatcher);
|
|
556
|
+
if (options.scrollBehavior !== undefined) {
|
|
557
|
+
this.#scrollBehavior = options.scrollBehavior;
|
|
558
|
+
}
|
|
555
559
|
}
|
|
556
560
|
async init() {
|
|
557
561
|
if (this.#isInitialized) {
|
|
@@ -596,9 +600,29 @@ class HashRouter {
|
|
|
596
600
|
this.#params = matcher.extractParams(path);
|
|
597
601
|
this.#query = matcher.extractQuery(path);
|
|
598
602
|
this.#pushState(path);
|
|
603
|
+
this.#handleScrollBehavior(from, to);
|
|
599
604
|
this.#dispatcher.dispatch(ROUTER_EVENT, { from, to, router: this });
|
|
600
605
|
}
|
|
601
606
|
}
|
|
607
|
+
#handleScrollBehavior(from, to) {
|
|
608
|
+
if (this.#scrollBehavior === false) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
setTimeout(() => {
|
|
612
|
+
if (typeof this.#scrollBehavior === 'function') {
|
|
613
|
+
const position = this.#scrollBehavior(from, to);
|
|
614
|
+
if (position) {
|
|
615
|
+
window.scrollTo({
|
|
616
|
+
left: position.x || 0,
|
|
617
|
+
top: position.y || 0,
|
|
618
|
+
behavior: position.behavior || 'smooth'
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
} else if (this.#scrollBehavior === 'top') {
|
|
622
|
+
window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
|
|
623
|
+
}
|
|
624
|
+
}, 0);
|
|
625
|
+
}
|
|
602
626
|
back() {
|
|
603
627
|
window.history.back();
|
|
604
628
|
}
|