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.

recent_view, inbox: Fix scroll jumping to bottom when view is covered.

Aman Agrawal6909b2d7068a
diff --git a/web/src/inbox_ui.ts b/web/src/inbox_ui.ts
index f56e4ece16c..b92f1d049b1 100644
--- a/web/src/inbox_ui.ts
+++ b/web/src/inbox_ui.ts
@@ -2358,9 +2358,13 @@ function move_focus_to_visible_area(): void {
const compose_top = window.innerHeight - $("#compose").outerHeight(true)!;
const inbox_center_x = (inbox_filters_props.left + inbox_filters_props.right) / 2;
const inbox_center_y = (compose_top + inbox_filters_props.bottom) / 2;
- const element_in_row = document.elementFromPoint(inbox_center_x, inbox_center_y);
- if (!element_in_row) {
- // The table is too short for there to be an topic row element
+ const element_in_row = views_util.find_element_at_point(
+ inbox_center_x,
+ inbox_center_y,
+ ".inbox-row, .inbox-header",
+ );
+ if (element_in_row === undefined) {
+ // The table is too short for there to be a row element
// at the center of the table region; in that case, we just
// select the last element.
row_focus = $all_rows.length - 1;
diff --git a/web/src/recent_view_ui.ts b/web/src/recent_view_ui.ts
index 88ba20576e4..89d548da9a7 100644
--- a/web/src/recent_view_ui.ts
+++ b/web/src/recent_view_ui.ts
@@ -1486,12 +1486,13 @@ function recenter_focus_if_off_screen(): void {
const topic_center_x = (thead_props.left + thead_props.right) / 2;
const topic_center_y = (thead_props.bottom + compose_top) / 2;
- const topic_element = document.elementFromPoint(topic_center_x, topic_center_y);
- if (
- topic_element === null ||
- $(topic_element).parents("#recent-view-content-tbody").length === 0
- ) {
- // The table is too short for there to be an topic row element
+ const topic_element = views_util.find_element_at_point(
+ topic_center_x,
+ topic_center_y,
+ "#recent-view-content-tbody",
+ );
+ if (topic_element === undefined) {
+ // The table is too short for there to be a topic row element
// at the center of the table region; in that case, we just
// select the last element.
row_focus = $topic_rows.length - 1;
diff --git a/web/src/views_util.ts b/web/src/views_util.ts
index d43ec1ca780..130c0a402e9 100644
--- a/web/src/views_util.ts
+++ b/web/src/views_util.ts
@@ -190,6 +190,24 @@ export function is_in_focus(): boolean {
);
}
+export function find_element_at_point(
+ x: number,
+ y: number,
+ container_selector: string,
+): Element | undefined {
+ // When the view is obscured by a modal, overlay, popover, etc.
+ // `elementFromPoint` would return the covering element. Use
+ // `elementsFromPoint` to search through the full element stack
+ // and find the matching element beneath it.
+ const elements = document.elementsFromPoint(x, y);
+ for (const element of elements) {
+ if (element.closest(container_selector) !== null) {
+ return element;
+ }
+ }
+ return undefined;
+}
+
export function is_scroll_position_for_render(): boolean {
const scroll_position = window.scrollY;
const window_height = window.innerHeight;