Skip to content

Commit ce9d389

Browse files
committed
fix: keep the Android keyboard visible when pasting
in Composer component env: Android Chrome Signed-off-by: dominictb <tb-dominic@outlook.com>
1 parent 8375abe commit ce9d389

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/components/Composer/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function Composer(
250250
// eslint-disable-next-line react-hooks/exhaustive-deps
251251
}, [isComposerFullSize]);
252252

253-
useHtmlPaste(textInput, handlePaste, true);
253+
useHtmlPaste(textInput, handlePaste, true, false);
254254

255255
useEffect(() => {
256256
if (typeof ref === 'function') {

src/hooks/useHtmlPaste/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ const insertAtCaret = (target: HTMLElement, text: string) => {
1818
// Move caret to the end of the newly inserted text node.
1919
range.setStart(node, node.length);
2020
range.setEnd(node, node.length);
21-
selection.removeAllRanges();
22-
selection.addRange(range);
21+
selection.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset);
2322

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

33-
const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeListenerOnScreenBlur = false) => {
32+
const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeListenerOnScreenBlur = false, shouldRefocusAfterPaste = true) => {
3433
const navigation = useNavigation();
3534

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

4948
// Pointer will go out of sight when a large paragraph is pasted on the web. Refocusing the input keeps the cursor in view.
50-
textInputRef.current?.blur();
49+
// 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
50+
if (shouldRefocusAfterPaste) {
51+
textInputRef.current?.blur();
52+
}
53+
5154
textInputRef.current?.focus();
5255
// eslint-disable-next-line no-empty
5356
} catch (e) {}

src/hooks/useHtmlPaste/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type UseHtmlPaste = (
55
textInputRef: MutableRefObject<(HTMLTextAreaElement & TextInput) | TextInput | null>,
66
preHtmlPasteCallback?: (event: ClipboardEvent) => boolean,
77
removeListenerOnScreenBlur?: boolean,
8+
shouldRefocusAfterPaste?: boolean,
89
) => void;
910

1011
export default UseHtmlPaste;

0 commit comments

Comments
 (0)