Skip to content

Commit

Permalink
fix: keep the Android keyboard visible when pasting
Browse files Browse the repository at this point in the history
in Composer component
env: Android Chrome

Signed-off-by: dominictb <[email protected]>
  • Loading branch information
dominictb committed May 27, 2024
1 parent 8375abe commit ce9d389
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/Composer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function Composer(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isComposerFullSize]);

useHtmlPaste(textInput, handlePaste, true);
useHtmlPaste(textInput, handlePaste, true, false);

useEffect(() => {
if (typeof ref === 'function') {
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useHtmlPaste/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const insertAtCaret = (target: HTMLElement, text: string) => {
// Move caret to the end of the newly inserted text node.
range.setStart(node, node.length);
range.setEnd(node, node.length);
selection.removeAllRanges();
selection.addRange(range);
selection.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset);

// Dispatch paste event to simulate real browser behavior
target.dispatchEvent(new Event('paste', {bubbles: true}));
Expand All @@ -30,7 +29,7 @@ const insertAtCaret = (target: HTMLElement, text: string) => {
}
};

const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeListenerOnScreenBlur = false) => {
const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeListenerOnScreenBlur = false, shouldRefocusAfterPaste = true) => {
const navigation = useNavigation();

/**
Expand All @@ -47,7 +46,11 @@ const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeLi
}

// Pointer will go out of sight when a large paragraph is pasted on the web. Refocusing the input keeps the cursor in view.
textInputRef.current?.blur();
// If shouldRefocusAfterPaste = false, we want to call `focus()` only as it won't change the focus state of the input since it is already focused
if (shouldRefocusAfterPaste) {
textInputRef.current?.blur();
}

textInputRef.current?.focus();
// eslint-disable-next-line no-empty
} catch (e) {}
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useHtmlPaste/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type UseHtmlPaste = (
textInputRef: MutableRefObject<(HTMLTextAreaElement & TextInput) | TextInput | null>,
preHtmlPasteCallback?: (event: ClipboardEvent) => boolean,
removeListenerOnScreenBlur?: boolean,
shouldRefocusAfterPaste?: boolean,
) => void;

export default UseHtmlPaste;

0 comments on commit ce9d389

Please sign in to comment.