Fix onClick and floating mounting for mention menu (#206)

This commit is contained in:
Hakan Shehu
2025-09-04 08:27:42 +02:00
committed by GitHub
parent ac2464f8f7
commit 692a0dbe87

View File

@@ -5,6 +5,7 @@ import {
shift, shift,
autoUpdate, autoUpdate,
FloatingPortal, FloatingPortal,
VirtualElement,
} from '@floating-ui/react'; } from '@floating-ui/react';
import type { Range } from '@tiptap/core'; import type { Range } from '@tiptap/core';
import { Editor, Node } from '@tiptap/core'; import { Editor, Node } from '@tiptap/core';
@@ -60,19 +61,26 @@ const CommandList = ({
}) => { }) => {
const [selectedIndex, setSelectedIndex] = useState(0); const [selectedIndex, setSelectedIndex] = useState(0);
const { refs, floatingStyles } = useFloating({ const { refs, floatingStyles, update } = useFloating({
placement: 'bottom-start', placement: 'bottom-start',
middleware: [offset(6), flip(), shift()], middleware: [offset(6), flip(), shift()],
whileElementsMounted: autoUpdate, whileElementsMounted: autoUpdate,
strategy: 'fixed', strategy: 'fixed',
elements: {
reference: {
getBoundingClientRect: () => props.clientRect?.() || new DOMRect(),
contextElement: document.body,
} as unknown as Element,
},
}); });
useLayoutEffect(() => {
const rect = props.clientRect?.();
if (!rect) return;
const virtualEl = {
getBoundingClientRect: () => rect,
contextElement: props.editor.view.dom as Element,
};
refs.setPositionReference(virtualEl as VirtualElement);
update();
}, [props.clientRect, props.editor.view.dom, refs, update]);
const selectItem = useCallback( const selectItem = useCallback(
(index: number) => { (index: number) => {
const item = items[index]; const item = items[index];
@@ -145,6 +153,12 @@ const CommandList = ({
}`} }`}
key={item.id} key={item.id}
onClick={() => selectItem(index)} onClick={() => selectItem(index)}
onPointerDownCapture={(e) => {
// Added this event handler because the onClick handler was not working
e.preventDefault();
e.stopPropagation();
selectItem(index);
}}
> >
<div className="flex size-10 min-w-10 items-center justify-center rounded-md border bg-background"> <div className="flex size-10 min-w-10 items-center justify-center rounded-md border bg-background">
<Avatar <Avatar