Don't scroll parents of absolutely-positioned elements #179
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "master"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
FIX: Don't try to scroll absolutely-positioned elements into view by scrolling their parent elements.
I've discovered a minor bug with how
scrollToSelectionworks withinprosemirror-viewas well as an easy fix for it.Reproduction
Here is a screen recording which shows the issue occurring. After some modifications to the CSS to re-create the layout of my use-case.
https://github.com/user-attachments/assets/2b31e615-310b-42ea-8c2f-d3367b2441ab
To reproduce this, I wrote a small script that can be pasted into a browser's console on this example. Though, the specific values will likely need to be modified as it depends on your viewport's size.
The gist of this reproduction is that a parent of the editor element has absolute positioning and is a descendant of an element which is scrollable.
It is invalid to be scrolling the parent, because the editor is absolutely positioned and therefore in a different context as it's ancestor.
Potential Solution
When recursively searching for offsets to scroll by, there already is an existing check to stop iterating when an element has either
fixedorstickypositioning, I believe this will also need to check forabsolutepositioning. Like so:The reasoning is that, like
fixedandstickypositioned elements, absolutely positioned elements break from their parent's layout (especially when positioned relative to another element withposition: relative). Therefore, it is not valid to attempt to scroll the parent of an absolutely positioned element, since is not within the same layout context.Stopping this loop for absolutely positioned elements doesn't seem like a good idea. It is entirely possible for such an element to be within a scrollable parent that needs to be scrolled to display the cursor.
In fact, your screencast doesn't seem to show a regular absolutely positioned element—I'd expect that to move along with the rest of the page, as the page scrolls.
The element definitely can be within a scrollable parent, but it isn't a 1-1 correlation that the scrollable parent would actually affect the positioning of an absolutely positioned element (i.e. to actually make the element be within view).
The reason that the element does not scroll is that in my screen recording, the element which is scrolling does not have
position: relativeset on it, causing theabsolutepositioning to be relative to the page. You can reproduce it with this example: https://stackblitz.com/edit/stackblitz-starters-5yhtzbdi?file=styles.cssVideo:
https://github.com/user-attachments/assets/beaad0cf-d711-4306-b1e0-c9dab632c10f
Which is the problem, absolutely positioned elements are only absolutely positioned relative to their nearest non-static position.
So, I guess that the technically correct thing to do here is that, for absolutely positioned elements, is to skip any ancestors which are not statically positioned after encountering an absolutely positioned element.
I think something like this patch should make it more sound than ignoring absolutely positioned elements altogether
I think using
offsetParent, as in attached patch, makes this skipping more straightforward.I totally agree, wasn't aware of that API.
Appreciate your help here @marijnh!
@marijnh Thanks for this! would you be able to release the fix?
Certainly. I've tagged a version 1.38.1
Pull request closed