Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
sidebars: Sync Tab and arrow key navigation in sidebars.
Evy Kassirerd35eee14c861
diff --git a/tools/test-js-with-node b/tools/test-js-with-nodeindex a1eafaf8f8d..f923d0edd1c 100755--- a/tools/test-js-with-node+++ b/tools/test-js-with-node@@ -46,6 +46,7 @@ EXEMPT_FILES = make_set(["web/src/about_zulip.ts","web/src/abstract_gif_network.ts",+ "web/src/activity_ui.ts","web/src/add_group_members_pill.ts","web/src/add_stream_options_popover.ts","web/src/add_subscribers_pill.ts",@@ -254,6 +255,7 @@ EXEMPT_FILES = make_set("web/src/settings_user_topics.ts","web/src/settings_users.ts","web/src/setup.ts",+ "web/src/sidebar_tooltip_helpers.ts","web/src/sidebar_ui.ts","web/src/spectators.ts","web/src/spoilers.ts",diff --git a/web/src/activity_ui.ts b/web/src/activity_ui.tsindex 079309ccf0f..fcbcf681e1a 100644--- a/web/src/activity_ui.ts+++ b/web/src/activity_ui.ts@@ -268,6 +268,142 @@ function keydown_enter_key(): void {popovers.hide_all();}+function focus_user_row($row: JQuery): void {+ util.the($row.find("a.user-presence-link")).focus({preventScroll: true});+}++// Helpers that find the nearest visible user row relative to a section header+// or link. "Visible" means not inside a collapsed section.++function first_user_in_or_after($section: JQuery): JQuery {+ if (!$section.hasClass("collapsed")) {+ const $entry = $section.find("li.user_sidebar_entry").first();+ if ($entry.length > 0) {+ return $entry;+ }+ }+ return first_user_after($section);+}+function first_user_after($section: JQuery): JQuery {+ return $section.nextAll(":not(.collapsed)").find("li.user_sidebar_entry").first();+}+function last_user_before($section: JQuery): JQuery {+ return $section.prevAll(":not(.collapsed)").find("li.user_sidebar_entry").last();+}+function last_user_in_or_before($section: JQuery): JQuery {+ if (!$section.hasClass("collapsed")) {+ const $entry = $section.find("li.user_sidebar_entry").last();+ if ($entry.length > 0) {+ return $entry;+ }+ }+ return last_user_before($section);+}+function last_user_in_buddy_list(): JQuery {+ return $(+ "#buddy_list_wrapper .buddy-list-section-container:not(.collapsed) li.user_sidebar_entry",+ ).last();+}++// Given the currently-focused element and arrow-key direction, return the user+// row we should land on. Returns undefined to do nothing (e.g., ArrowDown from+// the last element in the buddy list).+function resolve_arrow_target($active: JQuery, direction: "up" | "down"): JQuery | undefined {+ const $active_user_row = $active.closest("li.user_sidebar_entry");+ if ($active_user_row.length > 0) {+ // Focus is inside a user row (e.g., the user's name link or the vdot+ // menu icon). Return that row; the caller syncs the cursor to it and+ // then steps prev/next, because the cursor already knows which rows+ // are visible — DOM traversal would have to replicate that logic.+ return $active_user_row;+ }++ const $section = $active.closest(".buddy-list-section-container");+ if ($active.closest(".buddy-list-subsection-header").length > 0) {+ // Focus is on a section header (the toggle triangle or the heading).+ // ArrowDown lands on the first user in this section if it's expanded,+ // or the first user in the next expanded section. ArrowUp lands on+ // the last user in the previous expanded section.+ return direction === "down" ? first_user_in_or_after($section) : last_user_before($section);+ }+ if ($active.closest(".view-all-subscribers-link").length > 0) {+ // Focus is on the "View all subscribers" link, which sits below the+ // users in the "users matching view" section. ArrowDown crosses into+ // the next section's first user; ArrowUp goes back to the last user+ // of the current section.+ return direction === "down" ? first_user_after($section) : last_user_in_or_before($section);+ }+ if (+ $active.closest(".view-all-users-link").length > 0 ||+ $active.closest(".invite-user-shortcut").length > 0+ ) {+ // Focus is on one of the two links at the very bottom of the buddy+ // list ("View all users" or "Invite to organization"). ArrowDown has+ // nowhere to go; ArrowUp lands on the last visible user row.+ return direction === "down" ? undefined : last_user_in_buddy_list();+ }+ // We've installed this handler on #buddy_list_wrapper, so every focusable+ // descendant should be covered by a branch above. Log and bail if not.+ blueslip.error("Unexpected focused element in buddy list", {+ element: $active[0]?.nodeName,+ class: $active[0]?.className,+ });+ return undefined;+}++// Handle arrow key navigation when a buddy list element has Tab focus,+// so that Tab and arrow key navigation stay in sync. Three steps:+// (a) resolve which user row we should land on,+// (b) sync the cursor to it,+// (c) move DOM focus to it.+function handle_buddy_list_arrow_navigation(e: JQuery.KeyDownEvent): void {+ // This handler is registered inside set_cursor_and_filter, which creates+ // user_cursor, so it's always defined by the time we get here.+ assert(user_cursor !== undefined);++ if (e.key === "Tab") {+ // Tab is handled by the browser, but we clear the cursor highlight+ // so it doesn't remain painted on the arrow-navigated row after+ // focus moves elsewhere.+ user_cursor.clear();+ return;+ }+ if (e.key !== "ArrowUp" && e.key !== "ArrowDown") {+ return;+ }+ if (e.altKey || e.ctrlKey || e.shiftKey || !(document.activeElement instanceof HTMLElement)) {+ return;+ }++ const direction = e.key === "ArrowDown" ? "down" : "up";+ const $active = $(document.activeElement);+ const $landing_row = resolve_arrow_target($active, direction);+ if ($landing_row === undefined || $landing_row.length === 0) {+ return;+ }++ user_cursor.set_is_highlight_visible(true);+ user_cursor.go_to(buddy_list.get_user_id_from_li({$li: $landing_row}));++ // If focus was inside a user row, we landed on *that* user; step the+ // cursor one further in the arrow direction.+ let $focus_row = $landing_row;+ if ($active.closest("li.user_sidebar_entry").length > 0) {+ if (direction === "down") {+ user_cursor.next();+ } else {+ user_cursor.prev();+ }+ const new_user_id = user_cursor.get_key();+ assert(new_user_id !== undefined);+ $focus_row = buddy_list.find_li({key: new_user_id, force_render: true})!;+ }++ focus_user_row($focus_row);+ e.preventDefault();+ e.stopPropagation();+}+export let set_cursor_and_filter = (): void => {user_cursor = new ListCursor({list: buddy_list,@@ -306,8 +442,21 @@ export let set_cursor_and_filter = (): void => {user_cursor!.next();return true;},+ Tab() {+ // If the user navigated to a row with arrow keys, Tab should focus that+ // row instead of the next element in DOM order.+ assert(user_cursor !== undefined);+ const cursor_key = user_cursor.get_key();+ if (cursor_key !== undefined && user_cursor.is_highlight_visible) {+ focus_user_row(buddy_list.find_li({key: cursor_key, force_render: true})!);+ }+ user_cursor.clear();+ return false;+ },},});++ $("#buddy_list_wrapper").on("keydown", handle_buddy_list_arrow_navigation);};export function rewire_set_cursor_and_filter(value: typeof set_cursor_and_filter): void {diff --git a/web/src/buddy_list.ts b/web/src/buddy_list.tsindex 020e4e7b516..c84c6ecbb21 100644--- a/web/src/buddy_list.ts+++ b/web/src/buddy_list.ts@@ -24,10 +24,11 @@ import * as peer_data from "./peer_data.ts";import * as people from "./people.ts";import * as scroll_util from "./scroll_util.ts";import * as settings_config from "./settings_config.ts";+import {disconnect_toggle_class, observe_toggle_class} from "./sidebar_tooltip_helpers.ts";import {current_user} from "./state_data.ts";import * as stream_data from "./stream_data.ts";import type {StreamSubscription} from "./sub_store.ts";-import {INTERACTIVE_HOVER_DELAY} from "./tippyjs.ts";+import {EXTRA_LONG_HOVER_DELAY, INTERACTIVE_HOVER_DELAY} from "./tippyjs.ts";import * as ui_util from "./ui_util.ts";import {user_settings} from "./user_settings.ts";import * as util from "./util.ts";@@ -301,6 +302,26 @@ export class BuddyList extends BuddyListConf {});},);++ tippy.delegate("body", {+ target: ".section-toggle-tooltip-target",+ onShow(instance) {+ const $toggle = $(instance.reference);+ observe_toggle_class(instance, () => {+ if ($toggle.hasClass("rotate-icon-down")) {+ instance.setContent($t({defaultMessage: "Collapse section"}));+ } else {+ instance.setContent($t({defaultMessage: "Expand section"}));+ }+ });+ },+ delay: EXTRA_LONG_HOVER_DELAY,+ appendTo: () => document.body,+ onHidden(instance) {+ disconnect_toggle_class(instance);+ instance.destroy();+ },+ });}async non_participant_users_matching_view_count(): Promise<number | null> {diff --git a/web/src/left_sidebar_tooltips.ts b/web/src/left_sidebar_tooltips.tsindex 709c489e04e..961343397ea 100644--- a/web/src/left_sidebar_tooltips.ts+++ b/web/src/left_sidebar_tooltips.ts@@ -6,6 +6,7 @@ import * as drafts from "./drafts.ts";import {$t} from "./i18n.ts";import * as scheduled_messages from "./scheduled_messages.ts";import * as settings_data from "./settings_data.ts";+import {disconnect_toggle_class, observe_toggle_class} from "./sidebar_tooltip_helpers.ts";import * as starred_messages from "./starred_messages.ts";import {EXTRA_LONG_HOVER_DELAY,@@ -16,24 +17,6 @@ import {import * as unread from "./unread.ts";import {user_settings} from "./user_settings.ts";-// Watches the reference element's class for changes while a tooltip is-// visible, so the content stays accurate when the element is toggled via-// keyboard (or any other path) without hiding the tooltip first.-const class_observers = new WeakMap<tippy.Instance, MutationObserver>();-function observe_toggle_class(instance: tippy.Instance, update: () => void): void {- update();- const observer = new MutationObserver(update);- observer.observe(instance.reference, {- attributes: true,- attributeFilter: ["class"],- });- class_observers.set(instance, observer);-}-function disconnect_toggle_class(instance: tippy.Instance): void {- class_observers.get(instance)?.disconnect();- class_observers.delete(instance);-}-export function initialize(): void {tippy.delegate("body", {target: ".tippy-left-sidebar-tooltip",@@ -239,28 +222,6 @@ export function initialize(): void {},});- // This is actually for the right sidebar, but putting it here because it has- // shared logic.- tippy.delegate("body", {- target: ".section-toggle-tooltip-target",- onShow(instance) {- const $toggle = $(instance.reference);- observe_toggle_class(instance, () => {- if ($toggle.hasClass("rotate-icon-down")) {- instance.setContent($t({defaultMessage: "Collapse section"}));- } else {- instance.setContent($t({defaultMessage: "Expand section"}));- }- });- },- delay: EXTRA_LONG_HOVER_DELAY,- appendTo: () => document.body,- onHidden(instance) {- disconnect_toggle_class(instance);- instance.destroy();- },- });-tippy.delegate("body", {target: ".header-main .column-left .left-sidebar-toggle-button",delay: LONG_HOVER_DELAY,diff --git a/web/src/sidebar_tooltip_helpers.ts b/web/src/sidebar_tooltip_helpers.tsnew file mode 100644index 00000000000..0b781b53e99--- /dev/null+++ b/web/src/sidebar_tooltip_helpers.ts@@ -0,0 +1,19 @@+import type * as tippy from "tippy.js";++// Watches the reference element's class for changes while a tooltip is+// visible, so the content stays accurate when the element is toggled via+// keyboard (or any other path) without hiding the tooltip first.+const class_observers = new WeakMap<tippy.Instance, MutationObserver>();+export function observe_toggle_class(instance: tippy.Instance, update: () => void): void {+ update();+ const observer = new MutationObserver(update);+ observer.observe(instance.reference, {+ attributes: true,+ attributeFilter: ["class"],+ });+ class_observers.set(instance, observer);+}+export function disconnect_toggle_class(instance: tippy.Instance): void {+ class_observers.get(instance)?.disconnect();+ class_observers.delete(instance);+}diff --git a/web/src/sidebar_ui.ts b/web/src/sidebar_ui.tsindex 26132703ec3..6855238b142 100644--- a/web/src/sidebar_ui.ts+++ b/web/src/sidebar_ui.ts@@ -1,10 +1,12 @@import $ from "jquery";import _ from "lodash";+import assert from "minimalistic-assert";import render_left_sidebar from "../templates/left_sidebar.hbs";import render_buddy_list_popover from "../templates/popovers/buddy_list_popover.hbs";import render_right_sidebar from "../templates/right_sidebar.hbs";+import * as blueslip from "./blueslip.ts";import {buddy_list} from "./buddy_list.ts";import * as channel from "./channel.ts";import * as compose_ui from "./compose_ui.ts";@@ -668,6 +670,111 @@ const update_left_sidebar_for_search = _.throttle(() => {}}, 50);+function focus_left_sidebar_row($row: JQuery): void {+ // For header rows, focus the section toggle icon.+ if ($row.hasClass("left-sidebar-section-header")) {+ util.the($row.find(".sidebar-heading-icon")).focus({preventScroll: true});+ return;+ }+ // For focusable rows, either they have an <a> tag that gets+ // focus, or a tabindex="0" label directly on them. Determine+ // which case we're in and focus the appropriate element.+ const $link = $row.find("a");+ if ($link.length > 0) {+ util.the($link).focus({preventScroll: true});+ } else if (util.the($row).tabIndex === 0) {+ util.the($row).focus({preventScroll: true});+ } else {+ blueslip.error("Left sidebar row has no focusable child", {+ class: util.the($row).className,+ });+ }+}++// Given the currently-focused element and arrow-key direction, return the row+// we should land on, or undefined to do nothing. The returned row is where we+// sync the cursor; if focus was already in a navigable row, we sync to it and+// then step one further in `direction`.+function resolve_left_sidebar_arrow_target(+ $active: JQuery,+ direction: "up" | "down",+): JQuery | undefined {+ const $row = $active.closest(".top_left_row, .bottom_left_row, .left-sidebar-section-header");+ if ($row.length > 0) {+ // Focus is inside a navigable row (a VIEWS entry, a channel/topic/DM+ // row, or a section header). Return that row; the caller syncs the+ // cursor to it and then steps prev/next, since the cursor already+ // knows which rows are visible.+ return $row;+ }+ if ($active.closest("#subscribe-to-more-streams").length > 0 && direction === "up") {+ // Focus is on the "Browse channels" / "Create a channel" link, which+ // sits below all channel rows. ArrowDown has nowhere to go; ArrowUp+ // lands on the last visible row.+ return all_rows().last();+ }+ // Focus isn't on anything we navigate between (e.g. an inline action+ // button inside a row that we don't special-case). Do nothing.+ return undefined;+}++// Handle arrow key navigation when a left sidebar element has Tab focus,+// so that Tab and arrow key navigation stay in sync. Three steps:+// (a) resolve which row we should land on,+// (b) sync the cursor to it,+// (c) move DOM focus to it.+function handle_left_sidebar_arrow_navigation(e: JQuery.KeyDownEvent): void {+ if (e.key === "Tab") {+ // Tab is handled by the browser, but we clear the cursor highlight+ // so it doesn't remain painted on the arrow-navigated row after+ // focus moves elsewhere.+ left_sidebar_cursor.clear();+ return;+ }+ if (e.key !== "ArrowUp" && e.key !== "ArrowDown") {+ return;+ }+ if (e.altKey || e.ctrlKey || e.shiftKey || !(document.activeElement instanceof HTMLElement)) {+ return;+ }++ // Search inputs have their own arrow key handlers.+ const $active = $(document.activeElement);+ if (+ $active.is(".left-sidebar-search-input, .direct-messages-list-filter, #topic_filter_query")+ ) {+ return;+ }++ const direction = e.key === "ArrowDown" ? "down" : "up";+ const $landing_row = resolve_left_sidebar_arrow_target($active, direction);+ if ($landing_row === undefined || $landing_row.length === 0) {+ return;+ }++ left_sidebar_cursor.set_is_highlight_visible(true);+ left_sidebar_cursor.go_to($landing_row);++ // If focus was already inside a navigable row, we landed on *that* row;+ // step the cursor one further in the arrow direction.+ if (+ $active.closest(".top_left_row, .bottom_left_row, .left-sidebar-section-header").length > 0+ ) {+ if (direction === "down") {+ left_sidebar_cursor.next();+ } else {+ left_sidebar_cursor.prev();+ }+ }++ const $new_row = left_sidebar_cursor.get_key();+ assert($new_row !== undefined);+ focus_left_sidebar_row($new_row);++ e.preventDefault();+ e.stopPropagation();+}+function focus_left_sidebar_filter(e: JQuery.ClickEvent): void {left_sidebar_cursor.reset();e.stopPropagation();@@ -737,6 +844,16 @@ export function set_event_handlers(): void {left_sidebar_cursor.next();return true;},+ Tab() {+ // If the user navigated to a row with arrow keys, Tab should focus+ // that row instead of the next element in DOM order.+ const $row = left_sidebar_cursor.get_key();+ if ($row !== undefined && left_sidebar_cursor.is_highlight_visible) {+ focus_left_sidebar_row($row);+ }+ left_sidebar_cursor.clear();+ return false;+ },},});@@ -745,6 +862,10 @@ export function set_event_handlers(): void {left_sidebar_cursor.clear();});$search_input.on("input", update_left_sidebar_for_search);++ // Handle arrow key navigation when a sidebar element has Tab+ // focus, so that Tab and arrow key navigation stay in sync.+ $("#left-sidebar").on("keydown", handle_left_sidebar_arrow_navigation);}export function initiate_search(): void {diff --git a/web/styles/left_sidebar.css b/web/styles/left_sidebar.cssindex 6b2583c88a1..5e23cdd307a 100644--- a/web/styles/left_sidebar.css+++ b/web/styles/left_sidebar.css@@ -776,6 +776,18 @@}}+/* Arrow-key sidebar navigation focuses the toggle icon inside a+ section header row while also applying .highlighted_row to the row+ itself. Suppress the inner outline in that case to avoid showing+ two nested focus rings on the same row. The IDs are needed to win+ over the ID-scoped :focus-visible rules on the Views and DM+ toggle icons above. */+.highlighted_row #toggle-direct-messages-section-icon:focus-visible,+.highlighted_row #toggle-top-left-navigation-area-icon:focus-visible,+.highlighted_row .stream-list-section-toggle:focus-visible {+ outline: 0;+}+.active-direct-messages-section {background-color: var(--color-background-active-narrow-filter);