generated from nichoth/template-ts-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
6 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
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
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
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,41 @@ | ||
/** | ||
* Converts querySelector string to an HTMLElement or validates an | ||
* existing HTMLElement. | ||
* | ||
* | ||
* @param {string|Element|HTMLElement} selector - A CSS selector string, or an | ||
* instance of an Element. | ||
* @returns {Element} The HTMLElement, Element, or Window that corresponds to | ||
* the selector. | ||
* @throws {Error} Throws an error if the `selector` is not a string that | ||
* resolves to an HTMLElement, or not an instance of | ||
* HTMLElement, Element, or Window. | ||
* | ||
*/ | ||
export function toElement (_selector:string|HTMLElement|Element) { | ||
let selector:string|Element|null = _selector | ||
if (globalThis.document) { | ||
if (typeof selector === 'string') { | ||
selector = globalThis.document.querySelector(selector) | ||
} | ||
|
||
if (!( | ||
selector instanceof globalThis.HTMLElement || | ||
selector instanceof globalThis.Element | ||
)) { | ||
throw new Error('`stringOrElement` needs to be an instance of ' + | ||
'HTMLElement or a querySelector that resolves to an HTMLElement') | ||
} | ||
|
||
return selector | ||
} | ||
} | ||
|
||
export async function requestAnimationFrame ():Promise<void> { | ||
if (globalThis.document && globalThis.document.hasFocus()) { | ||
// RAF only works when the window is focused | ||
await new Promise(resolve => globalThis.requestAnimationFrame(resolve)) | ||
} else { | ||
await new Promise((resolve) => setTimeout(resolve, 0)) | ||
} | ||
} |
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
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