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.

popovers: Safely return focus to inbox/recent triggers.

Alya Abbottfa25619135b8
diff --git a/web/src/stream_popover.ts b/web/src/stream_popover.ts
index 7eb965831d7..584377c7053 100644
--- a/web/src/stream_popover.ts
+++ b/web/src/stream_popover.ts
@@ -239,8 +239,16 @@ function build_stream_popover(opts: {elt: HTMLElement; stream_id: number}): void
},
},
{
- get_focus_return_element: (reference) =>
- util.the($(reference).closest(".selectable_sidebar_block")),
+ get_focus_return_element(reference) {
+ // When the popover is triggered from the left sidebar,
+ // return focus to the whole row. From inbox channel
+ // headers there is no `.selectable_sidebar_block`
+ // ancestor, so fall back to the nearest focusable
+ // ancestor (or the reference itself) rather than
+ // crashing in `util.the`.
+ const $block = $(reference).closest(".selectable_sidebar_block");
+ return $block[0] ?? $(reference).closest("[tabindex='0']")[0] ?? reference;
+ },
},
);
}
diff --git a/web/src/topic_popover.ts b/web/src/topic_popover.ts
index 5f30a3ec347..670d0dfc959 100644
--- a/web/src/topic_popover.ts
+++ b/web/src/topic_popover.ts
@@ -272,8 +272,17 @@ export function initialize(): void {
},
{
also_trigger_on_enter: true,
- get_focus_return_element: (reference) =>
- util.the($(reference).closest(".topic-box, .selectable_sidebar_block")),
+ get_focus_return_element(reference) {
+ // Left sidebar triggers sit inside a `.topic-box` or
+ // `.selectable_sidebar_block`; return focus to that
+ // enclosing row so the user re-lands on a natural
+ // target. From inbox and recent-view triggers there
+ // is no such ancestor, so fall back to the nearest
+ // focusable ancestor (or the reference itself)
+ // rather than crashing in `util.the`.
+ const $block = $(reference).closest(".topic-box, .selectable_sidebar_block");
+ return $block[0] ?? $(reference).closest("[tabindex='0']")[0] ?? reference;
+ },
},
);
}