Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/focus-popup-consent-menu.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 5 additions & 2 deletions e2e/tests/states.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand All @@ -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);
});
Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/dom/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class HeaderMenu {
const item = (e.target as HTMLElement).closest<HTMLElement>('[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?.();
Expand Down
2 changes: 2 additions & 0 deletions packages/widget/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down
17 changes: 17 additions & 0 deletions packages/widget/tests/consent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions packages/widget/tests/dom/menu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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');
Expand Down
Loading