Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep focus where it was after a fallback copy

Yudhi Armyndhariscee6199d5055
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 1ac5d2461..544d554d6 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -2864,6 +2864,16 @@ test('Copy chat ID works where navigator.clipboard does not exist', async t => {
assert.ok(!rtl.screen.queryByText('Could not copy to clipboard'));
});
+test('Copy chat ID leaves keyboard focus on the room menu button when the fallback copy runs', async t => {
+ // A browser puts focus in the textarea that select() acts on; jsdom does not.
+ t.mock.method(window.HTMLTextAreaElement.prototype, 'select', function (this: HTMLTextAreaElement) {
+ this.focus();
+ });
+ await copyChatIdWith(t, true);
+ await rtl.screen.findByText('Chat ID copied');
+ assert.equal(focusedName(), 'More actions', 'focus fell to the page');
+});
+
test('Copy chat ID says so when the copy did not happen', async t => {
const copy = await copyChatIdWith(t, false);
await rtl.screen.findByText('Could not copy to clipboard');
diff --git a/dashboard/src/utils/clipboard.ts b/dashboard/src/utils/clipboard.ts
index 40757cc5d..969804a7b 100644
--- a/dashboard/src/utils/clipboard.ts
+++ b/dashboard/src/utils/clipboard.ts
@@ -14,6 +14,8 @@ export async function copyToClipboard(text: string): Promise<boolean> {
// Fall through to the legacy path (e.g. NotAllowedError outside a user gesture).
}
}
+ // Selecting the textarea takes focus, and removing it leaves focus on the page: give it back to where it was.
+ const focused = document.activeElement;
try {
const ta = document.createElement('textarea');
ta.value = text;
@@ -24,6 +26,7 @@ export async function copyToClipboard(text: string): Promise<boolean> {
ta.select();
const ok = document.execCommand('copy');
document.body.removeChild(ta);
+ if (focused instanceof HTMLElement) focused.focus();
return ok;
} catch {
return false;