Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

popover_menus: Fix second item being focused on first key down.

Aman Agrawal3458db074f0c
diff --git a/web/src/popover_menus.ts b/web/src/popover_menus.ts
index 3a03e53023b..d85992b3f7e 100644
--- a/web/src/popover_menus.ts
+++ b/web/src/popover_menus.ts
@@ -86,14 +86,31 @@ export function popover_items_handle_keyboard(key: string, $items?: JQuery): voi
return;
}
+ // If the focused item doesn't have a visible focus ring (e.g., it was
+ // focused programmatically when the popover opened via mouse click rather
+ // than keyboard navigation), treat the navigation position as unset so
+ // that the first arrow key press shows the focus ring on item 1 rather
+ // than skipping to item 2. We blur first because calling .focus() on an
+ // already-focused element is a no-op and won't trigger :focus-visible.
+ const focused_item_has_focus_ring =
+ index !== -1 && document.activeElement?.matches(":focus-visible") === true;
+ if (
+ !focused_item_has_focus_ring &&
+ index !== -1 &&
+ document.activeElement instanceof HTMLElement
+ ) {
+ document.activeElement.blur();
+ }
+ const nav_index = focused_item_has_focus_ring ? index : -1;
+
if (key === "down_arrow" || key === "vim_down") {
[...$items]
- .slice(index === -1 ? 0 : index + 1)
+ .slice(nav_index === -1 ? 0 : nav_index + 1)
.find((item) => item.getClientRects().length)
?.focus();
} else if (key === "up_arrow" || key === "vim_up") {
[...$items]
- .slice(0, index === -1 ? $items.length : index)
+ .slice(0, nav_index === -1 ? $items.length : nav_index)
.findLast((item) => item.getClientRects().length)
?.focus();
}