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

fix(viewport-ruler): incorrectly caching viewport size #7951

Merged

Conversation

crisbeto
Copy link
Member

  • Currently the ViewportRuler caches the ClientRect of the document.documentElement which is only used for determining the scroll position and is being updated on demand anyway. These changes switch to caching the innerWidth and innerHeight which was the original intent.
  • Adds a getViewportSize method that returns the cached viewport width and height. The reasoning is that for all of the cases where we were using getViewportRect, we were only taking the width and height. getViewportSize helps us avoid the overhead from determining the top, bottom, left and right via the scroll position.

* Currently the `ViewportRuler` caches the `ClientRect` of the `document.documentElement` which is only used for determining the scroll position and is being updated on demand anyway. These changes switch to caching the `innerWidth` and `innerHeight` which was the original intent.
* Adds a `getViewportSize` method that returns the cached viewport width and height. The reasoning is that for all of the cases where we were using `getViewportRect`, we were only taking the `width` and `height`. `getViewportSize` helps us avoid the overhead from determining the `top`, `bottom`, `left` and `right` via the scroll position.
@crisbeto crisbeto added the in progress This issue is currently in progress label Oct 22, 2017
@crisbeto crisbeto self-assigned this Oct 22, 2017
@crisbeto crisbeto requested a review from kara as a code owner October 22, 2017 11:20
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Oct 22, 2017
@crisbeto crisbeto assigned jelbourn and unassigned crisbeto Oct 22, 2017
@crisbeto crisbeto added pr: needs review and removed in progress This issue is currently in progress labels Oct 22, 2017
/** Cached document client rectangle. */
private _documentRect?: ClientRect;
/** Cached viewport dimensions. */
private _viewportSize: {width: number; height: number};
Copy link
Member

Choose a reason for hiding this comment

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

Does reading the window width / height actually cost anything? Do we need to cache it at all?

Copy link
Member Author

Choose a reason for hiding this comment

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

// The top-left-corner of the viewport is determined by the scroll position of the document
// body, normally just (scrollLeft, scrollTop). However, Chrome and Firefox disagree about
// whether `document.body` or `document.documentElement` is the scrolled element, so reading
// `scrollTop` and `scrollLeft` is inconsistent. However, using the bounding rect of
// `document.documentElement` works consistently, where the `top` and `left` values will
// equal negative the scroll position.
const top = -documentRect!.top || document.body.scrollTop || window.scrollY ||
const documentRect = document.documentElement.getBoundingClientRect();
Copy link
Member

Choose a reason for hiding this comment

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

Not for this PR, but thinking about this again, I wonder if there's a better way to get what we're after here than calling getBoundingClientRect here.

Copy link
Member Author

Choose a reason for hiding this comment

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

AFAIK the cross-browser way to do it was something like this: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0.

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker P2 The issue is important to a large percentage of users, with a workaround and removed pr: needs review labels Oct 24, 2017
@andrewseguin andrewseguin merged commit 0d6d9cc into angular:master Nov 2, 2017
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P2 The issue is important to a large percentage of users, with a workaround
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants