Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

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

Hakan Shehu692a0dbe8753
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);
+ }}
>
<div className="flex size-10 min-w-10 items-center justify-center rounded-md border bg-background">
<Avatar