Skip to content

Commit 92e204d

Browse files
committedSep 10, 2024
Add try/catch to getElement helper
1 parent 76b0eae commit 92e204d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed
 

‎src/modules/dom.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scroll from 'scroll';
22
import scrollParent from 'scrollparent';
33

44
export function canUseDOM() {
5-
return !!(typeof window !== 'undefined' && window.document && window.document.createElement);
5+
return !!(typeof window !== 'undefined' && window.document?.createElement);
66
}
77

88
/**
@@ -57,7 +57,16 @@ export function getDocumentHeight(median = true): number {
5757
*/
5858
export function getElement(element: string | HTMLElement): HTMLElement | null {
5959
if (typeof element === 'string') {
60-
return document.querySelector(element);
60+
try {
61+
return document.querySelector(element);
62+
} catch (error: any) {
63+
if (process.env.NODE_ENV !== 'production') {
64+
// eslint-disable-next-line no-console
65+
console.error(error);
66+
}
67+
68+
return null;
69+
}
6170
}
6271

6372
return element;

0 commit comments

Comments
 (0)
Please sign in to comment.