-
Notifications
You must be signed in to change notification settings - Fork 23
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
#8380: add isInViewport property to element reader #8381
Changes from 1 commit
cece926
92b27f9
1166461
d519b16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,11 +132,27 @@ export async function waitForBody(): Promise<void> { | |
|
||
/** | ||
* Return true if the element is visible (i.e. not in `display: none`), even if outside the viewport | ||
* See https://developer.mozilla.org/en-US/docs/Web/API/Element/checkVisibility | ||
*/ | ||
export function isVisible(element: HTMLElement): boolean { | ||
return element.checkVisibility(); | ||
} | ||
|
||
/** | ||
* Returns true if the element is completely in the viewport. | ||
*/ | ||
export function isInViewport(element: HTMLElement): boolean { | ||
// https://stackoverflow.com/a/125106 | ||
const rect = element.getBoundingClientRect(); | ||
return ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also have isVisible exposed on ElementReader. So I think it's good to keep them separate. Although, related to your comment here: #8381 (comment), the devil's advocate is that ideally for the customer's use case we'd expose a property that captures whether or not the element would be fully captured by our Screenshot Brick. We just happen to know there's no funny business going on the site they need to screenshot, so the method as-is would be sufficient. Perhaps there's a better name we could use the method/output property? Although, from the Google/SO Results, this is what most developers seem to call it. (Although a good proportion of them seem to use it to mean the element is at least partially in the viewport) Some other name options for brainstorming:
Although "position" might be confusing w.r.t. CSS position properties There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Maybe I'm missing the context, but is this for a specific customer? I think it's fine to leave the methodName and functionality as-is, but I would recommend updating the jsdoc to be more comprehensive on the behavior and limitations of this function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes. To see that you can look for the "customer" label and user story in the issue: #8380. Adding the customer name epic now
👍 |
||
rect.top >= 0 && | ||
rect.left >= 0 && | ||
rect.bottom <= | ||
(window.innerHeight || document.documentElement.clientHeight) && | ||
rect.right <= (window.innerWidth || document.documentElement.clientWidth) | ||
); | ||
} | ||
|
||
/** | ||
* Returns a callback that runs only when the document is visible. | ||
* - If the document is visible, runs immediately | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should note on this method that this doesn't work for elements hidden by z-index overlapped, or if they are hidden by overflow-scroll in element's container. This may also have issues with things hidden by relative/absolute positioning, and if they are within an iframe that is not entirely visible in the top frame.