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

phase1: fix emoji picker refocus issue #35572

Merged
merged 23 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,15 @@ const CONST = {
RIGHT: 'right',
},
POPOVER_MENU_PADDING: 8,
BUSINESS_TYPE: {
ntdiary marked this conversation as resolved.
Show resolved Hide resolved
DEFAULT: 'default',
ATTACHMENT: 'attachment',
},
RESTORE_FOCUS_TYPE: {
DEFAULT: 'default',
DELETE: 'delete',
PRESERVE: 'preserve',
},
},
TIMING: {
CALCULATE_MOST_RECENT_LAST_MODIFIED_ACTION: 'calc_most_recent_last_modified_action',
Expand Down
1 change: 1 addition & 0 deletions src/components/EmojiPicker/EmojiPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ const EmojiPicker = forwardRef((props, ref) => {
outerStyle={StyleUtils.getOuterModalStyle(windowHeight, props.viewportOffsetTop)}
innerContainerStyle={styles.popoverInnerContainer}
avoidKeyboard
shouldEnableNewFocusManagement
Julesssss marked this conversation as resolved.
Show resolved Hide resolved
>
<EmojiPickerMenu
onEmojiSelected={selectEmoji}
Expand Down
35 changes: 23 additions & 12 deletions src/components/Modal/BaseModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import useNativeDriver from '@libs/useNativeDriver';
import variables from '@styles/variables';
import * as Modal from '@userActions/Modal';
import CONST from '@src/CONST';
import ModalContent from './ModalContent';
import type BaseModalProps from './types';

function BaseModal(
Expand All @@ -41,6 +42,8 @@ function BaseModal(
avoidKeyboard = false,
children,
shouldUseCustomBackdrop = false,
shouldEnableNewFocusManagement = false,
restoreFocusType,
}: BaseModalProps,
ref: React.ForwardedRef<View>,
) {
Expand All @@ -55,6 +58,14 @@ function BaseModal(
const isVisibleRef = useRef(isVisible);
const wasVisible = usePrevious(isVisible);

const modalId = useMemo(() => ComposerFocusManager.getId(), []);
const saveFocusState = () => {
if (shouldEnableNewFocusManagement) {
ComposerFocusManager.saveFocusState(modalId);
}
ComposerFocusManager.resetReadyToFocus(modalId);
};

/**
* Hides modal
* @param callHideCallback - Should we call the onModalHide callback
Expand All @@ -69,11 +80,9 @@ function BaseModal(
onModalHide();
}
Modal.onModalDidClose();
if (!fullscreen) {
ComposerFocusManager.setReadyToFocus();
}
ComposerFocusManager.tryRestoreFocusAfterClosedCompletely(modalId, restoreFocusType);
},
[shouldSetModalVisibility, onModalHide, fullscreen],
[shouldSetModalVisibility, onModalHide, restoreFocusType, modalId],
);

useEffect(() => {
Expand Down Expand Up @@ -121,7 +130,7 @@ function BaseModal(
};

const handleDismissModal = () => {
ComposerFocusManager.setReadyToFocus();
ComposerFocusManager.setReadyToFocus(modalId);
};

const {
Expand Down Expand Up @@ -183,7 +192,7 @@ function BaseModal(
onModalShow={handleShowModal}
propagateSwipe={propagateSwipe}
onModalHide={hideModal}
onModalWillShow={() => ComposerFocusManager.resetReadyToFocus()}
onModalWillShow={saveFocusState}
onDismiss={handleDismissModal}
onSwipeComplete={() => onClose?.()}
swipeDirection={swipeDirection}
Expand All @@ -207,12 +216,14 @@ function BaseModal(
avoidKeyboard={avoidKeyboard}
customBackdrop={shouldUseCustomBackdrop ? <Overlay onPress={handleBackdropPress} /> : undefined}
>
<View
style={[styles.defaultModalContainer, modalContainerStyle, modalPaddingStyles, !isVisible && styles.pointerEventsNone]}
ref={ref}
>
<ColorSchemeWrapper>{children}</ColorSchemeWrapper>
</View>
<ModalContent onDismiss={handleDismissModal}>
<View
style={[styles.defaultModalContainer, modalContainerStyle, modalPaddingStyles, !isVisible && styles.pointerEventsNone]}
ref={ref}
>
<ColorSchemeWrapper>{children}</ColorSchemeWrapper>
</View>
</ModalContent>
</ReactNativeModal>
);
}
Expand Down
19 changes: 19 additions & 0 deletions src/components/Modal/ModalContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {ReactNode} from 'react';
import React from 'react';

type ModalContentProps = {
/** Modal contents */
children: ReactNode;

/** called after modal content is dismissed */
onDismiss: () => void;
};

function ModalContent({children, onDismiss = () => {}}: ModalContentProps) {
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => () => onDismiss?.(), []);
ntdiary marked this conversation as resolved.
Show resolved Hide resolved
return children;
}
ModalContent.displayName = 'ModalContent';

export default ModalContent;
10 changes: 0 additions & 10 deletions src/components/Modal/index.android.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import React from 'react';
import {AppState} from 'react-native';
import ComposerFocusManager from '@libs/ComposerFocusManager';
import BaseModal from './BaseModal';
import type BaseModalProps from './types';

AppState.addEventListener('focus', () => {
ComposerFocusManager.setReadyToFocus();
});

AppState.addEventListener('blur', () => {
ComposerFocusManager.resetReadyToFocus();
});

// Only want to use useNativeDriver on Android. It has strange flashes issue on IOS
// https://github.com/react-native-modal/react-native-modal#the-modal-flashes-in-a-weird-way-when-animating
function Modal({useNativeDriver = true, ...rest}: BaseModalProps) {
Expand Down
9 changes: 9 additions & 0 deletions src/components/Modal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ type BaseModalProps = Partial<ModalProps> & {

/** Should we use a custom backdrop for the modal? (This prevents focus issues on desktop) */
shouldUseCustomBackdrop?: boolean;

/**
* Whether the modal should enable the new focus manager.
* We are attempting to migrate to a new refocus manager, adding this property for gradual migration.
* */
shouldEnableNewFocusManagement?: boolean;

/** How to re-focus after the modal is dismissed */
restoreFocusType?: ValueOf<typeof CONST.MODAL.RESTORE_FOCUS_TYPE>;
};

export default BaseModalProps;
Expand Down
Loading
Loading