From 491eeeef7ef4c86677e83463d57d8a335e8790a2 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 6 Jan 2020 13:16:56 -0800 Subject: [PATCH] feat(scrollIntoView): expose scrollIntoViewIfNeeded in api (#382) This also replaces isIntersectingViewport with visibleRatio for more flexibility. --- docs/api.md | 23 +++++++++++++++++------ src/dom.ts | 10 +++++----- src/screenshotter.ts | 2 +- test/assets/offscreenbuttons.html | 19 +++++++++++++++++++ test/elementhandle.spec.js | 27 ++++++++++++++++++++------- 5 files changed, 62 insertions(+), 19 deletions(-) diff --git a/docs/api.md b/docs/api.md index abe581c555..6c328dbf3c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -58,15 +58,16 @@ * [elementHandle.fill(value)](#elementhandlefillvalue) * [elementHandle.focus()](#elementhandlefocus) * [elementHandle.hover([options])](#elementhandlehoveroptions) - * [elementHandle.isIntersectingViewport()](#elementhandleisintersectingviewport) * [elementHandle.ownerFrame()](#elementhandleownerframe) * [elementHandle.press(key[, options])](#elementhandlepresskey-options) * [elementHandle.screenshot([options])](#elementhandlescreenshotoptions) + * [elementHandle.scrollIntoViewIfNeeded()](#elementhandlescrollintoviewifneeded) * [elementHandle.select(...values)](#elementhandleselectvalues) * [elementHandle.setInputFiles(...files)](#elementhandlesetinputfilesfiles) * [elementHandle.toString()](#elementhandletostring) * [elementHandle.tripleclick([options])](#elementhandletripleclickoptions) * [elementHandle.type(text[, options])](#elementhandletypetext-options) + * [elementHandle.visibleRatio()](#elementhandlevisibleratio) - [class: Frame](#class-frame) * [frame.$(selector)](#frameselector) * [frame.$$(selector)](#frameselector-1) @@ -786,9 +787,6 @@ Calls [focus](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus This method scrolls element into view if needed, and then uses [page.mouse](#pagemouse) to hover over the center of the element. If the element is detached from DOM, the method throws an error. -#### elementHandle.isIntersectingViewport() -- returns: <[Promise]<[boolean]>> Resolves to true if the element is visible in the current viewport. - #### elementHandle.ownerFrame() - returns: <[Promise]<[Frame]>> Returns the frame containing the given element. @@ -816,6 +814,15 @@ If `key` is a single character and no modifier keys besides `Shift` are being he This method scrolls element into view if needed, and then uses [page.screenshot](#pagescreenshotoptions) to take a screenshot of the element. If the element is detached from DOM, the method throws an error. +#### elementHandle.scrollIntoViewIfNeeded() +- returns: <[Promise]> Resolves after the element has been scrolled into view. + +This method tries to scroll element into view, unless it is completely visible as defined by [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)'s ```ratio```. See also [elementHandle.visibleRatio()](#elementhandlevisibleratio). + +Throws when ```elementHandle``` does not point to an element [connected](https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected) to a Document or a ShadowRoot. + +> **NOTE** If javascript is disabled, element is scrolled into view even when already completely visible. + #### elementHandle.select(...values) - `...values` <...[string]|[ElementHandle]|[Object]> Options to select. If the `