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

clean up mobile safari code #40295

Merged
merged 3 commits into from
May 13, 2024
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
8 changes: 1 addition & 7 deletions src/libs/updateMultilineInputRange/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as Browser from '@libs/Browser';
import type UpdateMultilineInputRange from './types';

/**
Expand All @@ -17,12 +16,7 @@ const updateMultilineInputRange: UpdateMultilineInputRange = (input, shouldAutoF

if ('value' in input && input.value && input.setSelectionRange) {
const length = input.value.length;

// For mobile Safari, updating the selection prop on an unfocused input will cause it to automatically gain focus
// and subsequent programmatic focus shifts (e.g., modal focus trap) to show the blue frame (:focus-visible style),
// so we need to ensure that it is only updated after focus.
const shouldSetSelection = !(Browser.isMobileSafari() && !shouldAutoFocus);
if (shouldSetSelection) {
if (shouldAutoFocus) {
input.setSelectionRange(length, length);
}
// eslint-disable-next-line no-param-reassign
Expand Down
39 changes: 6 additions & 33 deletions src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as Browser from '@libs/Browser';
import * as ComposerUtils from '@libs/ComposerUtils';
import * as EmojiUtils from '@libs/EmojiUtils';
import focusComposerWithDelay from '@libs/focusComposerWithDelay';
Expand Down Expand Up @@ -68,7 +67,6 @@ type ReportActionItemMessageEditProps = {
const emojiButtonID = 'emojiButton';
const messageEditInput = 'messageEditInput';

const isMobileSafari = Browser.isMobileSafari();
const shouldUseForcedSelectionRange = shouldUseEmojiPickerSelection();

function ReportActionItemMessageEdit(
Expand All @@ -84,22 +82,14 @@ function ReportActionItemMessageEdit(
const {isSmallScreenWidth} = useWindowDimensions();
const prevDraftMessage = usePrevious(draftMessage);

const getInitialSelection = () => {
if (isMobileSafari) {
return {start: 0, end: 0};
}

const length = draftMessage.length;
return {start: length, end: length};
};
const emojisPresentBefore = useRef<Emoji[]>([]);
const [draft, setDraft] = useState(() => {
if (draftMessage) {
emojisPresentBefore.current = EmojiUtils.extractEmojis(draftMessage);
}
return draftMessage;
});
const [selection, setSelection] = useState<Selection>(getInitialSelection);
const [selection, setSelection] = useState<Selection>({start: draft.length, end: draft.length});
const [isFocused, setIsFocused] = useState<boolean>(false);
const {hasExceededMaxCommentLength, validateCommentMaxLength} = useHandleExceedMaxCommentLength();
const [modal, setModal] = useState<OnyxTypes.Modal>({
Expand Down Expand Up @@ -170,26 +160,8 @@ function ReportActionItemMessageEdit(
[action.reportActionID],
);

useEffect(() => {
// For mobile Safari, updating the selection prop on an unfocused input will cause it to automatically gain focus
// and subsequent programmatic focus shifts (e.g., modal focus trap) to show the blue frame (:focus-visible style),
// so we need to ensure that it is only updated after focus.
if (isMobileSafari) {
setDraft((prevDraft) => {
setSelection({
start: prevDraft.length,
end: prevDraft.length,
});
return prevDraft;
});

// Scroll content of textInputRef to bottom
if (textInputRef.current) {
textInputRef.current.scrollTop = textInputRef.current.scrollHeight;
}
}

return () => {
useEffect(
() => () => {
InputFocus.callback(() => setIsFocused(false));
InputFocus.inputFocusChange(false);

Expand All @@ -208,9 +180,10 @@ function ReportActionItemMessageEdit(
// Show the main composer when the focused message is deleted from another client
// to prevent the main composer stays hidden until we swtich to another chat.
setShouldShowComposeInputKeyboardAware(true);
};
},
// eslint-disable-next-line react-hooks/exhaustive-deps -- this cleanup needs to be called only on unmount
}, [action.reportActionID]);
[action.reportActionID],
);

// show the composer after editing is complete for devices that hide the composer during editing.
useEffect(() => () => ComposerActions.setShouldShowComposeInput(true), []);
Expand Down
Loading