@dfinity/gix-components
Version:
A UI kit developed by the GIX team
61 lines (60 loc) • 1.65 kB
JavaScript
import { isNode } from "./env.utils";
import { nonNullish } from "@dfinity/utils";
export const isPortrait = () => {
if (isNode()) {
return false;
}
return (window.matchMedia && window.matchMedia("(orientation: portrait)").matches);
};
export const isIOS = () => {
if (isNode()) {
return false;
}
const agent = userAgent();
return /iPhone|iPod/i.test(agent) || isIPad();
};
export const isIPad = () => {
if (isNode()) {
return false;
}
const agent = userAgent();
// iOS 12 and below
if (/iPad/i.test(agent)) {
return true;
}
// iOS 13+
return /Macintosh/i.test(agent) && isMobile();
};
export const isAndroid = () => {
if (isNode()) {
return false;
}
const a = userAgent();
return /android|sink/i.test(a);
};
export const isAndroidTablet = () => {
if (isNode()) {
return false;
}
const a = userAgent();
return isAndroid() && !/mobile/i.test(a);
};
/**
* Current loophole (Feb 2024):
* - Firefox used on a desktop with touch screen will be identified as a mobile device.
*/
export const isMobile = () => {
if (isNode()) {
return false;
}
// Available in Chrome on any devices (Feb 2024)
// https://caniuse.com/mdn-api_navigator_useragentdata
if ("userAgentData" in navigator && nonNullish(navigator.userAgentData)) {
return navigator.userAgentData.mobile;
}
// Legacy
const isTouchScreen = window.matchMedia("(any-pointer:coarse)").matches;
return isTouchScreen;
};
export const isDesktop = () => !isMobile();
const userAgent = () => navigator.userAgent;