From e17e7411577f6c512d7c1bff3d447891d31623ad Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 23 Jan 2025 15:20:00 +0530 Subject: [PATCH] clean --- src/client/app/router.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index 6249e51e..83faca5c 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -93,11 +93,7 @@ export function createRouter( const currentLoc = new URL(loc, fakeHost) if (href === loc) { - if (!initialLoad) { - if (hash) scrollTo(hash, smoothScroll) - else window.scrollTo(0, 0) - return - } + if (!initialLoad) return scrollTo(hash, smoothScroll) } else { history.replaceState({ scrollPosition: window.scrollY }, '') history.pushState({}, '', href) @@ -111,8 +107,7 @@ export function createRouter( }) ) - if (hash) scrollTo(hash, smoothScroll) - else window.scrollTo(0, 0) + return scrollTo(hash, smoothScroll) } return @@ -166,8 +161,7 @@ export function createRouter( history.replaceState({}, '', href) } - if (targetLoc.hash && !scrollPosition) scrollTo(targetLoc.hash) - else window.scrollTo(0, scrollPosition) + return scrollTo(targetLoc.hash, false, scrollPosition) }) } } @@ -277,7 +271,12 @@ export function useRoute(): Route { return useRouter().route } -export function scrollTo(hash: string, smooth = false) { +export function scrollTo(hash: string, smooth = false, scrollPosition = 0) { + if (!hash || scrollPosition) { + window.scrollTo(0, scrollPosition) + return + } + let target: Element | null = null try {