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.

settings: Fix focus behavior during keyboard navigation.

Sahil Batra27efd873112c
diff --git a/web/src/overlay_util.ts b/web/src/overlay_util.ts
index 9d323575f47..3762d62887c 100644
--- a/web/src/overlay_util.ts
+++ b/web/src/overlay_util.ts
@@ -13,3 +13,17 @@ export function disable_scrolling(): void {
export function enable_scrolling(): void {
$(":root").css({"overflow-y": "scroll", "--disabled-scrollbar-width": "0px"});
}
+
+export function get_visible_focusable_elements_in_overlay_container(
+ $container: JQuery,
+): HTMLElement[] {
+ const visible_focusable_elements = [
+ ...$container.find(
+ "input, button, select, .input, .sidebar-item, .ind-tab.first, a[href], a[tabindex='0']",
+ ),
+ ].filter(
+ (element) =>
+ element.getClientRects().length > 0 && $(element).css("visibility") !== "hidden",
+ );
+ return visible_focusable_elements;
+}
diff --git a/web/src/overlays.ts b/web/src/overlays.ts
index 09bcaa22870..3e11cd196c2 100644
--- a/web/src/overlays.ts
+++ b/web/src/overlays.ts
@@ -204,3 +204,93 @@ export function initialize(): void {
e.stopPropagation();
});
}
+
+export function trap_focus_for_settings_overlay(): void {
+ $("#settings_overlay_container").on("keydown", (e) => {
+ if (e.key !== "Tab") {
+ return;
+ }
+
+ const two_column_mode = $("#settings_content").css("--single-column") === undefined;
+ const $settings_overlay_container = $("#settings_overlay_container");
+ let visible_focusable_elements;
+ if (two_column_mode) {
+ visible_focusable_elements =
+ overlay_util.get_visible_focusable_elements_in_overlay_container(
+ $settings_overlay_container,
+ );
+ } else {
+ const $right_section = $settings_overlay_container.find(".content-wrapper");
+ const $left_section = $settings_overlay_container.find(".sidebar-wrapper");
+ if ($right_section.hasClass("show")) {
+ const $settings_panel = $right_section.find(".settings-section.show");
+ visible_focusable_elements =
+ overlay_util.get_visible_focusable_elements_in_overlay_container(
+ $settings_panel,
+ );
+ } else {
+ visible_focusable_elements =
+ overlay_util.get_visible_focusable_elements_in_overlay_container($left_section);
+ }
+ }
+
+ if (visible_focusable_elements.length === 0) {
+ return;
+ }
+
+ if (e.shiftKey) {
+ if (document.activeElement === visible_focusable_elements[0]) {
+ e.preventDefault();
+ visible_focusable_elements.at(-1)!.focus();
+ }
+ } else {
+ if (document.activeElement === visible_focusable_elements.at(-1)) {
+ e.preventDefault();
+ visible_focusable_elements[0]!.focus();
+ }
+ }
+ });
+
+ $("#channels_overlay_container, #groups_overlay_container").on("keydown", (e) => {
+ if (e.key !== "Tab") {
+ return;
+ }
+
+ const $overlay = $(e.currentTarget);
+ const two_column_mode =
+ $overlay.find(".two-pane-settings-container").css("--single-column") === undefined;
+ let visible_focusable_elements;
+ if (two_column_mode) {
+ visible_focusable_elements =
+ overlay_util.get_visible_focusable_elements_in_overlay_container($overlay);
+ } else {
+ const $right_section = $overlay.find(".right");
+ const $left_section = $overlay.find(".left");
+ if ($right_section.hasClass("show")) {
+ visible_focusable_elements =
+ overlay_util.get_visible_focusable_elements_in_overlay_container(
+ $right_section,
+ );
+ } else {
+ visible_focusable_elements =
+ overlay_util.get_visible_focusable_elements_in_overlay_container($left_section);
+ }
+ }
+
+ if (visible_focusable_elements.length === 0) {
+ return;
+ }
+
+ if (e.shiftKey) {
+ if (document.activeElement === visible_focusable_elements[0]) {
+ e.preventDefault();
+ visible_focusable_elements.at(-1)!.focus();
+ }
+ } else {
+ if (document.activeElement === visible_focusable_elements.at(-1)) {
+ e.preventDefault();
+ visible_focusable_elements[0]!.focus();
+ }
+ }
+ });
+}
diff --git a/web/src/ui_init.js b/web/src/ui_init.js
index 1b51dee7eea..fb835a4c244 100644
--- a/web/src/ui_init.js
+++ b/web/src/ui_init.js
@@ -753,6 +753,7 @@ export async function initialize_everything(state_data) {
desktop_integration.initialize();
group_permission_settings.initialize();
+ overlays.trap_focus_for_settings_overlay();
$("#app-loading").addClass("loaded");
}
diff --git a/web/styles/subscriptions.css b/web/styles/subscriptions.css
index 2475806508b..90854369999 100644
--- a/web/styles/subscriptions.css
+++ b/web/styles/subscriptions.css
@@ -1474,6 +1474,8 @@ div.settings-radio-input-parent {
position: relative;
overflow: hidden;
+ --single-column: yes;
+
.two-pane-settings-header .fa-chevron-left {
visibility: visible;
}