Skip to content

Commit 276aa0b

Browse files
committed
Position popover below caret, instead of below whole text area
Change anchor element for mentions popover in markdown editor to be a hidden element which is positioned at the location of the caret within the text area, instead of using the textarea itself. Fixes #6799.
1 parent e149f4c commit 276aa0b

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/sidebar/components/MarkdownEditor.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
getContainingWordOffsets,
4141
termBeforePosition,
4242
} from '../util/term-before-position';
43+
import { getCaretCoordinates } from '../util/textarea-caret-position';
4344
import MarkdownView from './MarkdownView';
4445
import MentionPopover from './MentionPopover';
4546

@@ -216,6 +217,7 @@ function TextArea({
216217
const [activeMention, setActiveMention] = useState<string>();
217218
const textareaRef = useSyncedRef(containerRef);
218219
const [highlightedSuggestion, setHighlightedSuggestion] = useState(0);
220+
const caretElementRef = useRef<HTMLDivElement>(null);
219221
const userSuggestions = useMemo(() => {
220222
if (!mentionsEnabled || activeMention === undefined) {
221223
return [];
@@ -240,9 +242,14 @@ function TextArea({
240242
return;
241243
}
242244

245+
// Re-position mention popover anchor element at the caret.
246+
const { x, y } = getCaretCoordinates(textarea);
247+
if (caretElementRef.current) {
248+
caretElementRef.current.style.transform = `translate(${x}px, ${y}px)`;
249+
}
250+
243251
const term = termBeforePosition(textarea.value, textarea.selectionStart);
244252
const isAtMention = term.startsWith('@');
245-
246253
setPopoverOpen(isAtMention);
247254
setActiveMention(isAtMention ? term.substring(1) : undefined);
248255

@@ -358,11 +365,15 @@ function TextArea({
358365
ref={textareaRef}
359366
{...accessibilityAttributes}
360367
/>
368+
<div
369+
ref={caretElementRef}
370+
className="absolute top-0 left-0 w-0 h-0 pointer-events-none"
371+
/>
361372
{mentionsEnabled && (
362373
<MentionPopover
363374
open={popoverOpen}
364375
onClose={() => setPopoverOpen(false)}
365-
anchorElementRef={textareaRef}
376+
anchorElementRef={caretElementRef}
366377
users={userSuggestions}
367378
highlightedSuggestion={highlightedSuggestion}
368379
onSelectUser={insertMention}

0 commit comments

Comments
 (0)