From 692a0dbe87536c56d5de4bb78d18c56a37f39b2b Mon Sep 17 00:00:00 2001 From: Hakan Shehu Date: Thu, 4 Sep 2025 08:27:42 +0200 Subject: [PATCH] Fix onClick and floating mounting for mention menu (#206) --- packages/ui/src/editor/extensions/mention.tsx | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/ui/src/editor/extensions/mention.tsx b/packages/ui/src/editor/extensions/mention.tsx index 44c6441b..15fcff15 100644 --- a/packages/ui/src/editor/extensions/mention.tsx +++ b/packages/ui/src/editor/extensions/mention.tsx @@ -5,6 +5,7 @@ import { shift, autoUpdate, FloatingPortal, + VirtualElement, } from '@floating-ui/react'; import type { Range } from '@tiptap/core'; import { Editor, Node } from '@tiptap/core'; @@ -60,19 +61,26 @@ const CommandList = ({ }) => { const [selectedIndex, setSelectedIndex] = useState(0); - const { refs, floatingStyles } = useFloating({ + const { refs, floatingStyles, update } = useFloating({ placement: 'bottom-start', middleware: [offset(6), flip(), shift()], whileElementsMounted: autoUpdate, 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( (index: number) => { const item = items[index]; @@ -145,6 +153,12 @@ const CommandList = ({ }`} key={item.id} onClick={() => selectItem(index)} + onPointerDownCapture={(e) => { + // Added this event handler because the onClick handler was not working + e.preventDefault(); + e.stopPropagation(); + selectItem(index); + }} >