diff --git a/.changeset/focus-popup-consent-menu.md b/.changeset/focus-popup-consent-menu.md new file mode 100644 index 00000000..c7d5ef46 --- /dev/null +++ b/.changeset/focus-popup-consent-menu.md @@ -0,0 +1,5 @@ +--- +"@chatcops/widget": patch +--- + +Keep focus inside the popup when it opens on the consent notice and after choosing an item from the header menu. diff --git a/e2e/tests/states.spec.ts b/e2e/tests/states.spec.ts index 0d62b6d7..01caf5e7 100644 --- a/e2e/tests/states.spec.ts +++ b/e2e/tests/states.spec.ts @@ -271,9 +271,11 @@ for (const look of looks) { await openChat(page); await expect(page.locator('.cc-consent')).toBeVisible(); await expect(page.locator('.cc-consent-btn')).toBeVisible(); - await page.locator('.cc-consent-btn').focus(); - await expect(page.locator('.cc-consent-btn')).toBeFocused(); + await expect(page.locator('.cc-consent')).toBeFocused(); await expectAccessible(page); + + await page.keyboard.press('Escape'); + await expect(page.locator(panel)).not.toHaveClass(/cc-visible/); }); test('header menu with keyboard navigation and New conversation', async ({ page }) => { @@ -297,6 +299,7 @@ for (const look of looks) { await page.keyboard.press('Enter'); await expect(menu).not.toBeVisible(); await expect(moreBtn).toHaveAttribute('aria-expanded', 'false'); + await expect(moreBtn).toBeFocused(); await expect(page.locator('.cc-message-user')).toHaveCount(0); }); diff --git a/packages/widget/src/dom/menu.ts b/packages/widget/src/dom/menu.ts index 6eb8a10e..a3aa5929 100644 --- a/packages/widget/src/dom/menu.ts +++ b/packages/widget/src/dom/menu.ts @@ -57,6 +57,7 @@ export class HeaderMenu { const item = (e.target as HTMLElement).closest('[role="menuitem"]'); if (!item) return; this.close(); + this.btn.focus(); const action = item.dataset.action; if (action === 'new') this.options.onNewConversation?.(); else if (action === 'download') this.options.onDownloadTranscript?.(); diff --git a/packages/widget/src/widget.ts b/packages/widget/src/widget.ts index 7cba3434..017f5c72 100644 --- a/packages/widget/src/widget.ts +++ b/packages/widget/src/widget.ts @@ -378,6 +378,7 @@ export class Widget { this.panel.setBodyVisible(false); const notice = document.createElement('div'); notice.className = 'cc-consent'; + notice.setAttribute('tabindex', '-1'); notice.textContent = this.locale.consentNotice; const { onConsentRequest } = this.config; if (onConsentRequest) { @@ -426,6 +427,7 @@ export class Widget { this.hydrate(); this.panel.show(); if (!this.isInline) { + if (this.consentEl?.isConnected) this.consentEl.focus(); this.fab?.setOpen(true); this.bubble?.hide(); } diff --git a/packages/widget/tests/consent.test.ts b/packages/widget/tests/consent.test.ts index 8b52ef92..09c103ce 100644 --- a/packages/widget/tests/consent.test.ts +++ b/packages/widget/tests/consent.test.ts @@ -100,6 +100,23 @@ describe('waiting for consent', () => { expect(noticeOf(w)?.textContent).toContain('Chat is available once you accept functional cookies.'); }); + it('moves focus to the notice when the popup opens', () => { + const w = start({ onConsentRequest: () => undefined }); + w.open(); + + expect(shadowOf(w).activeElement).toBe(noticeOf(w)); + }); + + it('does not take focus when an inline chat shows the notice', () => { + const box = document.createElement('div'); + box.id = 'box'; + document.body.appendChild(box); + const w = start({ mode: 'inline', container: '#box' }); + + expect(noticeOf(w)).not.toBeNull(); + expect(shadowOf(w).activeElement).not.toBe(noticeOf(w)); + }); + it('does not show the welcome bubble', async () => { vi.useFakeTimers(); const calls = spyOnStorage(); diff --git a/packages/widget/tests/dom/menu.test.ts b/packages/widget/tests/dom/menu.test.ts index a06866d7..3f88d188 100644 --- a/packages/widget/tests/dom/menu.test.ts +++ b/packages/widget/tests/dom/menu.test.ts @@ -82,11 +82,13 @@ describe('HeaderMenu', () => { menu.el.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); expect(onNewConversation).toHaveBeenCalledOnce(); expect(menu.isOpen).toBe(false); + expect(root.activeElement).toBe(moreBtn); menu.open(); menu.el.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown', bubbles: true })); menu.el.dispatchEvent(new KeyboardEvent('keydown', { key: ' ', bubbles: true })); expect(onDownloadTranscript).toHaveBeenCalledOnce(); + expect(root.activeElement).toBe(moreBtn); menu.open(); const event = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true }); @@ -95,6 +97,25 @@ describe('HeaderMenu', () => { expect(root.activeElement).toBe(moreBtn); }); + it('returns focus to the More options button after a click', () => { + const onNewConversation = vi.fn(); + const menu = new HeaderMenu(moreBtn, { + moreLabel: 'More options', + newConversationLabel: 'New conversation', + downloadTranscriptLabel: 'Download transcript', + privacyLabel: 'Privacy', + onNewConversation, + }); + root.appendChild(menu.el); + menu.open(); + + menu.items[0].click(); + + expect(onNewConversation).toHaveBeenCalledOnce(); + expect(menu.isOpen).toBe(false); + expect(root.activeElement).toBe(moreBtn); + }); + it('only shows Privacy link with valid http/https URLs and never for javascript: URLs', () => { expect(safePrivacyUrl('https://example.com/privacy')).toBe('https://example.com/privacy'); expect(safePrivacyUrl('http://example.com/privacy')).toBe('http://example.com/privacy');