From 8d8f4518a8ac3729e12899760e9607180f5c7956 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 23 Jan 2025 01:15:30 +0530 Subject: [PATCH] revert --- src/client/app/router.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index 8c783139..ab8bb3b7 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -71,7 +71,10 @@ export function createRouter( go } - async function go(href: string = inBrowser ? location.href : '/') { + async function go( + href: string = inBrowser ? location.href : '/', + cause: Element | null = null + ) { href = normalizeHref(href) const loc = inBrowser ? normalizeHref(location.href) : null @@ -94,7 +97,14 @@ export function createRouter( }) ) - hash ? scrollTo(hash) : window.scrollTo(0, 0) + if (hash) { + scrollTo( + hash, + cause ? cause.classList.contains('header-anchor') : false + ) + } else { + window.scrollTo(0, 0) + } } return @@ -262,7 +272,7 @@ export function useRoute(): Route { return useRouter().route } -export function scrollTo(hash: string) { +export function scrollTo(hash: string, smooth = false) { let target: Element | null = null try { @@ -285,11 +295,9 @@ export function scrollTo(hash: string) { function scrollToTarget() { // only smooth scroll if distance is smaller than screen height. - if (Math.abs(targetTop - window.scrollY) > window.innerHeight) { + if (!smooth || Math.abs(targetTop - window.scrollY) > window.innerHeight) window.scrollTo(0, targetTop) - } else { - window.scrollTo({ left: 0, top: targetTop, behavior: 'smooth' }) - } + else window.scrollTo({ left: 0, top: targetTop, behavior: 'smooth' }) } requestAnimationFrame(scrollToTarget)