Skip to content

Commit

Permalink
Merge pull request #27771 from s-alves10/fix/issue-27036
Browse files Browse the repository at this point in the history
fix: prevent transparent suggestion list by using portal
  • Loading branch information
techievivek authored Sep 26, 2023
2 parents 85a9c5f + d562c23 commit 3486648
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function BaseAutoCompleteSuggestions(props) {
});

const innerHeight = CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTION_ROW_HEIGHT * props.suggestions.length;
const animatedStyles = useAnimatedStyle(() => StyleUtils.getAutoCompleteSuggestionContainerStyle(rowHeight.value, props.shouldIncludeReportRecipientLocalTimeHeight));
const animatedStyles = useAnimatedStyle(() => StyleUtils.getAutoCompleteSuggestionContainerStyle(rowHeight.value));

useEffect(() => {
rowHeight.value = withTiming(measureHeightOfSuggestionRows(props.suggestions.length, props.isSuggestionPickerLarge), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ const propTypes = {
* When this value is false, the suggester will have a height of 2.5 items. When this value is true, the height can be up to 5 items. */
isSuggestionPickerLarge: PropTypes.bool.isRequired,

/** Show that we should include ReportRecipientLocalTime view height */
shouldIncludeReportRecipientLocalTimeHeight: PropTypes.bool.isRequired,

/** create accessibility label for each item */
accessibilityLabelExtractor: PropTypes.func.isRequired,

Expand Down
17 changes: 8 additions & 9 deletions src/components/AutoCompleteSuggestions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import useWindowDimensions from '../../hooks/useWindowDimensions';
* On the native platform, tapping on auto-complete suggestions will not blur the main input.
*/

function AutoCompleteSuggestions({parentContainerRef, ...props}) {
function AutoCompleteSuggestions({measureParentContainer, ...props}) {
const containerRef = React.useRef(null);
const {windowHeight, windowWidth} = useWindowDimensions();
const [{width, left, bottom}, setContainerState] = React.useState({
Expand All @@ -37,11 +37,11 @@ function AutoCompleteSuggestions({parentContainerRef, ...props}) {
}, []);

React.useEffect(() => {
if (!parentContainerRef || !parentContainerRef.current) {
if (!measureParentContainer) {
return;
}
parentContainerRef.current.measureInWindow((x, y, w) => setContainerState({left: x, bottom: windowHeight - y, width: w}));
}, [parentContainerRef, windowHeight, windowWidth]);
measureParentContainer((x, y, w) => setContainerState({left: x, bottom: windowHeight - y, width: w}));
}, [measureParentContainer, windowHeight, windowWidth]);

const componentToRender = (
<BaseAutoCompleteSuggestions
Expand All @@ -51,11 +51,10 @@ function AutoCompleteSuggestions({parentContainerRef, ...props}) {
/>
);

if (!width) {
return componentToRender;
}

return ReactDOM.createPortal(<View style={StyleUtils.getBaseAutoCompleteSuggestionContainerStyle({left, width, bottom})}>{componentToRender}</View>, document.querySelector('body'));
return (
Boolean(width) &&
ReactDOM.createPortal(<View style={StyleUtils.getBaseAutoCompleteSuggestionContainerStyle({left, width, bottom})}>{componentToRender}</View>, document.querySelector('body'))
);
}

AutoCompleteSuggestions.propTypes = propTypes;
Expand Down
11 changes: 8 additions & 3 deletions src/components/AutoCompleteSuggestions/index.native.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import React from 'react';
import {Portal} from '@gorhom/portal';
import BaseAutoCompleteSuggestions from './BaseAutoCompleteSuggestions';
import {propTypes} from './autoCompleteSuggestionsPropTypes';

function AutoCompleteSuggestions({parentContainerRef, ...props}) {
// eslint-disable-next-line react/jsx-props-no-spreading
return <BaseAutoCompleteSuggestions {...props} />;
function AutoCompleteSuggestions({measureParentContainer, ...props}) {
return (
<Portal hostName="suggestions">
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
<BaseAutoCompleteSuggestions {...props} />
</Portal>
);
}

AutoCompleteSuggestions.propTypes = propTypes;
Expand Down
4 changes: 0 additions & 4 deletions src/components/EmojiSuggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const propTypes = {
* 2.5 items. When this value is true, the height can be up to 5 items. */
isEmojiPickerLarge: PropTypes.bool.isRequired,

/** Show that we should include ReportRecipientLocalTime view height */
shouldIncludeReportRecipientLocalTimeHeight: PropTypes.bool.isRequired,

/** Stores user's preferred skin tone */
preferredSkinToneIndex: PropTypes.number.isRequired,

Expand Down Expand Up @@ -102,7 +99,6 @@ function EmojiSuggestions(props) {
highlightedSuggestionIndex={props.highlightedEmojiIndex}
onSelect={props.onSelect}
isSuggestionPickerLarge={props.isEmojiPickerLarge}
shouldIncludeReportRecipientLocalTimeHeight={props.shouldIncludeReportRecipientLocalTimeHeight}
accessibilityLabelExtractor={keyExtractor}
measureParentContainer={props.measureParentContainer}
/>
Expand Down
4 changes: 0 additions & 4 deletions src/components/MentionSuggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ const propTypes = {
* When this value is false, the suggester will have a height of 2.5 items. When this value is true, the height can be up to 5 items. */
isMentionPickerLarge: PropTypes.bool.isRequired,

/** Show that we should include ReportRecipientLocalTime view height */
shouldIncludeReportRecipientLocalTimeHeight: PropTypes.bool.isRequired,

/** Meaures the parent container's position and dimensions. */
measureParentContainer: PropTypes.func,
};
Expand Down Expand Up @@ -126,7 +123,6 @@ function MentionSuggestions(props) {
highlightedSuggestionIndex={props.highlightedMentionIndex}
onSelect={props.onSelect}
isSuggestionPickerLarge={props.isMentionPickerLarge}
shouldIncludeReportRecipientLocalTimeHeight={props.shouldIncludeReportRecipientLocalTimeHeight}
accessibilityLabelExtractor={keyExtractor}
measureParentContainer={props.measureParentContainer}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ function ComposerWithSuggestions({
setIsFullComposerAvailable,
setIsCommentEmpty,
submitForm,
shouldShowReportRecipientLocalTime,
shouldShowComposeInput,
measureParentContainer,
// Refs
Expand Down Expand Up @@ -535,7 +534,6 @@ function ComposerWithSuggestions({
isComposerFullSize={isComposerFullSize}
updateComment={updateComment}
composerHeight={composerHeight}
shouldShowReportRecipientLocalTime={shouldShowReportRecipientLocalTime}
onInsertedEmoji={onInsertedEmoji}
measureParentContainer={measureParentContainer}
// Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import _ from 'underscore';
import lodashGet from 'lodash/get';
import {withOnyx} from 'react-native-onyx';
import {useAnimatedRef} from 'react-native-reanimated';
import {PortalHost} from '@gorhom/portal';
import styles from '../../../../styles/styles';
import ONYXKEYS from '../../../../ONYXKEYS';
import * as Report from '../../../../libs/actions/Report';
Expand Down Expand Up @@ -318,6 +319,7 @@ function ReportActionCompose({
ref={containerRef}
style={[shouldShowReportRecipientLocalTime && !lodashGet(network, 'isOffline') && styles.chatItemComposeWithFirstRow, isComposerFullSize && styles.chatItemFullComposeRow]}
>
<PortalHost name="suggestions" />
<OfflineWithFeedback
pendingAction={pendingAction}
style={isComposerFullSize ? styles.chatItemFullComposeRow : {}}
Expand Down Expand Up @@ -380,7 +382,6 @@ function ReportActionCompose({
setIsFullComposerAvailable={setIsFullComposerAvailable}
setIsCommentEmpty={setIsCommentEmpty}
submitForm={submitForm}
shouldShowReportRecipientLocalTime={shouldShowReportRecipientLocalTime}
shouldShowComposeInput={shouldShowComposeInput}
onFocus={onFocus}
onBlur={onBlur}
Expand Down
2 changes: 0 additions & 2 deletions src/pages/home/report/ReportActionCompose/SuggestionEmoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ function SuggestionEmoji({
setSelection,
updateComment,
isComposerFullSize,
shouldShowReportRecipientLocalTime,
isAutoSuggestionPickerLarge,
forwardedRef,
resetKeyboardInput,
Expand Down Expand Up @@ -235,7 +234,6 @@ function SuggestionEmoji({
isComposerFullSize={isComposerFullSize}
preferredSkinToneIndex={preferredSkinTone}
isEmojiPickerLarge={isAutoSuggestionPickerLarge}
shouldIncludeReportRecipientLocalTimeHeight={shouldShowReportRecipientLocalTime}
measureParentContainer={measureParentContainer}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function SuggestionMention({
personalDetails,
updateComment,
composerHeight,
shouldShowReportRecipientLocalTime,
forwardedRef,
isAutoSuggestionPickerLarge,
measureParentContainer,
Expand Down Expand Up @@ -285,7 +284,6 @@ function SuggestionMention({
isComposerFullSize={isComposerFullSize}
isMentionPickerLarge={isAutoSuggestionPickerLarge}
composerHeight={composerHeight}
shouldIncludeReportRecipientLocalTimeHeight={shouldShowReportRecipientLocalTime}
measureParentContainer={measureParentContainer}
/>
);
Expand Down
2 changes: 0 additions & 2 deletions src/pages/home/report/ReportActionCompose/Suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function Suggestions({
setSelection,
updateComment,
composerHeight,
shouldShowReportRecipientLocalTime,
forwardedRef,
onInsertedEmoji,
resetKeyboardInput,
Expand Down Expand Up @@ -105,7 +104,6 @@ function Suggestions({
isComposerFullSize,
updateComment,
composerHeight,
shouldShowReportRecipientLocalTime,
isAutoSuggestionPickerLarge,
measureParentContainer,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ const propTypes = {
/** A method to call when the form is submitted */
submitForm: PropTypes.func.isRequired,

/** Whether the recipient local time is shown or not */
shouldShowReportRecipientLocalTime: PropTypes.bool.isRequired,

/** Whether the compose input is shown or not */
shouldShowComposeInput: PropTypes.bool.isRequired,

Expand Down
3 changes: 0 additions & 3 deletions src/pages/home/report/ReportActionCompose/suggestionProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ const baseProps = {
/** Callback to update the comment draft */
updateComment: PropTypes.func.isRequired,

/** Flag whether we need to consider the participants */
shouldShowReportRecipientLocalTime: PropTypes.bool.isRequired,

/** Meaures the parent container's position and dimensions. */
measureParentContainer: PropTypes.func.isRequired,
};
Expand Down
6 changes: 2 additions & 4 deletions src/styles/StyleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -938,19 +938,17 @@ function getBaseAutoCompleteSuggestionContainerStyle({left, bottom, width}: {lef
/**
* Gets the correct position for auto complete suggestion container
*/
function getAutoCompleteSuggestionContainerStyle(itemsHeight: number, shouldIncludeReportRecipientLocalTimeHeight: boolean): ViewStyle | CSSProperties {
function getAutoCompleteSuggestionContainerStyle(itemsHeight: number): ViewStyle | CSSProperties {
'worklet';

const optionalPadding = shouldIncludeReportRecipientLocalTimeHeight ? CONST.RECIPIENT_LOCAL_TIME_HEIGHT : 0;
const padding = CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTER_PADDING + optionalPadding;
const borderWidth = 2;
const height = itemsHeight + 2 * CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTER_INNER_PADDING + borderWidth;

// The suggester is positioned absolutely within the component that includes the input and RecipientLocalTime view (for non-expanded mode only). To position it correctly,
// we need to shift it by the suggester's height plus its padding and, if applicable, the height of the RecipientLocalTime view.
return {
overflow: 'hidden',
top: -(height + padding),
top: -(height + CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTER_PADDING),
height,
};
}
Expand Down

0 comments on commit 3486648

Please sign in to comment.