Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): let one Escape close only the phone drawer

Yudhi Armyndharis9a12f532a3d5
diff --git a/dashboard/src/components/Layout.test.ts b/dashboard/src/components/Layout.test.ts
index 2c1752fe8..35d6593f0 100644
--- a/dashboard/src/components/Layout.test.ts
+++ b/dashboard/src/components/Layout.test.ts
@@ -237,7 +237,7 @@ test('Escape closes the open language menu before the phone drawer', () => {
assert.ok(container.querySelector('.language-menu-list'));
rtl.fireEvent.keyDown(document, { key: 'Escape' });
- assert.equal(container.querySelector('.language-menu-list'), null, 'Escape left the language menu open');
+ assert.ok(container.querySelector('.language-menu-list') === null, 'Escape left the language menu open');
assert.ok(sidebar.classList.contains('open'), 'the same Escape also closed the drawer');
rtl.fireEvent.keyDown(document, { key: 'Escape' });
@@ -247,6 +247,72 @@ test('Escape closes the open language menu before the phone drawer', () => {
}
});
+// The drawer covers the page, so its Escape is the drawer's alone. The Chats page closes the open chat on an Escape
+// nobody prevented, and the emoji picker under the drawer takes the key in the capture phase: one press closed the
+// drawer and the chat behind it, or the picker nobody could see while the drawer stayed open.
+test('one Escape closes the phone drawer and nothing on the page under it', () => {
+ const seen: string[] = [];
+ // Registered before the drawer opens, as a picker or menu left open on the page is.
+ const pageCapture = (event: KeyboardEvent) => {
+ if (event.key !== 'Escape' || event.defaultPrevented) return;
+ event.preventDefault();
+ seen.push('page capture');
+ };
+ const pageBubble = (event: KeyboardEvent) => {
+ if (event.key === 'Escape') seen.push('page bubble');
+ };
+ document.addEventListener('keydown', pageCapture, true);
+ document.addEventListener('keydown', pageBubble);
+ try {
+ resizeTo(390);
+ const { container } = renderLayout();
+ const sidebar = container.querySelector('aside.sidebar')!;
+ rtl.fireEvent.click(container.querySelector('.mobile-menu-btn')!);
+ const link = rtl.screen.getByRole('link', { name: 'Sessions' });
+ link.focus();
+
+ const notPrevented = rtl.fireEvent.keyDown(link, { key: 'Escape' });
+ assert.ok(!sidebar.classList.contains('open'), 'Escape left the drawer open');
+ assert.equal(notPrevented, false, 'the drawer took Escape without preventing its default');
+ assert.deepEqual(seen, [], 'the page under the drawer handled the same Escape');
+
+ // Closed, the drawer leaves Escape to the page.
+ rtl.fireEvent.keyDown(document.body, { key: 'Escape' });
+ assert.deepEqual(seen, ['page capture', 'page bubble']);
+ } finally {
+ document.removeEventListener('keydown', pageCapture, true);
+ document.removeEventListener('keydown', pageBubble);
+ resizeTo(1024);
+ }
+});
+
+// A dialog renders above the drawer and dismisses itself on Escape; an Escape that commits an IME composition
+// belongs to the composition.
+test('the phone drawer leaves Escape to a dialog above it and to a composition', () => {
+ const dialog = document.createElement('div');
+ dialog.setAttribute('role', 'dialog');
+ try {
+ resizeTo(390);
+ const { container } = renderLayout();
+ const sidebar = container.querySelector('aside.sidebar')!;
+ rtl.fireEvent.click(container.querySelector('.mobile-menu-btn')!);
+
+ rtl.fireEvent.keyDown(document.body, { key: 'Escape', isComposing: true });
+ assert.ok(sidebar.classList.contains('open'), 'the Escape of a composition closed the drawer');
+
+ document.body.appendChild(dialog);
+ assert.equal(rtl.fireEvent.keyDown(document.body, { key: 'Escape' }), true, 'the drawer took the Escape');
+ assert.ok(sidebar.classList.contains('open'), 'Escape closed the drawer under a dialog');
+
+ dialog.remove();
+ rtl.fireEvent.keyDown(document.body, { key: 'Escape' });
+ assert.ok(!sidebar.classList.contains('open'), 'Escape left the drawer open once the dialog closed');
+ } finally {
+ dialog.remove();
+ resizeTo(1024);
+ }
+});
+
// The key management, infrastructure and plugin routes refuse a key restricted to selected sessions, whatever
// its role, and every refusal lands in the audit log as a failed attempt.
test('an admin key scoped to sessions is not offered the pages that refuse it', async () => {
diff --git a/dashboard/src/components/Layout.tsx b/dashboard/src/components/Layout.tsx
index 4cd97616b..e959045c9 100644
--- a/dashboard/src/components/Layout.tsx
+++ b/dashboard/src/components/Layout.tsx
@@ -144,18 +144,24 @@ export function Layout({ onLogout, userRole }: LayoutProps) {
}, [isMobileOpen]);
// Escape dismisses the innermost overlay: while the language menu is open it takes the key, then the drawer does.
+ // The drawer covers the page, so it takes the key before the page's own Escape handlers (on window, in the capture
+ // phase) and keeps it from them: one press closed the drawer and the chat open behind it. A dialog renders above
+ // the drawer and keeps its own Escape, and so does a composition being committed by an IME.
useEffect(() => {
if (!isMobileOpen || isLanguageMenuOpen) return;
const closeOnEscape = (event: KeyboardEvent) => {
- if (event.key !== 'Escape') return;
+ if (event.key !== 'Escape' || event.isComposing) return;
+ if (document.querySelector('[role="dialog"]')) return;
+ event.preventDefault();
+ event.stopPropagation();
setIsMobileOpen(false);
// The focused link is about to slide off screen, so hand focus back to the control that opened the drawer.
mobileMenuButtonRef.current?.focus();
};
- document.addEventListener('keydown', closeOnEscape);
- return () => document.removeEventListener('keydown', closeOnEscape);
+ window.addEventListener('keydown', closeOnEscape, true);
+ return () => window.removeEventListener('keydown', closeOnEscape, true);
}, [isMobileOpen, isLanguageMenuOpen]);
useEffect(() => {