diff --git a/src/modules/esl-popup/core/calcPosition.ts b/src/modules/esl-popup/core/calcPosition.ts index 375074c08..1175130a6 100644 --- a/src/modules/esl-popup/core/calcPosition.ts +++ b/src/modules/esl-popup/core/calcPosition.ts @@ -29,7 +29,7 @@ export interface ElementRect extends ObjectRect { cy: number; } -export interface IntersetionRatioRect { +export interface IntersectionRatioRect { top?: number; left?: number; right?: number; @@ -39,7 +39,7 @@ export interface IntersetionRatioRect { export interface PopupPositionConfig { position: PositionType; behavior: string; - intersectionRatio: IntersetionRatioRect, + intersectionRatio: IntersectionRatioRect, element: DOMRect; inner: ElementRect; outer: ObjectRect; diff --git a/src/modules/esl-popup/core/esl-popup.ts b/src/modules/esl-popup/core/esl-popup.ts index 916e76009..23449291f 100644 --- a/src/modules/esl-popup/core/esl-popup.ts +++ b/src/modules/esl-popup/core/esl-popup.ts @@ -4,12 +4,12 @@ import {bind} from '../../esl-utils/decorators/bind'; import {prop} from '../../esl-utils/decorators/prop'; import {rafDecorator} from '../../esl-utils/async/raf'; import {ESLToggleable} from '../../esl-toggleable/core'; - -import {listScrollParents} from './listScrollParents'; +import {getListScrollParents} from '../../esl-utils/dom/scroll'; +import {getWindowRect} from '../../esl-utils/dom/window'; import {calcPopupPosition, resizeRect} from './calcPosition'; import type {ToggleableActionParams} from '../../esl-toggleable/core'; -import type {PositionType, IntersetionRatioRect} from './calcPosition'; +import type {PositionType, IntersectionRatioRect} from './calcPosition'; const INTERSECTION_LIMIT_FOR_ADJACENT_AXIS = 0.7; @@ -39,7 +39,7 @@ export class ESLPopup extends ESLToggleable { protected _offsetWindow: number; protected _deferredUpdatePosition = rafDecorator(() => this._updatePosition()); protected _activatorObserver: ActivatorObserver; - protected _intersectionRatio: IntersetionRatioRect = {}; + protected _intersectionRatio: IntersectionRatioRect = {}; @attr({defaultValue: 'top'}) public position: PositionType; @attr({defaultValue: 'fit'}) public behavior: string; @@ -82,35 +82,6 @@ export class ESLPopup extends ESLToggleable { return ['top', 'bottom'].includes(this.position); } - // TODO: move to utilities - protected get _windowWidth() { - // return document.documentElement.clientWidth || document.body.clientWidth; - return window.innerWidth || document.documentElement.clientWidth; - } - - protected get _windowHeight() { - return window.innerHeight || document.documentElement.clientHeight; - } - - protected get _windowBottom() { - return window.pageYOffset + this._windowHeight; - } - - protected get _windowRight() { - return window.pageXOffset + this._windowWidth; - } - - protected get _windowRect() { - return { - top: window.pageYOffset + this._offsetWindow, - left: window.pageXOffset + this._offsetWindow, - right: this._windowRight - this._offsetWindow, - bottom: this._windowBottom - this._offsetWindow, - height: this._windowHeight, - width: this._windowWidth - }; - } - public onShow(params: PopupActionParams) { super.onShow(params); @@ -172,7 +143,7 @@ export class ESLPopup extends ESLToggleable { } protected _addActivatorObserver(target: HTMLElement) { - const scrollParents = listScrollParents(target); + const scrollParents = getListScrollParents(target); const unsubscribers = scrollParents.map(($root) => { const options = {passive: true} as EventListenerOptions; @@ -238,7 +209,7 @@ export class ESLPopup extends ESLToggleable { element: popupRect, trigger, inner: resizeRect(trigger, innerMargin), - outer: resizeRect(this._windowRect, -this._offsetWindow) + outer: resizeRect(getWindowRect(), -this._offsetWindow) }; const {left, top, arrow} = calcPopupPosition(config); diff --git a/src/modules/esl-popup/core/getComputedStyle.ts b/src/modules/esl-popup/core/getComputedStyle.ts deleted file mode 100644 index 2aad00f6f..000000000 --- a/src/modules/esl-popup/core/getComputedStyle.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {getWindow} from './getWindow'; - -export function getComputedStyle(element: Element): CSSStyleDeclaration { - return getWindow(element).getComputedStyle(element); -} diff --git a/src/modules/esl-popup/core/getDocumentElement.ts b/src/modules/esl-popup/core/getDocumentElement.ts deleted file mode 100644 index 6746fcb55..000000000 --- a/src/modules/esl-popup/core/getDocumentElement.ts +++ /dev/null @@ -1,5 +0,0 @@ -export function getDocumentElement(element: Element | Window): Element { - return ((element instanceof Window - ? element.document - : element.ownerDocument) || window.document).documentElement; -} diff --git a/src/modules/esl-popup/core/getNodeName.ts b/src/modules/esl-popup/core/getNodeName.ts deleted file mode 100644 index 99162d43f..000000000 --- a/src/modules/esl-popup/core/getNodeName.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function getNodeName(element?: Node | Window): string { - return element && !(element instanceof Window)? (element.nodeName).toLowerCase() : ''; -} diff --git a/src/modules/esl-popup/core/getParentNode.ts b/src/modules/esl-popup/core/getParentNode.ts deleted file mode 100644 index 36ae9c765..000000000 --- a/src/modules/esl-popup/core/getParentNode.ts +++ /dev/null @@ -1,12 +0,0 @@ -import {getNodeName} from './getNodeName'; -import {getDocumentElement} from './getDocumentElement'; - -export function getParentNode(element: Element | ShadowRoot): Node { - if (getNodeName(element) === 'html') { - return element; - } - - return (element instanceof ShadowRoot - ? element.host - : element.assignedSlot || element.parentNode) || getDocumentElement(element as Element); -} diff --git a/src/modules/esl-popup/core/getScrollParent.ts b/src/modules/esl-popup/core/getScrollParent.ts deleted file mode 100644 index 13da21461..000000000 --- a/src/modules/esl-popup/core/getScrollParent.ts +++ /dev/null @@ -1,15 +0,0 @@ -import {getNodeName} from './getNodeName'; -import {isScrollParent} from './isScrollParent'; -import {getParentNode} from './getParentNode'; - -export function getScrollParent(node: Node): Element { - if (['html', 'body', '#document'].indexOf(getNodeName(node)) >= 0) { - return node.ownerDocument?.body as Element; - } - - if (node instanceof HTMLElement && isScrollParent(node as Element)) { - return node as Element; - } - - return getScrollParent(getParentNode(node as Element)); -} diff --git a/src/modules/esl-popup/core/getWindow.ts b/src/modules/esl-popup/core/getWindow.ts deleted file mode 100644 index 6ddaaa085..000000000 --- a/src/modules/esl-popup/core/getWindow.ts +++ /dev/null @@ -1,12 +0,0 @@ -export function getWindow(node: Node | Window): Window { - if (node == null) { - return window; - } - - if (node instanceof Window) { - return node; - } - - const ownerDocument = node.ownerDocument; - return ownerDocument ? ownerDocument.defaultView || window : window; -} diff --git a/src/modules/esl-popup/core/isScrollParent.ts b/src/modules/esl-popup/core/isScrollParent.ts deleted file mode 100644 index 7b5514e19..000000000 --- a/src/modules/esl-popup/core/isScrollParent.ts +++ /dev/null @@ -1,7 +0,0 @@ -import {getComputedStyle} from './getComputedStyle'; - -export function isScrollParent(element: Element): boolean { - // Firefox wants us to check `-x` and `-y` variations as well - const {overflow, overflowX, overflowY} = getComputedStyle(element); - return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX); -} diff --git a/src/modules/esl-popup/core/listScrollParents.ts b/src/modules/esl-popup/core/listScrollParents.ts deleted file mode 100644 index 256929b0f..000000000 --- a/src/modules/esl-popup/core/listScrollParents.ts +++ /dev/null @@ -1,22 +0,0 @@ -import {getScrollParent} from './getScrollParent'; -import {getParentNode} from './getParentNode'; -import {isScrollParent} from './isScrollParent'; - -/* -given a DOM element, return the list of all scroll parents, up the list of ancesors -until we get to the top window object. This list is what we attach scroll listeners -to, because if any of these parent elements scroll, we'll need to re-calculate the -reference element's position. -*/ -export function listScrollParents(element: Node, list: Element[] = []): Element[] { - const scrollParent = getScrollParent(element); - const isBody = scrollParent === element.ownerDocument?.body; - const target = isBody - ? isScrollParent(scrollParent) ? scrollParent : [] - : scrollParent; - - const updatedList = list.concat(target); - return isBody - ? updatedList - : updatedList.concat(listScrollParents(getParentNode(scrollParent))); -}