Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Fix resetScrollPositions and restoreScrollPositions router methods #1980

Merged
merged 5 commits into from
Sep 19, 2024

Conversation

pedroborges
Copy link
Collaborator

This PR resolves two scroll-related problems in Inertia:

  1. Scroll Reset on Page Navigation

    • Issue: Using Inertia’s Link causes the scroll position to reset briefly before loading new content, leading to a flash of the previous page and jank on heavier or mobile pages.
    • Details: #1802
    • Previous Attempt: #1803 tried to fix this but added complexity. This PR provides a simpler solution.
  2. Scroll Position Restoration for [scroll-region] Elements

    • Issue: In the React adapter, [scroll-region] elements don't restore their scroll positions when navigating back because restoreScrollPositions doesn't detect them before the document body updates.
    • Details: #1816, #1802, #1211
    • Previous Attempt: #1796 used setTimeout, but nextFrame is a better approach.

Solution

Used a double requestAnimationFrame (RAF) method to ensure callbacks run after the next repaint. This approach is used internally by Vue:

This fix is implemented in the core, making it safe for React, Vue, and Svelte adapters without needing adapter-specific changes.


Fixes #1211, #1459, #1698, #1796, #1802, #1803, #1816, #1922

if (window.location.hash) {
// We're using a setTimeout() here as a workaround for a bug in the React adapter where the
// rendering isn't completing fast enough, causing the anchor link to not be scrolled to.
setTimeout(() => document.getElementById(window.location.hash.slice(1))?.scrollIntoView())
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

setTimeout is no longer needed

Copy link
Contributor

@joetannenbaum joetannenbaum left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @pedroborges!

@pedroborges pedroborges merged commit ba2099f into master Sep 19, 2024
7 checks passed
@pedroborges pedroborges deleted the fix-scroll-region-and-flash branch September 19, 2024 17:44
pedroborges added a commit to olragon/inertia that referenced this pull request Sep 24, 2024
* master: (95 commits)
  [1.x] Fix props reactivity (inertiajs#1969)
  [1.x] useForm wrongly overwrites default values ​​after successful submission (inertiajs#1935)
  Update changelog
  [1.x] Fix `resetScrollPositions` and `restoreScrollPositions` router methods (inertiajs#1980)
  [1.x] Fix [scroll-region] on html element with overflow-scroll (inertiajs#1782)
  [1.x] Fix useForm re-renders by memoizing functions in React (inertiajs#1607)
  [1.x] Fix "DataCloneError: <Object> could not be cloned" (inertiajs#1967)
  [1.x] preserveScroll should be true on initial page visit (inertiajs#1360)
  Fix type augmentation (inertiajs#1958)
  [1.x] Fix doubling hash in React StrictMode (inertiajs#1728)
  [1.x] Add SSR support for Svelte 5 (inertiajs#1970)
  [1.x] Fix <Render /> component to respect "preserveState" (inertiajs#1943)
  [1.x] Fix 'received an unexpected slot "default"' warning (inertiajs#1941)
  QA: Add @types/lodash to fix svelte-check
  QA: Update reactive if statement
  Review useForm types
  QA: Move the if server up
  QA: Revert tsconfig change
  QA: Remove plural
  QA: Remove unused props type + add extra types just in case
  ...

# Conflicts:
#	packages/react/src/index.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scroll regions inside page component are not restored after form submit.
2 participants