Skip to content

Commit

Permalink
Change to preferring element.scrollTo
Browse files Browse the repository at this point in the history
Scroll position can fail to update in Firefox when setting it via `element.scrollTop` and `element.scrollLeft`. Change to preferring `element.scrollTo` with a fallback for the previous method to ensure backwards compatibility.

Resolves #1079
  • Loading branch information
craigrileyuk authored and reinink committed Sep 9, 2022
1 parent d1f1c18 commit 78519da
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/inertia/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,18 @@ export class Router {
}

protected resetScrollPositions(): void {
document.documentElement.scrollTop = 0
document.documentElement.scrollLeft = 0
const canScrollTo = typeof document.documentElement.scrollTo === 'function'
if (canScrollTo) document.documentElement.scrollTo({ top: 0, left: 0 })
else {
document.documentElement.scrollTop = 0
document.documentElement.scrollLeft = 0
}
this.scrollRegions().forEach(region => {
region.scrollTop = 0
region.scrollLeft = 0
if (canScrollTo) region.scrollTo({ top: 0, left: 0 })
else {
region.scrollTop = 0
region.scrollLeft = 0
}
})
this.saveScrollPositions()
if (window.location.hash) {
Expand All @@ -90,10 +97,14 @@ export class Router {
}

protected restoreScrollPositions(): void {
const canScrollTo = typeof document.documentElement.scrollTo === 'function'
if (this.page.scrollRegions) {
this.scrollRegions().forEach((region: Element, index: number) => {
const scrollPosition = this.page.scrollRegions[index]
if (scrollPosition) {
if (scrollPosition && canScrollTo) {
region.scrollTo({ top: scrollPosition.top, left: scrollPosition.left })
}
else if (scrollPosition) {
region.scrollTop = scrollPosition.top
region.scrollLeft = scrollPosition.left
}
Expand Down

0 comments on commit 78519da

Please sign in to comment.