-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create helpers for working with the window as part esl-util DOM…
… helpers
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}; | ||
} |