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 2 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
29 changes: 0 additions & 29 deletions src/libs/DeviceCapabilities/canUseTouchScreen/index.js

This file was deleted.

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;
38 changes: 38 additions & 0 deletions src/libs/DeviceCapabilities/canUseTouchScreen/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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
*/
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 as ExtendedNavigator).msMaxTouchPoints > 0;
} else {
// Same case as for Navigator - TypeScript thinks that matchMedia is obligatory property of window although it may not be
const mQ = (window as Partial<Window & typeof globalThis>).matchMedia && matchMedia('(pointer:coarse)');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Same case as for Navigator - TypeScript thinks that matchMedia is obligatory property of window although it may not be
const mQ = (window as Partial<Window & typeof globalThis>).matchMedia && matchMedia('(pointer:coarse)');
const mQ = window.matchMedia?.('(pointer:coarse)');

if matchMedia can be undefined, maybe this could be a better approach, couldn't? It will only call the function if it exists.

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 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const hasHoverSupport: HasHoverSupport = () => window.matchMedia('(hover: hover) and (pointer: fine)').matches;
const hasHoverSupport: HasHoverSupport = () => window.matchMedia?.('(hover: hover) and (pointer: fine)').matches;

Based on previous comments about matchMedia being possibly undefined, shouldn't we add checks here as well?


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