From 7a2c67c5a67e93d3c1bd83ab06f171fcab0e0924 Mon Sep 17 00:00:00 2001 From: Rasmus Edvardsen Date: Mon, 9 Mar 2026 15:31:26 +0100 Subject: [PATCH] Simplify scrollToElement to use scrollIntoView --- src/utils/scrollTo.ts | 54 +------------------------------------------ 1 file changed, 1 insertion(+), 53 deletions(-) diff --git a/src/utils/scrollTo.ts b/src/utils/scrollTo.ts index 25ce31e..a037c47 100644 --- a/src/utils/scrollTo.ts +++ b/src/utils/scrollTo.ts @@ -1,60 +1,8 @@ -// help functions -const easeInOutQuad = (t: number, b: number, c: number, d: number): number => { - t /= d / 2 - if (t < 1) return c / 2 * t * t + b - t-- - return -c / 2 * (t * (t - 2) - 1) + b -} - -const animatedScrollTo = ( - element: HTMLElement, - to: number, - duration: number, - callback?: () => void -): void => { - let start = element.scrollTop - let change = to - start - let animationStart = +new Date() - let animating = true - let lastpos: number | null = null - - const animateScroll = (): void => { - if (!animating) { - return - } - requestAnimationFrame(animateScroll) - const now = +new Date() - const val = Math.floor(easeInOutQuad(now - animationStart, start, change, duration)) - if (lastpos) { - if (lastpos === Math.ceil(element.scrollTop)) { - lastpos = val - element.scrollTop = val - } else { - animating = false - } - } else { - lastpos = val - element.scrollTop = val - } - if (now > animationStart + duration) { - element.scrollTop = to - animating = false - if (callback) { - callback() - } - } - } - requestAnimationFrame(animateScroll) -} - const scrollToElement = (selector: string): void => { // Scroll to search highlight word - const container = document.documentElement const anchor = document.querySelector(selector) if (anchor) { - const { y } = anchor.getBoundingClientRect() - const DURATION = 300 - animatedScrollTo(container, container.scrollTop + y - 60, DURATION) + anchor.scrollIntoView({ behavior: 'smooth', block: 'start' }) } }