Skip to content

Commit af4fb74

Browse files
authored
fix: do not try close message input's autocomplete dropdown on textarea blur (#2015)
The blur event's relatedTarget value in Safari is incorrect when clicking on autocomplete item. We cannot therefore decide, whether to close or not the autocomplete dropdown. This fix introduces change that will lead to ignoring the textarea blur event in Safari. That means that user will be able to close the autocomplete dropdown only by selecting the item.
1 parent 24d2a4d commit af4fb74

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/components/AutoCompleteTextarea/Textarea.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515

1616
import { CommandItem } from '../CommandItem';
1717
import { UserItem } from '../UserItem';
18+
import { isSafari } from '../../utils/browsers';
1819

1920
export class ReactTextareaAutocomplete extends React.Component {
2021
static defaultProps = {
@@ -565,7 +566,16 @@ export class ReactTextareaAutocomplete extends React.Component {
565566
// that was actually clicked. If we clicked inside the auto-select dropdown, then
566567
// that's not a blur, from the auto-select point of view, so then do nothing.
567568
const el = e.relatedTarget;
568-
if (this.dropdownRef && el instanceof Node && this.dropdownRef.contains(el)) {
569+
// If this is a blur event in Safari, then relatedTarget is never a dropdown item, but a common parent
570+
// of textarea and dropdown container. That means that dropdownRef will not contain its parent and the
571+
// autocomplete will be closed before onclick handler can be invoked selecting an item.
572+
// It seems that Safari has different implementation determining the relatedTarget node than Chrome and Firefox.
573+
// Therefore, if focused away in Safari, the dropdown will be kept rendered until pressing Esc or selecting and item from it.
574+
const focusedAwayInSafari = isSafari() && e.type === 'blur';
575+
if (
576+
(this.dropdownRef && el instanceof Node && this.dropdownRef.contains(el)) ||
577+
focusedAwayInSafari
578+
) {
569579
return;
570580
}
571581

src/utils/browsers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const isSafari = () => {
2+
if (typeof navigator === 'undefined') return false;
3+
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent || '');
4+
};

0 commit comments

Comments
 (0)