Skip to content

Commit ef0aece

Browse files
committed
helper for window object
1 parent a7b70a8 commit ef0aece

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/utils/browser.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export function getBrowserWindow() {
2+
let windowObject;
3+
4+
try {
5+
windowObject ||= globalThis.window;
6+
} catch {
7+
// do nothing
8+
}
9+
10+
try {
11+
windowObject ||= global.window;
12+
} catch {
13+
// do nothing
14+
}
15+
16+
try {
17+
windowObject ||= window;
18+
} catch {
19+
// do nothing
20+
}
21+
22+
return windowObject;
23+
}
24+
25+
export function isBrowser() {
26+
const window = getBrowserWindow()
27+
28+
return window !== undefined
29+
&& typeof window === 'object';
30+
}

0 commit comments

Comments
 (0)