Skip to content

Comments

Use behavior: "instant" for scroll restoration to ignore CSS scroll-behavior#1484

Open
yujiteshima wants to merge 1 commit intohotwired:mainfrom
yujiteshima:fix/instant-scroll-on-restoration
Open

Use behavior: "instant" for scroll restoration to ignore CSS scroll-behavior#1484
yujiteshima wants to merge 1 commit intohotwired:mainfrom
yujiteshima:fix/instant-scroll-on-restoration

Conversation

@yujiteshima
Copy link
Contributor

Summary

Ensure scroll restoration on restoration visits ignores CSS scroll-behavior: smooth.

When CSS scroll-behavior: smooth is 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 to behavior: "auto", which respects CSS scroll-behavior.

Solution: Explicitly use behavior: "instant" so restoration always jumps immediately.

Why "instant" is appropriate

Scroll 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: smooth is 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

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Use behavior: "auto" when scrolling to last position on restoration visits

1 participant