Use behavior: "instant" for scroll restoration to ignore CSS scroll-behavior#1484
Open
yujiteshima wants to merge 1 commit intohotwired:mainfrom
Open
Use behavior: "instant" for scroll restoration to ignore CSS scroll-behavior#1484yujiteshima wants to merge 1 commit intohotwired:mainfrom
yujiteshima wants to merge 1 commit intohotwired:mainfrom
Conversation
…ehavior When `scroll-behavior: smooth` is set in CSS, restoration visits would animate to the previous scroll position. Using `behavior: "instant"` ensures scroll position is restored instantly regardless of CSS settings. Fixes hotwired#1448
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Ensure scroll restoration on restoration visits ignores CSS
scroll-behavior: smooth.When CSS
scroll-behavior: smoothis set (common in Bootstrap and other frameworks), restoration visits animate scrolling to the previous position instead of jumping instantly. This creates a poor user experience when navigating back/forward.Root cause:
scrollTo(x, y)defaults tobehavior: "auto", which respects CSSscroll-behavior.Solution: Explicitly use
behavior: "instant"so restoration always jumps immediately.Why
"instant"is appropriateScroll restoration is a framework-controlled action, not a user-initiated scroll. The user expects to return to their previous position immediately—matching native browser back/forward behavior. CSS
scroll-behavior: smoothis intended for explicit user actions like clicking anchor links, not for programmatic state restoration.Browser Compatibility
This change is low-risk, as unsupported environments fall back to instant scrolling.
Reference: Preventing smooth scrolling with JavaScript
Related
Fixes #1448