Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: improve device detection for touch screen laptops with high-DPI displays #15

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading