Skip to content

Commit

Permalink
Add try/catch to getElement helper
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbarbara committed May 31, 2024
1 parent 231672c commit 7022e24
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/modules/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import scroll from 'scroll';
import scrollParent from 'scrollparent';

export function canUseDOM() {
return !!(typeof window !== 'undefined' && window.document && window.document.createElement);
return !!(typeof window !== 'undefined' && window.document?.createElement);
}

/**
Expand Down Expand Up @@ -57,7 +57,16 @@ export function getDocumentHeight(median = true): number {
*/
export function getElement(element: string | HTMLElement): HTMLElement | null {
if (typeof element === 'string') {
return document.querySelector(element);
try {
return document.querySelector(element);
} catch (error: any) {
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.error(error);
}

return null;
}
}

return element;
Expand Down

0 comments on commit 7022e24

Please sign in to comment.