Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const useMessageListNavigation = (): { messageListRef: RefCallback<HTMLEl
const messageListRef = useCallback(
(node: HTMLElement | null) => {
let lastMessageFocused: HTMLElement | null = null;
let triggeredByKeyboard = false;

if (!node) {
return;
Expand Down Expand Up @@ -61,21 +62,22 @@ export const useMessageListNavigation = (): { messageListRef: RefCallback<HTMLEl

if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
if (e.key === 'ArrowUp') {
massageListFocusManager.focusPrevious({ wrap: true, accept: (node) => isListItem(node) });
massageListFocusManager.focusPrevious({ accept: (node) => isListItem(node) });
}

if (e.key === 'ArrowDown') {
massageListFocusManager.focusNext({ wrap: true, accept: (node) => isListItem(node) });
massageListFocusManager.focusNext({ accept: (node) => isListItem(node) });
}

lastMessageFocused = document.activeElement as HTMLElement;
triggeredByKeyboard = true;
}
});

node.addEventListener(
'blur',
(e) => {
if (!(e.currentTarget instanceof HTMLElement && e.relatedTarget instanceof HTMLElement)) {
if (!triggeredByKeyboard || !(e.currentTarget instanceof HTMLElement && e.relatedTarget instanceof HTMLElement)) {
return;
}

Expand All @@ -89,12 +91,14 @@ export const useMessageListNavigation = (): { messageListRef: RefCallback<HTMLEl
node.addEventListener(
'focus',
(e) => {
if (!(e.currentTarget instanceof HTMLElement && e.relatedTarget instanceof HTMLElement)) {
if (!triggeredByKeyboard || !(e.currentTarget instanceof HTMLElement && e.relatedTarget instanceof HTMLElement)) {
return;
}

if (lastMessageFocused && !e.currentTarget.contains(e.relatedTarget) && node.contains(e.target as HTMLElement)) {
lastMessageFocused?.focus();
lastMessageFocused = null;
triggeredByKeyboard = false;
}
},
{ capture: true },
Expand Down
25 changes: 18 additions & 7 deletions apps/meteor/tests/e2e/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ test.describe.serial('Messaging', () => {
await page.keyboard.press('Shift+Tab');
await expect(page.locator('[data-qa-type="message"]').last()).toBeFocused();

// move focus to the first typed message
// move focus to the first system message
await page.keyboard.press('ArrowUp');
await page.keyboard.press('ArrowUp');
await expect(page.locator('[data-qa="system-message"]').first()).toBeFocused();

// move focus to the first typed message
await page.keyboard.press('ArrowDown');
await expect(page.locator('[data-qa-type="message"]:has-text("msg1")')).toBeFocused();

// move focus to the favorite icon
Expand All @@ -48,17 +53,23 @@ test.describe.serial('Messaging', () => {
await page.keyboard.press('Tab');
await page.keyboard.press('Tab');
await expect(page.locator('[data-qa-type="message"]:has-text("msg1")').locator('[role=toolbar][aria-label="Message actions"]').getByRole('button', { name: 'Add reaction' })).toBeFocused();

// move focus to the first system message
await page.keyboard.press('Tab');
await page.keyboard.press('ArrowDown');
await expect(page.locator('[data-qa="system-message"]').first()).toBeFocused();


// move focus to the composer
await page.keyboard.press('Tab');
await page.keyboard.press('Tab');
await page.keyboard.press('Tab');
await expect(poHomeChannel.composer).toBeFocused();
});

test('should not restore focus on the last focused if it was triggered by click', async ({ page }) => {
await poHomeChannel.sidenav.openChat(targetChannel);
await page.locator('[data-qa-type="message"]:has-text("msg1")').click();
await poHomeChannel.composer.click();
await page.locator('[data-qa-type="message"]:has-text("msg2")').click();

await expect(page.locator('[data-qa-type="message"]:has-text("msg2")')).toBeFocused();
});

test('expect show "hello word" in both contexts (targetChannel)', async ({ browser }) => {
await poHomeChannel.sidenav.openChat(targetChannel);
const { page } = await createAuxContext(browser, Users.user2);
Expand Down