Skip to content

Commit

Permalink
Fix: improve device detection for touch screen laptops with high-DPI …
Browse files Browse the repository at this point in the history
…displays
  • Loading branch information
Waaiez committed Sep 5, 2024
1 parent 78132f2 commit e94ddc0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions apps/web/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ export function createDeviceDetector() {
// Check for touch capability
const hasTouch = 'ontouchstart' in window || navigator.maxTouchPoints > 0;

// Check for laptop-specific indicators
const hasMouseEvents = 'onmousemove' in window;
const hasKeyboard = 'onkeydown' in window;

// Check for mobile-specific browser features
const hasMobileUserAgent =
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
Expand All @@ -377,9 +381,20 @@ export function createDeviceDetector() {
const smallScreen = window.screen.width < 768 || window.screen.height < 768;
const highPixelRatio = window.devicePixelRatio > 1.5;

// Determine if it's a mobile/tablet device
const isMobileOrTablet =
(hasTouch && hasMobileUserAgent) || (hasTouch && (smallScreen || highPixelRatio));
const isMobileOrTablet = (() => {
if (hasTouch && hasMobileUserAgent) {
return true; // Definitely mobile or tablet
} else if (hasTouch && (smallScreen || highPixelRatio)) {
// Additional checks for touchscreen laptops
if (hasMouseEvents && hasKeyboard) {
return false; // Likely a touchscreen laptop
} else {
return true; // Likely mobile or tablet
}
} else {
return false; // Likely not mobile or tablet
}
})();

set({
isDesktop: !isMobileOrTablet,
Expand Down

0 comments on commit e94ddc0

Please sign in to comment.