Skip to content

Commit da2a99c

Browse files
committed
respect prefers reduced motion
1 parent f320044 commit da2a99c

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/client/app/router.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,21 @@ export function scrollTo(hash: string, smooth = false, scrollPosition = 0) {
269269
}
270270
if (!target) return
271271

272-
const targetPadding = parseInt(window.getComputedStyle(target).paddingTop, 10)
273-
274272
const targetTop =
275273
window.scrollY +
276-
target.getBoundingClientRect().top -
277-
getScrollOffset() +
278-
targetPadding
274+
target.getBoundingClientRect().top -
275+
getScrollOffset() +
276+
Number.parseInt(window.getComputedStyle(target).paddingTop, 10) || 0
277+
278+
const behavior = window.matchMedia('(prefers-reduced-motion)').matches
279+
? 'instant'
280+
: // only smooth scroll if distance is smaller than screen height
281+
smooth && Math.abs(targetTop - window.scrollY) <= window.innerHeight
282+
? 'smooth'
283+
: 'auto'
279284

280285
const scrollToTarget = () => {
281-
// only smooth scroll if distance is smaller than screen height
282-
if (!smooth || Math.abs(targetTop - window.scrollY) > window.innerHeight)
283-
window.scrollTo(0, targetTop)
284-
else window.scrollTo({ left: 0, top: targetTop, behavior: 'smooth' })
286+
window.scrollTo({ left: 0, top: targetTop, behavior })
285287

286288
// focus the target element for better accessibility
287289
target.focus({ preventScroll: true })

0 commit comments

Comments
 (0)