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

[No QA] [TS migration] Migrate 'DeviceCapabilities' lib to TypeScript #27314

Merged
Show file tree
Hide file tree
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import CanUseTouchScreen from './types';

const canUseTouchScreen: CanUseTouchScreen = () => true;

export default canUseTouchScreen;
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import {Merge} from 'type-fest';
import CanUseTouchScreen from './types';

type ExtendedNavigator = Merge<Navigator, {msMaxTouchPoints: number}>;

/**
* Allows us to identify whether the platform has a touchscreen.
*
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
*
* @returns {Boolean}
*/
function canUseTouchScreen() {
const canUseTouchScreen: CanUseTouchScreen = () => {
let hasTouchScreen = false;

// TypeScript removed support for msMaxTouchPoints, this doesn't mean however that
// this property doesn't exist - hence the use of ExtendedNavigator to ensure
// that the functionality doesn't change
// https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1029
if ('maxTouchPoints' in navigator) {
hasTouchScreen = navigator.maxTouchPoints > 0;
} else if ('msMaxTouchPoints' in navigator) {
hasTouchScreen = navigator.msMaxTouchPoints > 0;
hasTouchScreen = (navigator as ExtendedNavigator).msMaxTouchPoints > 0;
} else {
const mQ = window.matchMedia && matchMedia('(pointer:coarse)');
// Same case as for Navigator - TypeScript thinks that matchMedia is obligatory property of window although it may not be
const mQ = window.matchMedia?.('(pointer:coarse)');
if (mQ && mQ.media === '(pointer:coarse)') {
hasTouchScreen = !!mQ.matches;
} else if ('orientation' in window) {
hasTouchScreen = true; // deprecated, but good fallback
} else {
// Only as a last resort, fall back to user agent sniffing
const UA = navigator.userAgent;
const UA = (navigator as ExtendedNavigator).userAgent;
hasTouchScreen = /\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) || /\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA);
}
}
return hasTouchScreen;
}
};

export default canUseTouchScreen;
3 changes: 3 additions & 0 deletions src/libs/DeviceCapabilities/canUseTouchScreen/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type CanUseTouchScreen = () => boolean;

export default CanUseTouchScreen;
10 changes: 0 additions & 10 deletions src/libs/DeviceCapabilities/hasHoverSupport/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import HasHoverSupport from './types';

/**
* Allows us to identify whether the platform is hoverable.
*
* @returns {Boolean}
*/

const hasHoverSupport = () => false;
const hasHoverSupport: HasHoverSupport = () => false;

export default hasHoverSupport;
8 changes: 8 additions & 0 deletions src/libs/DeviceCapabilities/hasHoverSupport/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import HasHoverSupport from './types';

/**
* Allows us to identify whether the platform is hoverable.
*/
const hasHoverSupport: HasHoverSupport = () => window.matchMedia?.('(hover: hover) and (pointer: fine)').matches;

export default hasHoverSupport;
3 changes: 3 additions & 0 deletions src/libs/DeviceCapabilities/hasHoverSupport/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type HasHoverSupport = () => boolean;

export default HasHoverSupport;
Loading