Skip to content

Build your personal knowledge base with Trilium Notes

fix(menu): let Right and Left reach the menu from a filter input

Elian Doran49165de37425
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index be52707224..0233de049f 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -937,6 +937,8 @@ describe("contextMenu", () => {
await vi.waitFor(() => expect(document.activeElement).toBe(filterInput()));
// The caret's keys are the input's: nothing moves in the menu, and the submenu stays.
+ // From the start of the text, as Right at its end is the menu's.
+ filterInput()?.setSelectionRange(0, 0);
for (const name of [ "Home", "End", "ArrowLeft", "ArrowRight", "Backspace", " " ]) {
expect(key(name).defaultPrevented).toBe(false);
}
@@ -994,6 +996,43 @@ describe("contextMenu", () => {
expect(contextMenu.isShown).toBe(false);
});
+ it("gives Right and Left to the menu where the caret has nowhere to go", async () => {
+ await openFilterable();
+ await openSubmenuByKeys();
+ key("ArrowDown");
+ await vi.waitFor(() => expect(activeRow()).toBe("Code"));
+ filterInput()?.focus();
+
+ // Right in an empty input opens the active row's submenu, whose rows take the keys.
+ expect(key("ArrowRight").defaultPrevented).toBe(true);
+ await vi.waitFor(() => expect(activeRow()).toBe("Plain text"));
+ expect(document.activeElement).toBe(menuElement());
+ key("ArrowLeft");
+ await vi.waitFor(() => expect(activeRow()).toBe("Code"));
+
+ // With text, the caret moves until it reaches the end.
+ filterInput()?.focus();
+ edit("tex");
+ await vi.waitFor(() => expect(layer().rows).toEqual([ "Text|", "Plain text|Code" ]));
+ filterInput()?.setSelectionRange(1, 1);
+ expect(key("ArrowRight").defaultPrevented).toBe(false);
+ expect(key("ArrowLeft").defaultPrevented).toBe(false);
+ filterInput()?.setSelectionRange(0, 3);
+ expect(key("ArrowRight").defaultPrevented).toBe(false);
+ // At the end, Right is the menu's, and a match has no submenu to open.
+ filterInput()?.setSelectionRange(3, 3);
+ expect(key("ArrowRight").defaultPrevented).toBe(true);
+ expect(document.activeElement).toBe(filterInput());
+
+ // Left in an empty input closes the submenu, as it does from its rows.
+ edit("");
+ await vi.waitFor(() => expect(layer().rows).toEqual(unfiltered));
+ expect(key("ArrowLeft").defaultPrevented).toBe(true);
+ await vi.waitFor(() => expect(activeRow()).toBe("Insert child note"));
+ expect(filterInput()).toBeNull();
+ expect(document.activeElement).toBe(menuElement());
+ });
+
it("takes a press into the input, as a phone needs to bring up its keyboard", async () => {
await openFilterable();
const parent = [ ...menuElement()?.querySelectorAll<HTMLElement>("li.dropdown-submenu") ?? [] ]
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index 39a3934797..85e09a9b93 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -51,7 +51,7 @@ export interface MenuProps<T> extends Pick<PopupProps, "anchor" | "offset" | "co
/** The keys the menu keeps while focus is inside a custom row: those that leave it. */
const KEYS_LEAVING_CUSTOM_ROW = new Set([ "ArrowUp", "ArrowDown", "Escape", "Tab" ]);
-/** The keys the menu keeps while focus is in a submenu's filter input; the input edits with the rest. */
+/** The keys the menu always keeps while focus is in a submenu's filter input. See `isFilterKey()`. */
const KEYS_OF_FILTER = new Set([ "ArrowUp", "ArrowDown", "Enter", "Escape", "Tab" ]);
const FILTER_INPUT = "input.tn-menu-filter-input";
@@ -207,9 +207,10 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,
// filter input, only the keys that are the menu's, so the control there keeps its own.
const listener = (e: KeyboardEvent) => {
const menu = menuRef.current;
- const inFilter = e.target instanceof Element && e.target.matches(FILTER_INPUT);
- const keys = inFilter ? KEYS_OF_FILTER : KEYS_LEAVING_CUSTOM_ROW;
- if (e.target === menu || (menu?.contains(e.target as Node) && keys.has(e.key))) {
+ const takes = e.target instanceof HTMLInputElement && e.target.matches(FILTER_INPUT)
+ ? isFilterKey(e.target, e.key)
+ : KEYS_LEAVING_CUSTOM_ROW.has(e.key);
+ if (e.target === menu || (menu?.contains(e.target as Node) && takes)) {
keyHandler.current(e);
}
};
@@ -240,6 +241,8 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,
openSubmenu(level, activeHere.id, activeRow.element, true);
// A submenu already open has its rows; one opening now has them once it renders.
if (!activateFirstRow(level + 1)) pendingFirstRow.current = level + 1;
+ // Its rows take the keys, which the filter input would keep at its own level.
+ if (filterInput) menuRef.current?.focus({ preventScroll: true });
};
const closeLevel = (closing: number) => {
const parent = open[closing - 1];
@@ -270,6 +273,7 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,
// Out of the submenu.
case rtl ? "ArrowRight" : "ArrowLeft":
if (level > 0) closeLevel(level);
+ if (filterInput) menuRef.current?.focus({ preventScroll: true });
break;
case "Enter":
case " ":
@@ -558,6 +562,20 @@ function highlightMatch(title: string, text: string) {
return title;
}
+/**
+ * Whether a key pressed in a filter input is the menu's: Up, Down, Enter, Escape and Tab always,
+ * the arrow into a submenu once the caret has nowhere further to go, and the arrow out of the
+ * submenu while the input is empty. The input moves its caret with the arrows otherwise.
+ */
+function isFilterKey(input: HTMLInputElement, key: string) {
+ if (KEYS_OF_FILTER.has(key)) return true;
+ const rtl = isRightToLeft();
+ if (key === (rtl ? "ArrowLeft" : "ArrowRight")) {
+ return input.selectionStart === input.selectionEnd && input.selectionEnd === input.value.length;
+ }
+ return key === (rtl ? "ArrowRight" : "ArrowLeft") && input.value === "";
+}
+
/** The filter input of the submenu open in `menu`, if one is. */
function filterInputIn(menu: HTMLElement | null) {
return menu?.querySelector<HTMLInputElement>(FILTER_INPUT) ?? null;