-
Notifications
You must be signed in to change notification settings - Fork 0
/
envs.ts
89 lines (84 loc) · 1.96 KB
/
envs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Server & browser
*
*
*/
export function isServer(): boolean {
return typeof window === "undefined"
}
export function isSSR(): boolean {
return isServer()
}
export function isBrowser(): boolean {
return !isServer()
}
export function serverOnly(callback: () => any) {
return isServer() ? callback?.() : null
}
export function browserOnly(callback: () => any) {
return isBrowser() ? callback?.() : null
}
/**
* Mobile
*
*
*/
export function isAndroid(): boolean {
return browserOnly(() => /android/i.test(navigator.userAgent))
}
export function isWindowsPhone(): boolean {
return browserOnly(() => /windows phone/i.test(navigator.userAgent))
}
export function isiPhone(): boolean {
return browserOnly(() => /iPhone/i.test(navigator.userAgent))
}
export function isiPod(): boolean {
return browserOnly(() => /iPod/i.test(navigator.userAgent))
}
export function isiPad(): boolean {
return browserOnly(() => /iPad/i.test(navigator.userAgent))
}
export function isiOS(): boolean {
return browserOnly(() => isiPhone() || isiPod() || isiPad())
}
/**
* Devices
*
*
*/
export function isHandheldDevice(): boolean {
return browserOnly(() =>
/(tablet|ipad|playbook|silk)|(android(?!.*mobi))|Mobile|Android|iP(hone|od)|IEMobile|BlackBerry|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/i.test(
navigator.userAgent
)
)
}
export function isTouchDevice() {
return browserOnly(
() =>
"ontouchstart" in window ||
navigator.maxTouchPoints > 0 ||
navigator?.["msMaxTouchPoints"] > 0
)
}
/**
* Browser
*
*
*/
export function isChrome(): boolean {
return browserOnly(
() =>
/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor)
)
}
export function isFirefox(): boolean {
return browserOnly(
() => navigator.userAgent.toLowerCase().indexOf("firefox") > -1
)
}
export function isSafari(): boolean {
return browserOnly(
() => navigator.userAgent.toLowerCase().indexOf("safari") > -1
)
}