UNPKG

1.82 kBPlain TextView Raw
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13function testUserAgent(re: RegExp) {
14 if (typeof window === 'undefined' || window.navigator == null) {
15 return false;
16 }
17 return (
18 window.navigator['userAgentData']?.brands.some((brand: {brand: string, version: string}) => re.test(brand.brand))
19 ) ||
20 re.test(window.navigator.userAgent);
21}
22
23function testPlatform(re: RegExp) {
24 return typeof window !== 'undefined' && window.navigator != null
25 ? re.test(window.navigator['userAgentData']?.platform || window.navigator.platform)
26 : false;
27}
28
29export function isMac() {
30 return testPlatform(/^Mac/i);
31}
32
33export function isIPhone() {
34 return testPlatform(/^iPhone/i);
35}
36
37export function isIPad() {
38 return testPlatform(/^iPad/i) ||
39 // iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
40 (isMac() && navigator.maxTouchPoints > 1);
41}
42
43export function isIOS() {
44 return isIPhone() || isIPad();
45}
46
47export function isAppleDevice() {
48 return isMac() || isIOS();
49}
50
51export function isWebKit() {
52 return testUserAgent(/AppleWebKit/i) && !isChrome();
53}
54
55export function isChrome() {
56 return testUserAgent(/Chrome/i);
57}
58
59export function isAndroid() {
60 return testUserAgent(/Android/i);
61}