Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
overlays: Scroll within tall items before moving focus.
Aman Agrawal601f4ba21623
diff --git a/web/src/messages_overlay_ui.ts b/web/src/messages_overlay_ui.tsindex 4efffb6c9f0..524ad73191e 100644--- a/web/src/messages_overlay_ui.ts+++ b/web/src/messages_overlay_ui.ts@@ -58,14 +58,26 @@ export function modals_handle_events(event_key: string, context: Context): voidinitialize_focus(event_key, context);// This detects up arrow key presses when the overlay- // is open and scrolls through.- if (event_key === "up_arrow" || event_key === "vim_up") {+ // is open and scrolls through. If the focused element+ // extends above the visible area, first scroll to reveal+ // more of it before moving focus to the previous one.+ if (+ (event_key === "up_arrow" || event_key === "vim_up") &&+ !scroll_toward_visible(row_with_focus(context), "up", context) &&+ row_before_focus(context).length > 0+ ) {scroll_to_element(row_before_focus(context), context);}// This detects down arrow key presses when the overlay- // is open and scrolls through.- if (event_key === "down_arrow" || event_key === "vim_down") {+ // is open and scrolls through. If the focused element+ // extends below the visible area, first scroll to reveal+ // more of it before moving focus to the next one.+ if (+ (event_key === "down_arrow" || event_key === "vim_down") &&+ !scroll_toward_visible(row_with_focus(context), "down", context) &&+ row_after_focus(context).length > 0+ ) {scroll_to_element(row_after_focus(context), context);}@@ -150,6 +162,55 @@ function initialize_focus(event_name: string, context: Context): void {return;}+// When a focused element is taller than the scroll viewport (e.g. a long+// draft), pressing an arrow key should first scroll to reveal the hidden+// portion of the element before moving focus to the next one. Returns+// true if the element extended beyond the viewport in the given direction+// and we scrolled within it.+//+// We use a 1px threshold to avoid getting trapped by sub-pixel rounding:+// at fractional scroll positions, row_top or row_bottom can be a fraction+// like -0.3 that never reaches exactly 0, causing infinite negligible+// scrolls that prevent moving to the adjacent element.+const SCROLL_THRESHOLD = 1;++function scroll_toward_visible(+ $focused_row: JQuery,+ direction: "up" | "down",+ context: Context,+): boolean {+ if ($focused_row.length === 0) {+ return false;+ }++ const $items_list = $(`.${CSS.escape(context.items_list_selector)}`);+ const $scroll_container = scroll_util.get_scroll_element($items_list);++ const row_offset = $focused_row.offset()?.top ?? 0;+ const container_offset = $scroll_container.offset()?.top ?? 0;+ const row_top = row_offset - container_offset;+ const row_bottom = row_top + ($focused_row.innerHeight() ?? 0);+ const container_height = $scroll_container.height() ?? 0;++ if (direction === "down" && row_bottom > container_height + SCROLL_THRESHOLD) {+ // Element extends below the visible area; scroll down by up to+ // one viewport height, or just enough to reach the bottom.+ const scroll_amount = Math.min(container_height, row_bottom - container_height);+ $scroll_container.scrollTop(($scroll_container.scrollTop() ?? 0) + scroll_amount);+ return true;+ }++ if (direction === "up" && row_top < -SCROLL_THRESHOLD) {+ // Element extends above the visible area; scroll up by up to+ // one viewport height, or just enough to reach the top.+ const scroll_amount = Math.min(container_height, -row_top);+ $scroll_container.scrollTop(($scroll_container.scrollTop() ?? 0) - scroll_amount);+ return true;+ }++ return false;+}+function scroll_to_element($element: JQuery, context: Context): void {if ($element[0] === undefined) {return;