Repository navigation
fix(plugin-active-header-links): keep scroll position during hash updates - #1150
Open
Mister-Hope wants to merge 1 commit into
Open
Mister-Hope wants to merge 1 commit into
Mister-Hope wants to merge 1 commit into
Conversation
…ates When `scroll-behavior: smooth` is enabled, the client scroll restoration starts a smooth scroll that has not moved the viewport yet when the hash is updated, so the position comparison skipped the restoration and the page kept scrolling to the hash element, triggering another hash update. Wait for the restoration to finish with `nextTick()` and always scroll back with the `instant` behavior, which also aborts the pending smooth scroll. The deprecated `scrollBehavior` option is still disabled temporarily for themes that set it.
This branch has not been deployed
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
Fixes #1146.
With
html { scroll-behavior: smooth }, the active header links plugin could make the page scroll indefinitely between two headings.The plugin updates the route hash while scrolling, which triggers the client scroll restoration. The restoration starts a smooth scroll, but the viewport has not moved yet when the plugin compares positions, so it skips restoring the scroll position. The smooth scroll then moves the page to the hash element, which activates another heading and updates the hash again.
Fix
nextTick().behavior: 'instant', which also aborts the pending smooth scroll.scrollBehavioroption so themes that set it do not move the viewport.Tests
pnpm exec vitest run plugins/development/plugin-active-header-links/testspasses.