Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type {EdgeInsets} from 'react-native-safe-area-context';

function getLeftOffset(
x: number,
insets: EdgeInsets,
bigScreenLeftOffset: number,
shouldUseNarrowLayout: boolean,
menuWidth: number,
windowWidth: number,
isInLandscapeMode: boolean,
): number {
if (isInLandscapeMode) {
// On Android devices, sometimes x takes into consideration the insets.left value, sometimes not
// so in case it does we want to subtract it to get the correct left offset.
if (x - insets.left >= 0) {
return x - insets.left;
}

return x;
}

if (shouldUseNarrowLayout) {
return x;
}

return bigScreenLeftOffset;
}

export default getLeftOffset;
13 changes: 3 additions & 10 deletions src/components/AutoCompleteSuggestions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import useWindowDimensionsForAutoCompleteSuggestions from '@hooks/useWindowDimen
import {hasHoverSupport} from '@libs/DeviceCapabilities';
import CONST from '@src/CONST';
import AutoCompleteSuggestionsPortal from './AutoCompleteSuggestionsPortal';
import getLeftOffset from './getSuggestionsLeftOffset';
import type {AutoCompleteSuggestionsProps, MeasureParentContainerAndCursor} from './types';

const measureHeightOfSuggestionRows = (numRows: number, canBeBig: boolean, isInLandscapeMode: boolean): number => {
Expand Down Expand Up @@ -50,13 +51,6 @@ const initialContainerState = {
cursorCoordinates: {x: 0, y: 0},
};

function getLeftOffset(x: number, leftInset: number, bigScreenLeftOffset: number, shouldUseNarrowLayout: boolean, isInLandscapeMode: boolean): number {
if (shouldUseNarrowLayout) {
return isInLandscapeMode ? x - leftInset : x;
}
return bigScreenLeftOffset;
}

/**
* On the mobile-web platform, when long-pressing on auto-complete suggestions,
* we need to prevent focus shifting to avoid blurring the main input (which makes the suggestions picker close and fires the onSelect callback).
Expand All @@ -78,7 +72,6 @@ function AutoCompleteSuggestions<TSuggestion>({measureParentContainerAndReportCu
const insets = useSafeAreaInsets();
const {keyboardHeight, isKeyboardAnimatingRef} = useKeyboardState();
const {paddingBottom: bottomInset, paddingTop: topInset} = StyleUtils.getPlatformSafeAreaPadding(insets ?? undefined);
const {left: leftInset} = insets;

useEffect(() => {
const container = containerRef.current;
Expand Down Expand Up @@ -136,7 +129,7 @@ function AutoCompleteSuggestions<TSuggestion>({measureParentContainerAndReportCu
topInset,
});

const newLeftOffset = getLeftOffset(x, leftInset, bigScreenLeftOffset, shouldUseNarrowLayout, isInLandscapeMode);
const newLeftOffset = getLeftOffset(x, insets, bigScreenLeftOffset, shouldUseNarrowLayout, width, windowWidth, isInLandscapeMode);
// If the suggested word is longer than 150 (approximately half the width of the suggestion popup), then adjust a new position of popup
const isAdjustmentNeeded = Math.abs(prevLeftValue.current - bigScreenLeftOffset) > 150;
if (isInitialRender.current || isAdjustmentNeeded || prevIsInLandscapeModeValue.current !== isInLandscapeMode) {
Expand Down Expand Up @@ -179,7 +172,7 @@ function AutoCompleteSuggestions<TSuggestion>({measureParentContainerAndReportCu
topInset,
isKeyboardAnimatingRef,
isInLandscapeMode,
leftInset,
insets,
]);

// Prevent rendering if container dimensions are not set or if we have no suggestions
Expand Down
Loading