From 871b5ab4e44a7bd7c88d783ff225fbc509f1ea64 Mon Sep 17 00:00:00 2001 From: Hector Jimenez Date: Wed, 4 Jun 2025 14:13:23 -0700 Subject: [PATCH 1/2] :P --- .../src/core/GamepadUtils.ts | 22 +++++++++++++++++++ .../src/hooks/useGamepadNavigation.ts | 12 ++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/react-gamepad-navigation/src/core/GamepadUtils.ts b/packages/react-gamepad-navigation/src/core/GamepadUtils.ts index 9e62369e5..bf6ebae78 100644 --- a/packages/react-gamepad-navigation/src/core/GamepadUtils.ts +++ b/packages/react-gamepad-navigation/src/core/GamepadUtils.ts @@ -1,4 +1,5 @@ import { WindowWithFluentGPNShadowDOMAPI } from '../types/FluentGPNShadowDOMAPI'; +import { consolePrefix } from './Constants'; export type TimeoutId = number | undefined; export type IntervalId = number | undefined; @@ -32,3 +33,24 @@ export const getShadowDOMAPI = (targetDocument: Document | undefined) => { ?.__FluentGPNShadowDOMAPI; return shadowDOMAPI; }; + +export const isGamepadAPISupported = ( + targetView: Window | null +): targetView is Window => { + let api = null; + try { + if ( + targetView?.navigator && + typeof targetView.navigator.getGamepads === 'function' + ) { + api = targetView.navigator.getGamepads(); + } + } catch { + console.warn( + consolePrefix, + 'Failed to execute "getGamepads" on "Navigator": Access to the feature "gamepad" is denied' + ); + } + + return api !== null; +}; diff --git a/packages/react-gamepad-navigation/src/hooks/useGamepadNavigation.ts b/packages/react-gamepad-navigation/src/hooks/useGamepadNavigation.ts index f48456aae..567124659 100644 --- a/packages/react-gamepad-navigation/src/hooks/useGamepadNavigation.ts +++ b/packages/react-gamepad-navigation/src/hooks/useGamepadNavigation.ts @@ -4,7 +4,11 @@ import { GamepadState } from '../types/Keys'; import { consolePrefix } from '../core/Constants'; import { isSyntheticMouseEvent } from '../core/GamepadEvents'; import { getGamepadMappings } from '../core/GamepadMappings'; -import { getShadowDOMAPI, IntervalId } from '../core/GamepadUtils'; +import { + getShadowDOMAPI, + IntervalId, + isGamepadAPISupported, +} from '../core/GamepadUtils'; import { isPollingEnabled, setDefaultInputMode, @@ -27,9 +31,9 @@ export const getGamepadStates = (): Map => gamepadStates; // Gamepad polling const updateGamepadState = (targetDocument: Document) => { - const targetView = targetDocument.defaultView; let nextGamepads: (Gamepad | null)[] = []; - if (targetView?.navigator && targetView.navigator.getGamepads) { + const targetView = targetDocument.defaultView; + if (isGamepadAPISupported(targetView)) { nextGamepads = targetView.navigator.getGamepads(); } const nextGamepadStates = new Map(); @@ -230,7 +234,7 @@ export const useGamepadNavigation = (options: GamepadNavigationOptions) => { * we look through the window.navigator's current gamepads and add those to our list to start. */ const targetView: Window | null = targetDocument.defaultView; - if (targetView?.navigator && targetView.navigator.getGamepads) { + if (isGamepadAPISupported(targetView)) { const gamepadsList = targetView.navigator.getGamepads(); for (const gamepad of gamepadsList) { From b93304053953f7d63b2c767eefb5253fafd3f8bb Mon Sep 17 00:00:00 2001 From: Hector Jimenez Date: Thu, 19 Jun 2025 13:49:52 -0700 Subject: [PATCH 2/2] Change files --- ...ad-navigation-3d6c4782-c7de-41ef-ba41-b1a1e69ab0a3.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 change/@fluentui-contrib-react-gamepad-navigation-3d6c4782-c7de-41ef-ba41-b1a1e69ab0a3.json diff --git a/change/@fluentui-contrib-react-gamepad-navigation-3d6c4782-c7de-41ef-ba41-b1a1e69ab0a3.json b/change/@fluentui-contrib-react-gamepad-navigation-3d6c4782-c7de-41ef-ba41-b1a1e69ab0a3.json new file mode 100644 index 000000000..2b91bbce7 --- /dev/null +++ b/change/@fluentui-contrib-react-gamepad-navigation-3d6c4782-c7de-41ef-ba41-b1a1e69ab0a3.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "fix(react-gamepad-navigation): handling Gamepad API access denied", + "packageName": "@fluentui-contrib/react-gamepad-navigation", + "email": "hectorjimenez@outlook.com", + "dependentChangeType": "patch" +}