Files
outline/shared/utils/browser.ts
T
codegen-sh[bot]andTom Moor 758d4edbb9 Upgrade @typescript-eslint dependencies to v8.33.0 (#9363)
* Upgrade @typescript-eslint dependencies from v6.21.0 to v8.33.0

- Updated @typescript-eslint/eslint-plugin from ^6.21.0 to ^8.33.0
- Updated @typescript-eslint/parser from ^6.21.0 to ^8.33.0
- Tested linting functionality to ensure compatibility
- This brings the latest TypeScript ESLint features and bug fixes

* lint

* tsc

---------

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2025-06-01 11:01:15 -04:00

94 lines
2.2 KiB
TypeScript

/**
* Returns true if we're running in the browser.
*/
export const isBrowser = typeof window !== "undefined";
/**
* Returns true if the client is a touch device.
*/
export function isTouchDevice(): boolean {
if (!isBrowser) {
return false;
}
return window.matchMedia?.("(hover: none) and (pointer: coarse)")?.matches;
}
/**
* Returns the safe area insets for the current device.
*/
export function getSafeAreaInsets(): {
top: number;
right: number;
bottom: number;
left: number;
} {
// Check if CSS environment variables are supported
const style = getComputedStyle(document.documentElement);
const supportsEnv = window.CSS?.supports?.("top", "env(safe-area-inset-top)");
if (supportsEnv) {
return {
top: parseFloat(style.getPropertyValue("--sat") || "0"),
right: parseFloat(style.getPropertyValue("--sar") || "0"),
bottom: parseFloat(style.getPropertyValue("--sab") || "0"),
left: parseFloat(style.getPropertyValue("--sal") || "0"),
};
}
// Fallback to zero if not supported
return { top: 0, right: 0, bottom: 0, left: 0 };
}
/**
* Returns true if the client is running on a Mac.
*/
export function isMac(): boolean {
if (!isBrowser) {
return false;
}
return window.navigator.platform === "MacIntel";
}
/**
* Returns true if the client is running on Windows.
*/
export function isWindows(): boolean {
if (!isBrowser) {
return false;
}
return window.navigator.platform === "Win32";
}
export function isSafari(): boolean {
if (!isBrowser) {
return false;
}
const userAgent = window.navigator.userAgent;
return (
userAgent.includes("Safari") &&
!userAgent.includes("Chrome") &&
!userAgent.includes("Chromium")
);
}
let supportsPassive = false;
try {
const opts = Object.defineProperty({}, "passive", {
get() {
supportsPassive = true;
},
});
// @ts-expect-error ts-migrate(2769) testPassive is not a real event
window.addEventListener("testPassive", null, opts);
// @ts-expect-error ts-migrate(2769) testPassive is not a real event
window.removeEventListener("testPassive", null, opts);
} catch (_err) {
// Ignore
}
/**
* Returns true if the client supports passive event listeners
*/
export const supportsPassiveListener = supportsPassive;