Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep Tab inside a dialog with no controls

Yudhi Armyndharis449991b47bf3
diff --git a/dashboard/src/utils/modalA11y.test.ts b/dashboard/src/utils/modalA11y.test.ts
index 6f6902c89..18979e8a4 100644
--- a/dashboard/src/utils/modalA11y.test.ts
+++ b/dashboard/src/utils/modalA11y.test.ts
@@ -70,6 +70,7 @@ function makeWorld(focusableCount = 2) {
doc.activeElement = card as never;
},
querySelectorAll: () => focusables,
+ contains: (el: unknown) => el === card || focusables.includes(el as (typeof focusables)[number]),
};
attached.add(card);
@@ -274,3 +275,73 @@ test('Tab and Shift+Tab on a control that is neither first nor last are left to
assert.equal(forwardFromFirst.defaultPrevented, false);
unbind();
});
+
+test('Tab stays on an empty dialog card in either direction', () => {
+ const { doc, card } = makeWorld(0);
+ const unbind = bindModalA11y(doc as never, card as never, () => {});
+ try {
+ for (const shiftKey of [false, true]) {
+ const event = makeKeyEvent('Tab', shiftKey);
+ doc.dispatchKey(event);
+ assert.equal(event.defaultPrevented, true);
+ assert.equal(doc.activeElement, card as never);
+ }
+ } finally {
+ unbind();
+ }
+});
+
+test('a dialog regains focus after its last controls disappear', () => {
+ const { doc, card, focusables } = makeWorld(2);
+ const unbind = bindModalA11y(doc as never, card as never, () => {});
+ try {
+ focusables.splice(0);
+ const event = makeKeyEvent('Tab');
+ doc.dispatchKey(event);
+ assert.equal(event.defaultPrevented, true);
+ assert.equal(doc.activeElement, card as never);
+ } finally {
+ unbind();
+ }
+});
+
+test('Tab recovers focus from outside the active dialog in either direction', () => {
+ const { doc, card, focusables, makeElement } = makeWorld(2);
+ const background = makeElement();
+ const unbind = bindModalA11y(doc as never, card as never, () => {});
+ try {
+ for (const shiftKey of [false, true]) {
+ background.focus();
+ const event = makeKeyEvent('Tab', shiftKey);
+ doc.dispatchKey(event);
+ assert.equal(event.defaultPrevented, true);
+ assert.equal(doc.activeElement, focusables[shiftKey ? 1 : 0] as never);
+ }
+ } finally {
+ unbind();
+ }
+});
+
+test('a lower dialog does not handle Tab while the nested dialog owns focus', () => {
+ const { doc, card, makeElement } = makeWorld(0);
+ const controls = Array.from({ length: 3 }, makeElement);
+ const nested = {
+ focus() {
+ doc.activeElement = nested as never;
+ },
+ querySelectorAll: () => controls,
+ contains: (el: unknown) => el === nested || controls.includes(el as (typeof controls)[number]),
+ };
+ const unbindParent = bindModalA11y(doc as never, card as never, () => {});
+ const unbindNested = bindModalA11y(doc as never, nested as never, () => {});
+ try {
+ controls[1].focus();
+ const event = makeKeyEvent('Tab');
+ doc.dispatchKey(event);
+ assert.equal(event.defaultPrevented, false);
+ assert.equal(doc.activeElement, controls[1] as never);
+ } finally {
+ unbindNested();
+ unbindParent();
+ }
+});
diff --git a/dashboard/src/utils/modalA11y.ts b/dashboard/src/utils/modalA11y.ts
index 25ecc792e..d9e4879c8 100644
--- a/dashboard/src/utils/modalA11y.ts
+++ b/dashboard/src/utils/modalA11y.ts
@@ -49,9 +49,8 @@ export function bindModalA11y(
modalStack.push(stackEntry);
const onKeyDown = (event: KeyboardEvent) => {
+ if (modalStack[modalStack.length - 1] !== stackEntry) return;
if (event.key === 'Escape') {
- // A nested dialog is open above this one — it owns Escape (see modalStack).
- if (modalStack[modalStack.length - 1] !== stackEntry) return;
// Capture phase: nested widgets (selects, menus) may also listen for Escape — the dialog
// owns dismissal while it is open.
event.stopPropagation();
@@ -60,10 +59,17 @@ export function bindModalA11y(
}
if (event.key === 'Tab') {
const focusables = visibleFocusables(card);
- if (focusables.length === 0) return;
+ if (focusables.length === 0) {
+ event.preventDefault();
+ card.focus();
+ return;
+ }
const first = focusables[0];
const last = focusables[focusables.length - 1];
- if (event.shiftKey && doc.activeElement === first) {
+ if (!card.contains(doc.activeElement)) {
+ event.preventDefault();
+ (event.shiftKey ? last : first).focus();
+ } else if (event.shiftKey && doc.activeElement === first) {
event.preventDefault();
last.focus();
} else if (!event.shiftKey && doc.activeElement === last) {