Skip to content

Commit

Permalink
feat: create helpers for working with the window as part esl-util DOM…
Browse files Browse the repository at this point in the history
… helpers
  • Loading branch information
dshovchko committed Jul 28, 2021
1 parent a213262 commit 49a58b2
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/modules/esl-utils/dom/window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Get the window object associated with a document of the specified element.
* @param node - element for which to get window
* */
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;
}

/**
* Get the bottom coordinate value of the window.
* */
export function getWindowBottom(): number {
return window.pageYOffset + getWindowHeight();
}

/**
* Get the left coordinate value of the window.
* */
export function getWindowLeft(): number {
return window.pageXOffset;
}

/**
* Get the right coordinate value of the window.
* */
export function getWindowRight(): number {
return window.pageXOffset + getWindowWidth();
}

/**
* Get the top coordinate value of the window.
* */
export function getWindowTop(): number {
return window.pageYOffset;
}

/**
* Get the width of the window.
* */
export function getWindowHeight(): number {
return window.innerHeight || window.document.documentElement.clientHeight;
}

/**
* Get the height of the window.
* */
export function getWindowWidth(): number {
return window.innerWidth || window.document.documentElement.clientWidth;
}

/**
* Get the size and position of the window.
* @returns
*/
export function getWindowRect() {
return {
top: getWindowTop(),
left: getWindowLeft(),
right: getWindowRight(),
bottom: getWindowBottom(),
height: getWindowHeight(),
width: getWindowWidth()
};
}

0 comments on commit 49a58b2

Please sign in to comment.