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.

sidebar_ui: Preserve scroll position when toggling left sidebar.

sandeepcce00d559812
diff --git a/web/src/sidebar_ui.ts b/web/src/sidebar_ui.ts
index 0d543cbee6e..38368e8f460 100644
--- a/web/src/sidebar_ui.ts
+++ b/web/src/sidebar_ui.ts
@@ -17,7 +17,6 @@ import {ListCursor} from "./list_cursor.ts";
import {localstorage} from "./localstorage.ts";
import * as message_lists from "./message_lists.ts";
import * as message_reminder from "./message_reminder.ts";
-import * as message_viewport from "./message_viewport.ts";
import {page_params} from "./page_params.ts";
import * as pm_list from "./pm_list.ts";
import * as popover_menus from "./popover_menus.ts";
@@ -39,6 +38,25 @@ const LEFT_SIDEBAR_NAVIGATION_AREA_TITLE = $t({defaultMessage: "VIEWS"});
export let left_sidebar_cursor: ListCursor<JQuery>;
+// Toggling a sidebar changes the message feed's width, causing rows to
+// reflow and shift vertically. Preserve the selected row's viewport
+// offset across the layout change to keep the user's reading position
+// stable.
+function toggle_sidebar_preserving_selected_row_offset(classname: string): void {
+ let saved_selected_row_offset: number | undefined;
+ if (message_lists.current !== undefined) {
+ const $selected_row = message_lists.current.selected_row();
+ if ($selected_row.length > 0) {
+ saved_selected_row_offset = $selected_row.get_offset_to_window().top;
+ }
+ }
+ $("body").toggleClass(classname);
+ if (saved_selected_row_offset !== undefined) {
+ assert(message_lists.current !== undefined);
+ message_lists.current.view.set_message_offset(saved_selected_row_offset);
+ }
+}
+
function save_sidebar_toggle_status(): void {
const ls = localstorage();
ls.set("left-sidebar", $("body").hasClass("hide-left-sidebar"));
@@ -232,15 +250,7 @@ export function initialize(): void {
e.stopPropagation();
if (ui_util.matches_viewport_state("gte_md_min")) {
- $("body").toggleClass("hide-left-sidebar");
- if (
- message_lists.current !== undefined &&
- !ui_util.matches_viewport_state("gte_xl_min")
- ) {
- // We expand the middle column width between md and xl breakpoints when the
- // left sidebar is hidden. This can cause the pointer to move out of view.
- message_viewport.scroll_to_selected();
- }
+ toggle_sidebar_preserving_selected_row_offset("hide-left-sidebar");
// We recheck the scrolling-button status of the compose
// box, which may change for users who've chosen to
// use full width on wide screens.