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

Get scroll parent #65

Merged
merged 9 commits into from
Sep 8, 2022
Merged

Conversation

kevers-google
Copy link
Collaborator

Fix calculation the nearest ancestor scroll container.


case 'absolute':
case 'fixed':
return findClosestAncestor(element, isAbsoluteOrFixedElementContainer);
Copy link
Owner

Choose a reason for hiding this comment

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

This skips step 2:

If the position property is absolute, the containing block is formed by the edge of the padding box of the nearest ancestor element that has a position value other than static (fixed, absolute, relative, or sticky).

tldr; if position absolute, you need to consider the above condition or any fixed container condition.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


default:
return getScrollParent(node.parentNode);
return getScrollParent(containingBlock);
Copy link
Owner

Choose a reason for hiding this comment

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

While technically correct, I do prefer looping over recursion as it's slightly more efficient.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link
Owner

@flackr flackr left a comment

Choose a reason for hiding this comment

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

LGTM with minor cleanup

return getScrollParent(containingBlock);
while (true) {
const containingBlock = getContainingBlock(node);
if (!containingBlock)
Copy link
Owner

Choose a reason for hiding this comment

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

nit: I think it's slightly cleaner to move this to the while condition, e.g.:

while (node = getContainingBlock(node)) {
  const style = getComputedStyle(node);
  switch(style['overflow-x']) {
      case 'auto':
      case 'scroll':
      case 'hidden':
        return node;
  }
}
return document.scrollingElement;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done. Also added missing special case rule for the body element.

// of the first visible child body is applied instead.
if (node.tagName != "BODY" ||
getComputedStyle(document.scrollingElement).overflow != "visible")
return node;
Copy link
Owner

Choose a reason for hiding this comment

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

nit: I think it would be clearer if you flipped the condition and used this case for the exception (the body element in these special circumstances) to return document.scrollingElement and then left the return below as returning the node.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

// The UA must apply the overflow from the root element to the viewport;
// however, if the overflow is visible in both axis, then the overflow
// of the first visible child body is applied instead.
if (node.tagName != "BODY" ||
Copy link
Owner

Choose a reason for hiding this comment

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

As we talked about in person, wouldn't checking node == document.body correctly only apply this behavior to the first child?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link
Owner

@flackr flackr left a comment

Choose a reason for hiding this comment

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

LGTM thanks Kevin!

@kevers-google kevers-google merged commit dcd7e7d into flackr:master Sep 8, 2022
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.

2 participants