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.

inbox: Extract function to check if we can show focus outline.

Aman Agrawal44c8d34d6bf3
diff --git a/web/src/focus_outline_util.ts b/web/src/focus_outline_util.ts
new file mode 100644
index 00000000000..aeb75fb43b8
--- /dev/null
+++ b/web/src/focus_outline_util.ts
@@ -0,0 +1,34 @@
+const NAVIGATION_KEYS = new Set([
+ "tab",
+ "shift_tab",
+ "up_arrow",
+ "down_arrow",
+ "left_arrow",
+ "right_arrow",
+ "vim_up",
+ "vim_down",
+ "vim_left",
+ "vim_right",
+ "page_up",
+ "page_down",
+]);
+
+/**
+ * Remove the `no-visible-focus-outlines` CSS class from the given
+ * container when the user presses a navigation key, so that
+ * subsequent `:focus` outlines become visible.
+ *
+ * Returns `true` when the class was present and has just been
+ * removed (i.e. this is the first navigation keypress), so callers
+ * can decide whether to skip the normal navigation action.
+ */
+export function maybe_show_focus_outlines(
+ $container: JQuery,
+ input_key: string,
+): boolean {
+ if ($container.hasClass("no-visible-focus-outlines") && NAVIGATION_KEYS.has(input_key)) {
+ $container.removeClass("no-visible-focus-outlines");
+ return true;
+ }
+ return false;
+}
diff --git a/web/src/inbox_ui.ts b/web/src/inbox_ui.ts
index f1e3456ff26..346d9509209 100644
--- a/web/src/inbox_ui.ts
+++ b/web/src/inbox_ui.ts
@@ -21,6 +21,7 @@ import * as compose_state from "./compose_state.ts";
import * as dialog_widget from "./dialog_widget.ts";
import * as dropdown_widget from "./dropdown_widget.ts";
import type {Filter} from "./filter";
+import * as focus_outline_util from "./focus_outline_util.ts";
import * as hash_util from "./hash_util.ts";
import {$t, $t_html} from "./i18n.ts";
import * as inbox_util from "./inbox_util.ts";
@@ -1852,11 +1853,11 @@ function page_down_navigation(): void {
}
export function change_focused_element(input_key: string): boolean {
- const is_first_user_keypress = $("#inbox-view").hasClass("no-visible-focus-outlines");
- if (is_first_user_keypress) {
- // Start showing visible focus outlines.
- $("#inbox-view").removeClass("no-visible-focus-outlines");
- }
+ const is_first_user_keypress = focus_outline_util.maybe_show_focus_outlines(
+ $("#inbox-view"),
+ input_key,
+ );
+
if (is_first_user_keypress && !is_navigated_to_search()) {
// User has barely scrolled the page.
if (window.scrollY < 30) {