[ftr/element] custom scrollIntoView to support fixed header (#28727)

With https://github.com/elastic/kibana/pull/28503 we will be enabling the k7design by default, which adds a fixed header to the top of the page. This causes issues with the default "scroll into view" logic, as elements which are in the top overflow will be scrolled into view but then covered by the header.

My first attempt to solve this was adjusting the layout to only scroll the content below the header. This allowed the [standard scroll into view algorithm](https://drafts.csswg.org/cssom-view/#element-scrolling-members) to function as intended, but had a slightly worse UX on OSes like macOS, and @elastic/kibana-design ultimately pushed back because not allowing the body to scroll has other implications.

Instead I have implemented a `LeadfootElementWrapper#scrollIntoViewIfNecessary()` method which is automatically called before each `#click()` and `#moveMouseTo()` call. This new method scrolls the element into view when necessary, and then additionally adjusts the scroll position of the root scroll element by the necessary pixels if the top of the element is within `layout.fixedHeaderHeight` pixels.
This commit is contained in:
Spencer 2019-01-16 12:37:40 -08:00 committed by GitHub
parent 996f433467
commit e355ec47aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 169 additions and 3 deletions

View file

@ -203,6 +203,34 @@ module.exports = {
},
},
/**
* Files that run in the browser with only node-level transpilation
*/
{
files: [
'test/functional/services/lib/leadfoot_element_wrapper/scroll_into_view_if_necessary.js',
],
rules: {
'prefer-object-spread/prefer-object-spread': 'off',
'no-var': 'off',
'prefer-const': 'off',
'prefer-destructuring': 'off',
'no-restricted-syntax': [
'error',
'ArrowFunctionExpression',
'AwaitExpression',
'ClassDeclaration',
'RestElement',
'SpreadElement',
'YieldExpression',
'VariableDeclaration[kind="const"]',
'VariableDeclaration[kind="let"]',
'VariableDeclarator[id.type="ArrayPattern"]',
'VariableDeclarator[id.type="ObjectPattern"]',
],
},
},
/**
* Files that run AFTER node version check
* and are not also transpiled with babel

View file

@ -34,6 +34,32 @@ THE SOFTWARE.
This product uses Noto fonts that are licensed under the SIL Open
Font License, Version 1.1.
---
Based on the scroll-into-view-if-necessary module from npm
https://github.com/stipsan/compute-scroll-into-view/blob/master/src/index.ts#L269-L340
MIT License
Copyright (c) 2018 Cody Olsen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---
Pretty handling of logarithmic axes.
Copyright (c) 2007-2014 IOLA and Ole Laursen.

View file

@ -181,4 +181,9 @@ export const schema = Joi.object().keys({
failureDebugging: Joi.object().keys({
htmlDirectory: Joi.string().default(defaultRelativeToConfigPath('failure_debug/html'))
}).default(),
// settings for the find service
layout: Joi.object().keys({
fixedHeaderHeight: Joi.number().default(0),
}).default(),
}).default();

View file

@ -27,9 +27,10 @@ export function FindProvider({ getService }) {
const WAIT_FOR_EXISTS_TIME = config.get('timeouts.waitForExists');
const defaultFindTimeout = config.get('timeouts.find');
const fixedHeaderHeight = config.get('layout.fixedHeaderHeight');
const wrap = leadfootElement => (
new LeadfootElementWrapper(leadfootElement, leadfoot)
new LeadfootElementWrapper(leadfootElement, leadfoot, fixedHeaderHeight)
);
const wrapAll = leadfootElements => (

View file

@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
export { LeadfootElementWrapper } from './leadfoot_element_wrapper';

View file

@ -17,18 +17,21 @@
* under the License.
*/
import { scrollIntoViewIfNecessary } from './scroll_into_view_if_necessary';
export class LeadfootElementWrapper {
constructor(leadfootElement, leadfoot) {
constructor(leadfootElement, leadfoot, fixedHeaderHeight) {
if (leadfootElement instanceof LeadfootElementWrapper) {
return leadfootElement;
}
this._leadfootElement = leadfootElement;
this._leadfoot = leadfoot;
this._fixedHeaderHeight = fixedHeaderHeight;
}
_wrap(otherLeadfootElement) {
return new LeadfootElementWrapper(otherLeadfootElement, this._leadfoot);
return new LeadfootElementWrapper(otherLeadfootElement, this._leadfoot, this._fixedHeaderHeight);
}
_wrapAll(otherLeadfootElements) {
@ -42,6 +45,7 @@ export class LeadfootElementWrapper {
* @return {Promise<void>}
*/
async click() {
await this.scrollIntoViewIfNecessary();
await this._leadfootElement.click();
}
@ -222,6 +226,7 @@ export class LeadfootElementWrapper {
* @return {Promise<void>}
*/
async moveMouseTo(xOffset, yOffset) {
await this.scrollIntoViewIfNecessary();
return await this._leadfoot.moveMouseTo(this._leadfootElement, xOffset, yOffset);
}
@ -324,4 +329,14 @@ export class LeadfootElementWrapper {
async pressKeys(...args) {
await this._leadfoot.pressKeys(...args);
}
/**
* Scroll the element into view, avoiding the fixed header if necessary
*
* @nonstandard
* @return {Promise<void>}
*/
async scrollIntoViewIfNecessary() {
await this._leadfoot.execute(scrollIntoViewIfNecessary, [this._leadfootElement, this._fixedHeaderHeight]);
}
}

View file

@ -0,0 +1,71 @@
/* eslint-disable @kbn/license-header/require-license-header */
/**
* @notice
* Based on the scroll-into-view-if-necessary module from npm
* https://github.com/stipsan/compute-scroll-into-view/blob/master/src/index.ts#L269-L340
*
* MIT License
*
* Copyright (c) 2018 Cody Olsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export function scrollIntoViewIfNecessary(target, fixedHeaderHeight) {
var rootScroller = document.scrollingElement || document.documentElement;
if (!rootScroller) {
throw new Error('Unable to find document.scrollingElement or document.documentElement');
}
var rootRect = rootScroller.getBoundingClientRect();
var targetRect = target.getBoundingClientRect();
var viewportHeight = window.visualViewport
? visualViewport.height
: window.innerHeight;
var viewportWidth = window.visualViewport
? visualViewport.width
: window.innerWidth;
function isInView() {
return (
targetRect.top >= 0 &&
targetRect.left >= 0 &&
targetRect.bottom <= viewportHeight &&
targetRect.right <= viewportWidth &&
targetRect.top >= rootRect.top &&
targetRect.bottom <= rootRect.bottom &&
targetRect.left >= rootRect.left &&
targetRect.right <= rootRect.right
);
}
if (!isInView()) {
target.scrollIntoView();
}
var boundingRect = target.getBoundingClientRect();
var additionalScrollNecessary = fixedHeaderHeight - boundingRect.top;
if (additionalScrollNecessary > 0) {
rootScroller.scrollTop = rootScroller.scrollTop - additionalScrollNecessary;
}
}