Skip to content

fix(plugin-active-header-links): keep scroll position during hash updates - #1150

Open
Mister-Hope wants to merge 1 commit into
mainfrom
fix-active-header-links-scroll
Open

Mister-Hope wants to merge 1 commit into
mainfrom
fix-active-header-links-scroll

Conversation

@Mister-Hope

Copy link
Copy Markdown
Member

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

  • Wait for the client scroll restoration to finish with nextTick().
  • Always restore the scroll position with behavior: 'instant', which also aborts the pending smooth scroll.
  • Still temporarily disable the deprecated scrollBehavior option so themes that set it do not move the viewport.

Tests

  • Added a regression test that mimics the client scroll restoration with a smooth scroll and asserts the position is restored and the pending scroll aborted.
  • pnpm exec vitest run plugins/development/plugin-active-header-links/tests passes.

…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

No deployments
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.

[Bug report] Active header links can scroll indefinitely between headings with CSS smooth scrolling

1 participant