From 393d07a2d14b9dd6e40b2bee39db8f7091c5e8b0 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 5 Jul 2022 22:25:27 +0900 Subject: [PATCH] enhance(client): improve usability --- packages/client/src/nirax.ts | 7 ++++++- packages/client/src/router.ts | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/client/src/nirax.ts b/packages/client/src/nirax.ts index 9bcc5b683..4efa0345a 100644 --- a/packages/client/src/nirax.ts +++ b/packages/client/src/nirax.ts @@ -62,6 +62,7 @@ export class Router extends EventEmitter<{ props: Map | 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, diff --git a/packages/client/src/router.ts b/packages/client/src/router.ts index 3102302ce..7ce338bd0 100644 --- a/packages/client/src/router.ts +++ b/packages/client/src/router.ts @@ -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;