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.

overlays: Fix focus behaviour for drafts overlay.

Pritesh Shetty36b570cb5a41
diff --git a/web/src/overlay_util.ts b/web/src/overlay_util.ts
index 20472358270..1e2f3abf402 100644
--- a/web/src/overlay_util.ts
+++ b/web/src/overlay_util.ts
@@ -15,14 +15,45 @@ export function enable_scrolling(): void {
}
export const OVERLAY_FOCUSABLE_SELECTOR =
- "input, button, select, .input, .sidebar-item, .ind-tab.first, a[href], a[tabindex='0']";
+ "input, button, select, .input, .sidebar-item, .ind-tab.first, a[href], a[tabindex='0'], .overlay-message-info-box";
export function get_visible_focusable_elements_in_overlay_container(
$container: JQuery,
): HTMLElement[] {
const visible_focusable_elements = [...$(OVERLAY_FOCUSABLE_SELECTOR, $container)].filter(
(element) =>
- element.getClientRects().length > 0 && $(element).css("visibility") !== "hidden",
+ element.getClientRects().length > 0 &&
+ $(element).css("visibility") !== "hidden" &&
+ !$(element).is(":disabled"),
);
return visible_focusable_elements;
}
+
+export function wrap_overlay_tab_focus(
+ shift_key: boolean,
+ focusable_elements: HTMLElement[],
+): boolean {
+ if (focusable_elements.length === 0) {
+ // Nothing focusable inside the overlay; keep Tab trapped within it.
+ return true;
+ }
+
+ const active_element =
+ document.activeElement instanceof HTMLElement ? document.activeElement : null;
+ const focus_is_in_overlay =
+ active_element !== null && focusable_elements.includes(active_element);
+ const first_element = focusable_elements[0]!;
+ const last_element = focusable_elements.at(-1)!;
+
+ if (shift_key) {
+ if (active_element === first_element || !focus_is_in_overlay) {
+ last_element.focus();
+ return true;
+ }
+ } else if (active_element === last_element || !focus_is_in_overlay) {
+ first_element.focus();
+ return true;
+ }
+
+ return false;
+}
diff --git a/web/src/overlays.ts b/web/src/overlays.ts
index fce0a0744c5..d8d16315f88 100644
--- a/web/src/overlays.ts
+++ b/web/src/overlays.ts
@@ -124,6 +124,7 @@ export function open_overlay(opts: OverlayOptions): void {
opts.$overlay.attr("aria-hidden", "false");
$(".app").attr("aria-hidden", "true");
$("#navbar-fixed-container").attr("aria-hidden", "true");
+ opts.$overlay.trigger("focus");
}
export function close_overlay(name: string): void {
@@ -323,4 +324,18 @@ export function trap_focus_for_settings_overlay(): void {
}
}
});
+
+ const overlay_focus_trap_selector = "#draft_overlay";
+ $("body").on("keydown", overlay_focus_trap_selector, function (this: HTMLElement, e) {
+ if (e.key !== "Tab") {
+ return;
+ }
+
+ const visible_focusable_elements =
+ overlay_util.get_visible_focusable_elements_in_overlay_container($(this));
+
+ if (overlay_util.wrap_overlay_tab_focus(e.shiftKey, visible_focusable_elements)) {
+ e.preventDefault();
+ }
+ });
}
diff --git a/web/styles/app_components.css b/web/styles/app_components.css
index 84781f6beca..af35946107d 100644
--- a/web/styles/app_components.css
+++ b/web/styles/app_components.css
@@ -247,6 +247,10 @@ div.overlay {
.overlay-container {
background-color: var(--color-background-modal);
}
+
+ &:focus {
+ outline: none;
+ }
}
.input-append {
diff --git a/web/templates/draft_table_body.hbs b/web/templates/draft_table_body.hbs
index 82d7f8a822d..739f7bdcaf1 100644
--- a/web/templates/draft_table_body.hbs
+++ b/web/templates/draft_table_body.hbs
@@ -1,4 +1,4 @@
-<div id="draft_overlay" class="overlay" data-overlay="drafts">
+<div id="draft_overlay" class="overlay" data-overlay="drafts" tabindex="-1">
<div class="flex overlay-content">
<div class="drafts-container overlay-messages-container overlay-container">
<div class="overlay-messages-header">