diff --git a/src/libs/Browser/index.ts b/src/libs/Browser/index.ts index 2bd43ef698ec..1f4b0141a94b 100644 --- a/src/libs/Browser/index.ts +++ b/src/libs/Browser/index.ts @@ -1,9 +1,11 @@ -import type {GetBrowser, IsMobile, IsMobileChrome, IsMobileIOS, IsMobileSafari, IsMobileSafariOnIos26, IsMobileWebKit, IsSafari} from './types'; +import type {GetBrowser, IsIPadInDesktopMode, IsMobile, IsMobileChrome, IsMobileIOS, IsMobileSafari, IsMobileSafariOnIos26, IsMobileWebKit, IsSafari} from './types'; const getBrowser: GetBrowser = () => ''; const isMobile: IsMobile = () => false; +const isIPadInDesktopMode: IsIPadInDesktopMode = () => false; + const isMobileIOS: IsMobileIOS = () => false; const isMobileSafari: IsMobileSafari = () => false; @@ -16,4 +18,4 @@ const isSafari: IsSafari = () => false; const isMobileSafariOnIos26: IsMobileSafariOnIos26 = () => false; -export {getBrowser, isMobile, isMobileIOS, isMobileSafari, isMobileWebKit, isSafari, isMobileSafariOnIos26, isMobileChrome}; +export {getBrowser, isMobile, isIPadInDesktopMode, isMobileIOS, isMobileSafari, isMobileWebKit, isSafari, isMobileSafariOnIos26, isMobileChrome}; diff --git a/src/libs/Browser/index.web.ts b/src/libs/Browser/index.web.ts index 6064c4aaab7e..bdc12eed28f9 100644 --- a/src/libs/Browser/index.web.ts +++ b/src/libs/Browser/index.web.ts @@ -2,7 +2,7 @@ import getOSAndName from '@libs/actions/Device/getDeviceInfo/getOSAndName'; import CONST from '@src/CONST'; -import type {GetBrowser, IsMobile, IsMobileChrome, IsMobileIOS, IsMobileSafari, IsMobileSafariOnIos26, IsMobileWebKit, IsSafari} from './types'; +import type {GetBrowser, IsIPadInDesktopMode, IsMobile, IsMobileChrome, IsMobileIOS, IsMobileSafari, IsMobileSafariOnIos26, IsMobileWebKit, IsSafari} from './types'; /** * Fetch browser name from UA string @@ -41,6 +41,12 @@ const getBrowser: GetBrowser = () => { */ const isMobile: IsMobile = () => /Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Silk|Opera Mini/i.test(navigator.userAgent); +/** + * iPadOS Safari in "Request Desktop Website" mode (the default) reports a Macintosh user agent that + * isMobile() can't recognize; real Macs report zero touch points. + */ +const isIPadInDesktopMode: IsIPadInDesktopMode = () => /Macintosh/i.test(navigator.userAgent) && navigator.maxTouchPoints > 1; + const isMobileIOS: IsMobileIOS = () => { const userAgent = navigator.userAgent; return /iP(ad|od|hone)/i.test(userAgent); @@ -78,4 +84,4 @@ const isMobileSafariOnIos26: IsMobileSafariOnIos26 = (): boolean => { return isMobileSafari() && getOSAndName().osVersion === '26'; }; -export {getBrowser, isMobile, isMobileIOS, isMobileSafari, isMobileWebKit, isSafari, isMobileChrome, isMobileSafariOnIos26}; +export {getBrowser, isMobile, isIPadInDesktopMode, isMobileIOS, isMobileSafari, isMobileWebKit, isSafari, isMobileChrome, isMobileSafariOnIos26}; diff --git a/src/libs/Browser/types.ts b/src/libs/Browser/types.ts index b99436683c23..8ba8fb96f2b5 100644 --- a/src/libs/Browser/types.ts +++ b/src/libs/Browser/types.ts @@ -2,6 +2,8 @@ type GetBrowser = () => string; type IsMobile = () => boolean; +type IsIPadInDesktopMode = () => boolean; + type IsMobileIOS = () => boolean; type IsMobileSafari = () => boolean; @@ -14,4 +16,4 @@ type IsSafari = () => boolean; type IsMobileSafariOnIos26 = () => boolean; -export type {GetBrowser, IsMobile, IsMobileIOS, IsMobileSafari, IsMobileChrome, IsMobileWebKit, IsSafari, IsMobileSafariOnIos26}; +export type {GetBrowser, IsMobile, IsIPadInDesktopMode, IsMobileIOS, IsMobileSafari, IsMobileChrome, IsMobileWebKit, IsSafari, IsMobileSafariOnIos26}; diff --git a/src/libs/snapshotPickedFile/index.ts b/src/libs/snapshotPickedFile/index.ts index 713118ecb696..e876575ad419 100644 --- a/src/libs/snapshotPickedFile/index.ts +++ b/src/libs/snapshotPickedFile/index.ts @@ -1,10 +1,4 @@ -import {isMobile} from '@libs/Browser'; - -// iPadOS Safari in "Request Desktop Website" mode (the default) reports a Macintosh user agent that -// isMobile() can't recognize; real Macs report zero touch points. -function isIPadInDesktopMode(): boolean { - return /Macintosh/i.test(navigator.userAgent) && navigator.maxTouchPoints > 1; -} +import {isIPadInDesktopMode, isMobile} from '@libs/Browser'; /** * Copies a picked file's bytes into a memory-backed File. A picked File only references its OS diff --git a/src/pages/inbox/report/ReportActionCompose/ComposerProvider.tsx b/src/pages/inbox/report/ReportActionCompose/ComposerProvider.tsx index 567298adb0f3..10c4b8b514d5 100644 --- a/src/pages/inbox/report/ReportActionCompose/ComposerProvider.tsx +++ b/src/pages/inbox/report/ReportActionCompose/ComposerProvider.tsx @@ -2,6 +2,7 @@ import useOnyx from '@hooks/useOnyx'; import useOriginalReportID from '@hooks/useOriginalReportID'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; +import {isIPadInDesktopMode, isMobile} from '@libs/Browser'; import canFocusInputOnScreenFocus from '@libs/canFocusInputOnScreenFocus'; import Log from '@libs/Log'; import {chatIncludesConcierge} from '@libs/ReportUtils'; @@ -36,6 +37,7 @@ import useDebouncedCommentMaxLengthValidation from './useDebouncedCommentMaxLeng import useEditMessage from './useEditMessage'; const shouldFocusInputOnScreenFocus = canFocusInputOnScreenFocus(); +const isMobileBrowser = isMobile() || isIPadInDesktopMode(); type ComposerProviderProps = { reportID: string; @@ -51,7 +53,7 @@ function ComposerProvider({children, reportID}: ComposerProviderProps) { const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`); const [isComposerFullSize = false] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_IS_COMPOSER_FULL_SIZE}${reportID}`); - const shouldFocusComposerOnScreenFocus = shouldFocusInputOnScreenFocus || !!draftComment; + const shouldFocusComposerOnScreenFocus = shouldFocusInputOnScreenFocus || (!!draftComment && !isMobileBrowser); const initialFocused = shouldFocusComposerOnScreenFocus && !initialModalState?.isVisible && !initialModalState?.willAlertModalBecomeVisible; const includesConcierge = chatIncludesConcierge({participants: report?.participants}); diff --git a/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx b/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx index f4be08d51341..b414120b0cb5 100644 --- a/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx +++ b/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx @@ -17,7 +17,7 @@ import useStyleUtils from '@hooks/useStyleUtils'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; -import {isMobileSafari} from '@libs/Browser'; +import {isIPadInDesktopMode, isMobile, isMobileSafari} from '@libs/Browser'; import canFocusInputOnScreenFocus from '@libs/canFocusInputOnScreenFocus'; import {forceClearInput} from '@libs/ComponentUtils'; import {canSkipTriggerHotkeys, findCommonSuffixLength, insertText, insertWhiteSpaceAtIndex} from '@libs/ComposerUtils'; @@ -207,6 +207,8 @@ const willBlurTextInputOnTapOutside = willBlurTextInputOnTapOutsideFunc(); // prevent auto focus for mobile device const shouldFocusInputOnScreenFocus = canFocusInputOnScreenFocus(); +const isMobileBrowser = isMobile() || isIPadInDesktopMode(); + /** * This component holds the value and selection state. * If a component really needs access to these state values it should be put here. @@ -387,7 +389,7 @@ function ComposerWithSuggestions({ }, [text]); const maxComposerLines = shouldUseNarrowLayout ? CONST.COMPOSER.MAX_LINES_SMALL_SCREEN : CONST.COMPOSER.MAX_LINES; - const shouldAutoFocus = (shouldFocusInputOnScreenFocus || !!text) && areAllModalsHidden() && isFocused; + const shouldAutoFocus = (shouldFocusInputOnScreenFocus || (!!text && !isMobileBrowser)) && areAllModalsHidden() && isFocused; const delayedAutoFocusRouteKeyRef = useRef(null); const valueRef = useRef(initialText); diff --git a/tests/unit/ValidateAttachmentFileSnapshotTest.ts b/tests/unit/ValidateAttachmentFileSnapshotTest.ts index 2fc7b9d0d26b..fe9dab285d89 100644 --- a/tests/unit/ValidateAttachmentFileSnapshotTest.ts +++ b/tests/unit/ValidateAttachmentFileSnapshotTest.ts @@ -11,8 +11,9 @@ import * as FileUtils from '../../src/libs/fileDownload/FileUtils'; jest.mock('@src/libs/snapshotPickedFile', () => jest.requireActual<{default: (file: File, name: string) => Promise}>('@src/libs/snapshotPickedFile/index.ts')); // The web snapshot only copies bytes on desktop browsers; make the browser type controllable per test. +// Force the web implementation here too, so the user-agent based checks under test are the real ones. jest.mock('@src/libs/Browser', () => ({ - ...jest.requireActual>('@src/libs/Browser'), + ...jest.requireActual>('@src/libs/Browser/index.web.ts'), isMobile: jest.fn(() => false), }));