Skip to content

Conversation

@reidbarber
Copy link
Member

@reidbarber reidbarber commented Nov 25, 2025

We need to handle this manually since we're using the scrollable container instead of the window.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

Verify that scroll position is restored when navigating back/forward in the browser. Also verify that scroll positions are still accurate for hash links.

🧢 Your Project:

@rspbot
Copy link

rspbot commented Nov 25, 2025

Copy link
Member

@LFDanLu LFDanLu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work well, bit unfortunate that we have to do this manually though

let currentAbortController: AbortController | null = null;

// Store scroll positions by pathname
const scrollPositions = new Map<string, {scrollTop: number, windowScrollTop: number}>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can save these in the history entry itself? https://stackoverflow.com/a/16732655

Might also have to turn off the browser's defaults? https://developer.chrome.com/blog/history-api-scroll-restoration

I did wonder if the defaults would work if we moved the key from the <main> element further down (e.g. article) so that it doesn't get replaced on each navigation. Did you try that?

if (scrollContainer) {
scrollContainer.scrollTop = position.scrollTop;
}
window.scrollTo(0, position.windowScrollTop);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it need to be the inner div that we scroll not the window?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants