Skip to content

Build your personal knowledge base with Trilium Notes

fix(autocomplete): leave the keyboard's row alone under a still pointer

Elian Dorancc41170c8f5b
diff --git a/apps/client/src/widgets/react/FormAutocomplete.tsx b/apps/client/src/widgets/react/FormAutocomplete.tsx
index 726384b587..605f7bfcc7 100644
--- a/apps/client/src/widgets/react/FormAutocomplete.tsx
+++ b/apps/client/src/widgets/react/FormAutocomplete.tsx
@@ -202,7 +202,7 @@ export default function FormAutocomplete({ currentValue, onChange, source, openO
className={`form-autocomplete-item ${index === activeIndex ? "active" : ""}`}
role="option"
aria-selected={index === activeIndex}
- onMouseEnter={() => autocomplete.hover(index)}
+ onMouseMove={(e) => autocomplete.hover(index, e)}
onClick={() => pick(item)}
>
{renderItem ? renderItem(item) : item}
@@ -273,6 +273,9 @@ export function useAutocomplete<T>({ query, source, onPick, inputRef, disabled,
// Set when the keyboard or a new list moves the highlight, which the effect below scrolls into view.
// The pointer never sets it: scrolling a partly shown row under the pointer would move the list.
const scrollToActive = useRef(false);
+ // Where the pointer last moved over the list. Scrolling slides a row under a still pointer, and the
+ // browser can report that as a move at the same spot, which is not the pointer choosing the row.
+ const lastPointer = useRef<{ x: number; y: number }>();
// Names the entries so the field can point at the highlighted one: focus stays in the box, so
// that pointer is all a screen reader has to go on.
const itemIdPrefix = useUniqueName("autocomplete-item");
@@ -398,8 +401,11 @@ export function useAutocomplete<T>({ query, source, onPick, inputRef, disabled,
activeIndex,
setActiveIndex,
itemId,
- /** Highlights the row under the pointer, without scrolling the list. */
- hover(index: number) {
+ /** Highlights the row the pointer moves over, without scrolling the list. */
+ hover(index: number, e: { clientX: number; clientY: number }) {
+ const last = lastPointer.current;
+ if (last && last.x === e.clientX && last.y === e.clientY) return;
+ lastPointer.current = { x: e.clientX, y: e.clientY };
scrollToActive.current = false;
setActiveIndex(index);
},
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index e1c70c8b10..5cbaa97a54 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -267,7 +267,7 @@ describe("NoteAutocomplete's suggestion list", () => {
scrollIntoView.mockClear();
await act(async () => {
- rows()[1].dispatchEvent(new MouseEvent("mouseenter"));
+ rows()[1].dispatchEvent(new MouseEvent("mousemove", { bubbles: true, clientX: 10, clientY: 40 }));
});
expect(rows()[1].classList.contains("tn-menu-active")).toBe(true);
expect(scrollIntoView).not.toHaveBeenCalled();
@@ -280,6 +280,31 @@ describe("NoteAutocomplete's suggestion list", () => {
}
});
+ it("keeps the keyboard's row when the list moves under a pointer that does not", async () => {
+ const input = await mount();
+ await type(input, "b");
+ const hoverAt = async (row: HTMLElement, type: string, clientY: number) => {
+ await act(async () => {
+ row.dispatchEvent(new MouseEvent(type, { bubbles: true, clientX: 10, clientY }));
+ });
+ };
+
+ await hoverAt(rows()[1], "mousemove", 40);
+ expect(rows()[1].classList.contains("tn-menu-active")).toBe(true);
+ await press(input, "ArrowUp");
+ expect(rows()[0].classList.contains("tn-menu-active")).toBe(true);
+
+ // Scrolling slides another row under the pointer: the browser reports it entering, and can
+ // report a move at the same spot.
+ await hoverAt(rows()[1], "mouseenter", 40);
+ await hoverAt(rows()[1], "mousemove", 40);
+ expect(rows()[0].classList.contains("tn-menu-active")).toBe(true);
+
+ // A pointer that moves takes the row again.
+ await hoverAt(rows()[1], "mousemove", 44);
+ expect(rows()[1].classList.contains("tn-menu-active")).toBe(true);
+ });
+
// Issue #5669: a second Enter that arrives before the list re-renders submits the host's form
// rather than picking a row of the list the first Enter closed.
it("leaves a second Enter in the same task to the host", async () => {
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index 9c8dc50f19..90a10768b5 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -354,7 +354,7 @@ function NoteSuggestionList({ autocomplete, searchingFor }: {
className={clsx("aa-suggestion", index === autocomplete.activeIndex && "aa-cursor")}
role="option"
aria-selected={index === autocomplete.activeIndex}
- onMouseEnter={() => autocomplete.hover(index)}
+ onMouseMove={(e) => autocomplete.hover(index, e)}
onClick={() => autocomplete.pick(suggestion)}
>
<NoteSuggestion suggestion={suggestion} />
@@ -395,7 +395,7 @@ function NoteSuggestionMenu({ autocomplete, searchingFor }: {
className={clsx("dropdown-item", index === autocomplete.activeIndex && "tn-menu-active")}
role="option"
aria-selected={index === autocomplete.activeIndex}
- onMouseEnter={() => autocomplete.hover(index)}
+ onMouseMove={(e) => autocomplete.hover(index, e)}
onClick={() => autocomplete.pick(suggestion)}
>
<NoteSuggestionMenuItem suggestion={suggestion} />