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

fix: auto focus and scroll to bottom on large-text in composer #28790

Merged
merged 15 commits into from
Oct 17, 2023
Merged
Changes from 8 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
Expand Up @@ -33,6 +33,7 @@ import withKeyboardState from '../../../../components/withKeyboardState';
import {propTypes, defaultProps} from './composerWithSuggestionsProps';
import focusWithDelay from '../../../../libs/focusWithDelay';
import useDebounce from '../../../../hooks/useDebounce';
import updateMultilineInputRange from '../../../../libs/UpdateMultilineInputRange';

const {RNTextInputReset} = NativeModules;

Expand All @@ -50,6 +51,7 @@ const debouncedBroadcastUserIsTyping = _.debounce((reportID) => {
}, 100);

const willBlurTextInputOnTapOutside = willBlurTextInputOnTapOutsideFunc();
const isNativeApp = !willBlurTextInputOnTapOutside;

// We want consistent auto focus behavior on input between native and mWeb so we have some auto focus management code that will
// prevent auto focus on existing chat for mobile device
Expand Down Expand Up @@ -124,6 +126,7 @@ function ComposerWithSuggestions({

const textInputRef = useRef(null);
const insertedEmojisRef = useRef([]);
const initialFocusedRef = useRef(false);

// A flag to indicate whether the onScroll callback is likely triggered by a layout change (caused by text change) or not
const isScrollLikelyLayoutTriggered = useRef(false);
Expand Down Expand Up @@ -445,6 +448,17 @@ function ComposerWithSuggestions({
textInputRef.current.blur();
}, []);

useEffect(() => {
// Initial focus ref to prevent unneccessary focus after first render.
if (initialFocusedRef.current || isNativeApp || !shouldAutoFocus) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think initialFocusedRef is needed anymore since we removed focus calls.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the isNativeApp check is really necessary here either. Also not sure if you considered under what cases shouldAutoFocus would be false and if we'd not want to scroll to the bottom? It seems to me like we always want to be scrolled to the bottom, but I could be missing some obvious case!

return;
}

// Set the `selection at end` and `scrolls input to bottom` for `Web Platforms`.
updateMultilineInputRange(textInputRef.current);
initialFocusedRef.current = true;
}, [shouldAutoFocus]);

useEffect(() => {
const unsubscribeNavigationBlur = navigation.addListener('blur', () => KeyDownListener.removeKeyDownPressListner(focusComposerOnKeyPress));
const unsubscribeNavigationFocus = navigation.addListener('focus', () => {
Expand Down
Loading