Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): return focus to the chat menu button after a pick
Yudhi Armyndharis8599ca5d699d
diff --git a/dashboard/src/components/chats/ChatRowMenu.tsx b/dashboard/src/components/chats/ChatRowMenu.tsxindex 9c9d31157..d8d4f782d 100644--- a/dashboard/src/components/chats/ChatRowMenu.tsx+++ b/dashboard/src/components/chats/ChatRowMenu.tsx@@ -147,7 +147,12 @@ function ChatRowMenu({ chat, actions }: { chat: Chat; actions: ChatRowActions })chat={chat}items={items}groupClassName="chat-row-menu-group"- onPicked={() => setOpen(false)}+ onPicked={() => {+ // Focus moves to the chevron, which stays: the picked entry is about to unmount, and a dialog it+ // opens restores focus to whatever holds it then.+ setOpen(false);+ triggerRef.current?.focus();+ }}/></div>)}diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.tsindex a98963e36..48651ba72 100644--- a/dashboard/src/pages/Chats.features.test.ts+++ b/dashboard/src/pages/Chats.features.test.ts@@ -649,6 +649,54 @@ test("a row's menu sits fixed above the list, and scrolling the list closes it",assert.equal(container.querySelector('.chat-row-menu-popover'), null, 'the menu stayed open, adrift from its row');});+/** The accessible name of the element that has focus: DOM nodes are compared by name, as a failed deepEqual on them hangs. */+const focusedName = () => document.activeElement?.getAttribute('aria-label') ?? document.activeElement?.tagName;++test("dismissing a dialog a row's menu opened returns focus to the row's menu button", async () => {+ const { screen, fireEvent, waitFor } = rtl;+ renderPage();+ await screen.findByText('Alice');+ const trigger = screen.getByRole('button', { name: 'Actions for Alice' });+ trigger.focus();+ fireEvent.click(trigger);+ const item = screen.getByRole('button', { name: 'Block contact' });+ item.focus();+ fireEvent.click(item);+ await screen.findByRole('dialog');++ fireEvent.keyDown(document, { key: 'Escape' });+ await waitFor(() => assert.ok(!screen.queryByRole('dialog')));+ assert.equal(focusedName(), 'Actions for Alice', 'focus fell to the page');+});++test("dismissing a dialog the room's menu opened, or the menu itself, returns focus to its button", async () => {+ const { screen, fireEvent, waitFor, within } = rtl;+ const { container } = renderPage();+ fireEvent.click(await screen.findByText('Alice'));+ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the room did not open'));+ const trigger = container.querySelector('.room-menu-trigger') as HTMLButtonElement;++ trigger.focus();+ fireEvent.click(trigger);+ const item = within(container.querySelector('.room-menu-popover') as HTMLElement).getByRole('button', {+ name: 'Block contact',+ });+ item.focus();+ fireEvent.click(item);+ await screen.findByRole('dialog');+ fireEvent.keyDown(document, { key: 'Escape' });+ await waitFor(() => assert.ok(!screen.queryByRole('dialog')));+ assert.equal(focusedName(), 'More actions', 'focus fell to the page after the dialog');++ fireEvent.click(trigger);+ within(container.querySelector('.room-menu-popover') as HTMLElement)+ .getByRole('button', { name: 'Copy chat ID' })+ .focus();+ fireEvent.keyDown(document, { key: 'Escape' });+ await waitFor(() => assert.ok(!container.querySelector('.room-menu-popover')));+ assert.equal(focusedName(), 'More actions', 'focus fell to the page after Escape');+});+test('archiving a chat moves it into the archive view, and unarchiving brings it back', async () => {const { screen, fireEvent, waitFor } = rtl;renderPage();diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 06ca0fbf3..27e1113ba 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -195,6 +195,7 @@ export function Chats() {const [showNewChat, setShowNewChat] = useState<boolean>(false);const [roomMenuOpen, setRoomMenuOpen] = useState<boolean>(false);const roomMenuRef = useRef<HTMLDivElement | null>(null);+ const roomMenuTriggerRef = useRef<HTMLButtonElement | null>(null);// The group whose info dialog is open, from its row's menu or from the open room's.const [groupDialogChat, setGroupDialogChat] = useState<Chat | null>(null);// A destructive row action awaiting confirmation, and the one whose request is out. A dialog is busy only@@ -1384,9 +1385,15 @@ export function Chats() {}};+ // Closes the room menu from inside it, leaving focus on its button instead of the entry that unmounts.+ const closeRoomMenu = () => {+ setRoomMenuOpen(false);+ roomMenuTriggerRef.current?.focus();+ };+const handleCopyChatId = async () => {if (!activeChat) return;- setRoomMenuOpen(false);+ closeRoomMenu();try {await navigator.clipboard.writeText(activeChat.id);showSuccessToast(t('chats.chatIdCopied'));@@ -1409,7 +1416,7 @@ export function Chats() {const closeOnEscape = (event: KeyboardEvent) => {if (event.key !== 'Escape' || event.isComposing || event.defaultPrevented) return;event.preventDefault();- setRoomMenuOpen(false);+ closeRoomMenu();};document.addEventListener('mousedown', closeOnOutsidePress);document.addEventListener('keydown', closeOnEscape, true);@@ -1609,6 +1616,7 @@ export function Chats() {<div className="room-header-actions"><div className="room-menu" ref={roomMenuRef}><button+ ref={roomMenuTriggerRef}type="button"className={`icon-btn room-menu-trigger ${roomMenuOpen ? 'active' : ''}`}onClick={() => setRoomMenuOpen(open => !open)}@@ -1623,7 +1631,7 @@ export function Chats() {{roomMenuOpen && (<div className="room-menu-popover">{/* The row's actions, then what only an open room needs. */}- <ChatActions chat={activeChat} actions={rowActions} onPicked={() => setRoomMenuOpen(false)} />+ <ChatActions chat={activeChat} actions={rowActions} onPicked={closeRoomMenu} /><hr /><button type="button" onClick={() => void handleCopyChatId()}><Copy size={16} />