remove short path and extra options

pull/4511/head
Divyansh Singh 8 months ago
parent 470ccd3f1f
commit 4246ec4b5a

@ -166,12 +166,7 @@ if (inBrowser) {
// scroll to hash on new tab during dev // scroll to hash on new tab during dev
if (import.meta.env.DEV && location.hash) { if (import.meta.env.DEV && location.hash) {
const target = document.getElementById( scrollTo(location.hash)
decodeURIComponent(location.hash).slice(1)
)
if (target) {
scrollTo(target, location.hash)
}
} }
}) })
}) })

@ -71,10 +71,7 @@ export function createRouter(
go go
} }
async function go( async function go(href: string = inBrowser ? location.href : '/') {
href: string = inBrowser ? location.href : '/',
cause: Element | null = null
) {
href = normalizeHref(href) href = normalizeHref(href)
let loc: string | null = null let loc: string | null = null
@ -96,11 +93,8 @@ export function createRouter(
newURL: href newURL: href
}) })
) )
if (hash) {
scrollTo(cause, hash, cause?.classList.contains('header-anchor')) hash ? scrollTo(hash) : window.scrollTo(0, 0)
} else {
window.scrollTo(0, 0)
}
return return
} }
@ -158,7 +152,7 @@ export function createRouter(
} }
if (targetLoc.hash && !scrollPosition) { if (targetLoc.hash && !scrollPosition) {
scrollTo(null, targetLoc.hash) scrollTo(targetLoc.hash)
} else { } else {
window.scrollTo(0, scrollPosition) window.scrollTo(0, scrollPosition)
} }
@ -238,7 +232,7 @@ export function createRouter(
// only intercept inbound html links // only intercept inbound html links
if (origin === currentLoc.origin && treatAsHtml(pathname)) { if (origin === currentLoc.origin && treatAsHtml(pathname)) {
e.preventDefault() e.preventDefault()
go(href, link) go(href)
} }
}, },
{ capture: true } { capture: true }
@ -273,13 +267,11 @@ export function useRoute(): Route {
return useRouter().route return useRouter().route
} }
export function scrollTo(from: Element | null, hash: string, smooth = false) { export function scrollTo(hash: string) {
let target: Element | null = null let target: Element | null = null
try { try {
target = from?.classList.contains('header-anchor') target = document.getElementById(decodeURIComponent(hash).slice(1))
? from
: document.getElementById(decodeURIComponent(hash).slice(1))
} catch (e) { } catch (e) {
console.warn(e) console.warn(e)
} }
@ -289,17 +281,22 @@ export function scrollTo(from: Element | null, hash: string, smooth = false) {
window.getComputedStyle(target).paddingTop, window.getComputedStyle(target).paddingTop,
10 10
) )
const targetTop = const targetTop =
window.scrollY + window.scrollY +
target.getBoundingClientRect().top - target.getBoundingClientRect().top -
getScrollOffset() + getScrollOffset() +
targetPadding targetPadding
function scrollToTarget() { function scrollToTarget() {
// only smooth scroll if distance is smaller than screen height. // only smooth scroll if distance is smaller than screen height.
if (!smooth || Math.abs(targetTop - window.scrollY) > window.innerHeight) if (Math.abs(targetTop - window.scrollY) > window.innerHeight) {
window.scrollTo(0, targetTop) window.scrollTo(0, targetTop)
else window.scrollTo({ left: 0, top: targetTop, behavior: 'smooth' }) } else {
window.scrollTo({ left: 0, top: targetTop, behavior: 'smooth' })
} }
}
requestAnimationFrame(scrollToTarget) requestAnimationFrame(scrollToTarget)
} }
} }

Loading…
Cancel
Save