Skip to content

Commit 0e10107

Browse files
committed
feat(utils): add platform utils
1 parent bd0fc81 commit 0e10107

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"@pansy/classnames": "^1.0.1",
4747
"@pansy/scrollbar-width": "^1.0.0",
4848
"tree-lodash": "^0.3.1",
49-
"type-fest": "^4.9.0"
49+
"type-fest": "^4.9.0",
50+
"user-agent-data-types": "^0.4.2"
5051
},
5152
"devDependencies": {
5253
"@commitlint/cli": "^18.4.4",

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export { default as classNames } from '@pansy/classnames';
22
export { scrollbarWidth } from '@pansy/scrollbar-width';
33
export * from './rect';
4-
export { isIOS } from './isIOS';
4+
export * from './platform';
55
export { isBrowser } from './isBrowser';
66
export { isUrl } from './isUrl';
77
export { isNil } from './isNil';

src/utils/isIOS.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/utils/platform.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/// <reference types="user-agent-data-types" />
2+
3+
function testUserAgent(re: RegExp) {
4+
if (typeof window === 'undefined' || window.navigator == null) {
5+
return false;
6+
}
7+
return (
8+
window.navigator['userAgentData']?.brands.some((brand: { brand: string; version: string }) =>
9+
re.test(brand.brand),
10+
) || re.test(window.navigator.userAgent)
11+
);
12+
}
13+
14+
function testPlatform(re: RegExp) {
15+
return typeof window !== 'undefined' && window.navigator != null
16+
? re.test(window.navigator['userAgentData']?.platform || window.navigator.platform)
17+
: false;
18+
}
19+
20+
export function isMac() {
21+
return testPlatform(/^Mac/i);
22+
}
23+
24+
export function isIPhone() {
25+
return testPlatform(/^iPhone/i);
26+
}
27+
28+
export function isIPad() {
29+
return (
30+
testPlatform(/^iPad/i) ||
31+
// iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
32+
(isMac() && navigator.maxTouchPoints > 1)
33+
);
34+
}
35+
36+
export function isAndroid() {
37+
return testUserAgent(/Android/i);
38+
}
39+
40+
export function isIOS() {
41+
return isIPhone() || isIPad();
42+
}
43+
44+
export function isAppleDevice() {
45+
return isMac() || isIOS();
46+
}
47+
48+
export function isWebKit() {
49+
return testUserAgent(/AppleWebKit/i) && !isChrome();
50+
}
51+
52+
export function isChrome() {
53+
return testUserAgent(/Chrome/i);
54+
}
55+
56+
export function isFirefox() {
57+
return testUserAgent(/Firefox/i);
58+
}

0 commit comments

Comments
 (0)