enhance(client): improve usability

This commit is contained in:
syuilo 2022-07-05 22:25:27 +09:00 committed by Chloe Kudryavtsev
parent d61a2f9009
commit 393d07a2d1
2 changed files with 10 additions and 1 deletions

View file

@ -62,6 +62,7 @@ export class Router extends EventEmitter<{
props: Map<string, string> | null;
key: string;
}) => void;
same: () => void;
}> {
private routes: RouteDef[];
private currentPath: string;
@ -213,11 +214,15 @@ export class Router extends EventEmitter<{
}
public push(path: string) {
const beforePath = this.currentPath;
if (path === beforePath) {
this.emit('same');
return;
}
if (this.navHook) {
const cancel = this.navHook(path);
if (cancel) return;
}
const beforePath = this.currentPath;
this.navigate(path, null);
this.emit('push', {
beforePath,

View file

@ -270,6 +270,10 @@ mainRouter.addListener('push', ctx => {
}
});
mainRouter.addListener('same', () => {
window.scroll({ top: 0, behavior: 'smooth' });
});
window.addEventListener('popstate', (event) => {
mainRouter.change(location.pathname + location.search + location.hash, event.state?.key);
const scrollPos = scrollPosStore.get(event.state?.key) ?? 0;