Skip to content

Commit 848d133

Browse files
dbkrHalf-Shot
authored andcommitted
Make landmark navigation work with new room list (#30747)
* Make landmark navigation work with new room list Split out from #30640 * Fix landmark selection to work with either room list * Add test for landmark navigation * Add test * Fix test * Clear mocks between runs
1 parent 82f1b77 commit 848d133

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

src/accessibility/LandmarkNavigation.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { TimelineRenderingType } from "../contexts/RoomContext";
1010
import { Action } from "../dispatcher/actions";
1111
import defaultDispatcher from "../dispatcher/dispatcher";
12+
import SettingsStore from "../settings/SettingsStore";
1213

1314
export const enum Landmark {
1415
// This is the space/home button in the left panel.
@@ -72,10 +73,16 @@ export class LandmarkNavigation {
7273
const landmarkToDomElementMap: Record<Landmark, () => HTMLElement | null | undefined> = {
7374
[Landmark.ACTIVE_SPACE_BUTTON]: () => document.querySelector<HTMLElement>(".mx_SpaceButton_active"),
7475

75-
[Landmark.ROOM_SEARCH]: () => document.querySelector<HTMLElement>(".mx_RoomSearch"),
76+
[Landmark.ROOM_SEARCH]: () =>
77+
SettingsStore.getValue("feature_new_room_list")
78+
? document.querySelector<HTMLElement>(".mx_RoomListSearch_search")
79+
: document.querySelector<HTMLElement>(".mx_RoomSearch"),
7680
[Landmark.ROOM_LIST]: () =>
77-
document.querySelector<HTMLElement>(".mx_RoomTile_selected") ||
78-
document.querySelector<HTMLElement>(".mx_RoomTile"),
81+
SettingsStore.getValue("feature_new_room_list")
82+
? document.querySelector<HTMLElement>(".mx_RoomListItemView_selected") ||
83+
document.querySelector<HTMLElement>(".mx_RoomListItemView")
84+
: document.querySelector<HTMLElement>(".mx_RoomTile_selected") ||
85+
document.querySelector<HTMLElement>(".mx_RoomTile"),
7986

8087
[Landmark.MESSAGE_COMPOSER_OR_HOME]: () => {
8188
const isComposerOpen = !!document.querySelector(".mx_MessageComposer");

src/components/views/rooms/RoomListPanel/RoomListPanel.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
55
Please see LICENSE files in the repository root for full details.
66
*/
77

8-
import React from "react";
8+
import React, { useState, useCallback } from "react";
99

1010
import { shouldShowComponent } from "../../../../customisations/helpers/UIComponents";
1111
import { UIComponent } from "../../../../settings/UIFeature";
@@ -14,6 +14,10 @@ import { RoomListHeaderView } from "./RoomListHeaderView";
1414
import { RoomListView } from "./RoomListView";
1515
import { Flex } from "../../../../shared-components/utils/Flex";
1616
import { _t } from "../../../../languageHandler";
17+
import { getKeyBindingsManager } from "../../../../KeyBindingsManager";
18+
import { KeyBindingAction } from "../../../../accessibility/KeyboardShortcuts";
19+
import { Landmark, LandmarkNavigation } from "../../../../accessibility/LandmarkNavigation";
20+
import { type IState as IRovingTabIndexState } from "../../../../accessibility/RovingTabIndex";
1721

1822
type RoomListPanelProps = {
1923
/**
@@ -28,6 +32,31 @@ type RoomListPanelProps = {
2832
*/
2933
export const RoomListPanel: React.FC<RoomListPanelProps> = ({ activeSpace }) => {
3034
const displayRoomSearch = shouldShowComponent(UIComponent.FilterContainer);
35+
const [focusedElement, setFocusedElement] = useState<Element | null>(null);
36+
37+
const onFocus = useCallback((ev: React.FocusEvent): void => {
38+
setFocusedElement(ev.target as Element);
39+
}, []);
40+
41+
const onBlur = useCallback((): void => {
42+
setFocusedElement(null);
43+
}, []);
44+
45+
const onKeyDown = useCallback(
46+
(ev: React.KeyboardEvent, state?: IRovingTabIndexState): void => {
47+
if (!focusedElement) return;
48+
const navAction = getKeyBindingsManager().getNavigationAction(ev);
49+
if (navAction === KeyBindingAction.PreviousLandmark || navAction === KeyBindingAction.NextLandmark) {
50+
ev.stopPropagation();
51+
ev.preventDefault();
52+
LandmarkNavigation.findAndFocusNextLandmark(
53+
Landmark.ROOM_SEARCH,
54+
navAction === KeyBindingAction.PreviousLandmark,
55+
);
56+
}
57+
},
58+
[focusedElement],
59+
);
3160

3261
return (
3362
<Flex
@@ -36,6 +65,9 @@ export const RoomListPanel: React.FC<RoomListPanelProps> = ({ activeSpace }) =>
3665
direction="column"
3766
align="stretch"
3867
aria-label={_t("room_list|list_title")}
68+
onFocus={onFocus}
69+
onBlur={onBlur}
70+
onKeyDown={onKeyDown}
3971
>
4072
{displayRoomSearch && <RoomListSearch activeSpace={activeSpace} />}
4173
<RoomListHeaderView />

test/unit-tests/components/views/rooms/RoomListPanel/RoomListPanel-test.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,39 @@
88
import React from "react";
99
import { render, screen } from "jest-matrix-react";
1010
import { mocked } from "jest-mock";
11+
import userEvent from "@testing-library/user-event";
1112

1213
import { RoomListPanel } from "../../../../../../src/components/views/rooms/RoomListPanel";
1314
import { shouldShowComponent } from "../../../../../../src/customisations/helpers/UIComponents";
1415
import { MetaSpace } from "../../../../../../src/stores/spaces";
16+
import { LandmarkNavigation } from "../../../../../../src/accessibility/LandmarkNavigation";
17+
import { ReleaseAnnouncementStore } from "../../../../../../src/stores/ReleaseAnnouncementStore";
1518

1619
jest.mock("../../../../../../src/customisations/helpers/UIComponents", () => ({
1720
shouldShowComponent: jest.fn(),
1821
}));
1922

23+
jest.mock("../../../../../../src/accessibility/LandmarkNavigation", () => ({
24+
LandmarkNavigation: {
25+
findAndFocusNextLandmark: jest.fn(),
26+
},
27+
Landmark: {
28+
ROOM_SEARCH: "something",
29+
},
30+
}));
31+
32+
// mock out release announcements as they interfere with what's focused
33+
// (this can be removed once the new room list announcement is gone)
34+
jest.spyOn(ReleaseAnnouncementStore.instance, "getReleaseAnnouncement").mockReturnValue(null);
35+
2036
describe("<RoomListPanel />", () => {
2137
function renderComponent() {
2238
return render(<RoomListPanel activeSpace={MetaSpace.Home} />);
2339
}
2440

2541
beforeEach(() => {
42+
jest.clearAllMocks();
43+
2644
// By default, we consider shouldShowComponent(UIComponent.FilterContainer) should return true
2745
mocked(shouldShowComponent).mockReturnValue(true);
2846
});
@@ -37,4 +55,38 @@ describe("<RoomListPanel />", () => {
3755
renderComponent();
3856
expect(screen.queryByRole("button", { name: "Search Ctrl K" })).toBeNull();
3957
});
58+
59+
it("should move to the next landmark when the shortcut key is pressed", async () => {
60+
renderComponent();
61+
62+
const userEv = userEvent.setup();
63+
64+
// Pick something arbitrary and focusable in the room list component and focus it
65+
const exploreRooms = screen.getByRole("button", { name: "Explore rooms" });
66+
exploreRooms.focus();
67+
expect(exploreRooms).toHaveFocus();
68+
69+
screen.getByRole("navigation", { name: "Room list" }).focus();
70+
await userEv.keyboard("{Control>}{F6}{/Control}");
71+
72+
expect(LandmarkNavigation.findAndFocusNextLandmark).toHaveBeenCalled();
73+
});
74+
75+
it("should not move to the next landmark if room list loses focus", async () => {
76+
renderComponent();
77+
78+
const userEv = userEvent.setup();
79+
80+
// Pick something arbitrary and focusable in the room list component and focus it
81+
const exploreRooms = screen.getByRole("button", { name: "Explore rooms" });
82+
exploreRooms.focus();
83+
expect(exploreRooms).toHaveFocus();
84+
85+
exploreRooms.blur();
86+
expect(exploreRooms).not.toHaveFocus();
87+
88+
await userEv.keyboard("{Control>}{F6}{/Control}");
89+
90+
expect(LandmarkNavigation.findAndFocusNextLandmark).not.toHaveBeenCalled();
91+
});
4092
});

0 commit comments

Comments
 (0)