Fix getAnchoredPosition to account for scroll position when positioning elements #585
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.
Fixes #5358 where
getAnchoredPosition
fails when scroll position causes elements to render out of view.Problem
The
getAnchoredPosition
function looks at available space to determine which side to render a floating element relative to its anchor. However, it doesn't consider scroll position, making it possible for elements to be positioned outside the visible viewport when the page is scrolled.For example, if a user scrolls down on a long page and opens a dropdown menu, the positioning algorithm might choose to render the menu below the anchor because there's space in the container, but this puts the menu below the visible scroll area where users can't see it.
Solution
This PR adds a new feature flag
enableAnchoredPositionViewportFix
to thePositionSettings
interface. When enabled, the positioning algorithm accounts for the current scroll position and viewport size to ensure floating elements remain visible.Changes Made
enableAnchoredPositionViewportFix?: boolean
toPositionSettings
interfacewindow.pageYOffset
andwindow.innerHeight
to calculate the actual visible viewport area instead of just the container boundsExample Usage
Testing
Added comprehensive tests that demonstrate:
All existing tests continue to pass, ensuring no regressions.
References
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.