Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep keyboard selection when more topics load (#44768)

Penar Musaraj843c252399fb
diff --git a/frontend/discourse/app/components/topic-list/item.gjs b/frontend/discourse/app/components/topic-list/item.gjs
index e1a6c5e85a4..1c1747192b5 100644
--- a/frontend/discourse/app/components/topic-list/item.gjs
+++ b/frontend/discourse/app/components/topic-list/item.gjs
@@ -1,4 +1,5 @@
import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
import { concat } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
@@ -38,6 +39,8 @@ export default class Item extends Component {
@service site;
@service siteSettings;
+ @tracked isFocused = false;
+
highlightIfNeeded = modifier((element) => {
if (this.args.topic.id === this.historyStore.get("lastTopicIdViewed")) {
element.dataset.isLastViewedTopic = true;
@@ -159,6 +162,16 @@ export default class Item extends Component {
element.classList.add("highlighted");
}
+ @action
+ onBlur() {
+ this.isFocused = false;
+ }
+
+ @action
+ onFocus() {
+ this.isFocused = true;
+ }
+
@action
onTitleFocus(event) {
event.target.closest(".topic-list-item").classList.add("selected");
@@ -286,6 +299,7 @@ export default class Item extends Component {
aria-level={{this.ariaLevel}}
class={{dConcatClass
"topic-list-item"
+ (if this.isFocused "selected")
(if @topic.category (concat "category-" @topic.category.fullSlug))
(if (eq @topic @lastVisitedTopic) "last-visit")
(if @topic.visited "visited")
@@ -306,6 +320,8 @@ export default class Item extends Component {
role={{this.role}}
style={{this.style}}
{{this.highlightIfNeeded}}
+ {{on "focus" this.onFocus}}
+ {{on "blur" this.onBlur}}
{{on "keydown" this.keyDown}}
{{on "click" this.click}}
{{on "auxclick" this.click}}
diff --git a/frontend/discourse/tests/acceptance/keyboard-shortcuts-test.js b/frontend/discourse/tests/acceptance/keyboard-shortcuts-test.js
index a6481b3bd79..5d0187b6717 100644
--- a/frontend/discourse/tests/acceptance/keyboard-shortcuts-test.js
+++ b/frontend/discourse/tests/acceptance/keyboard-shortcuts-test.js
@@ -2,6 +2,8 @@ import {
click,
currentURL,
fillIn,
+ find,
+ settled,
triggerKeyEvent,
visit,
} from "@ember/test-helpers";
@@ -423,3 +425,51 @@ acceptance("Keyboard Shortcuts Help Modal - Search", function () {
assert.dom(".shortcut-category-esc_test tbody tr").exists({ count: 1 });
});
});
+
+acceptance("Keyboard Shortcuts - Topic pagination", function (needs) {
+ needs.user();
+ needs.pretender((server, helper) => {
+ server.get("/latest.json", (request) => {
+ const response = cloneJSON(DiscoveryFixtures["/latest.json"]);
+ const page = Number(request.queryParams.page || 0);
+ response.topic_list.topics = response.topic_list.topics.slice(
+ page * 3,
+ (page + 1) * 3
+ );
+ response.topic_list.more_topics_url = page ? null : "/latest?page=1";
+ return helper.response(response);
+ });
+ });
+
+ test("bulk select keeps keyboard selection when more topics load", async function (assert) {
+ await visit("/latest");
+ await click("button.bulk-select");
+ await triggerKeyEvent(document, "keypress", "J");
+ await triggerKeyEvent(document, "keypress", "J");
+
+ const selected = find(".topic-list-item.selected");
+ assert.dom(selected).isFocused("the keyboard-selected topic has focus");
+
+ await this.owner.lookup("service:session").topicList.loadMore();
+ await settled();
+
+ assert
+ .dom(".topic-list-item")
+ .exists({ count: 6 }, "the next page is appended");
+ assert.dom(selected).isFocused("focus stays on the same topic");
+ assert
+ .dom(selected)
+ .hasClass("selected", "the keyboard selection survives pagination");
+
+ await triggerKeyEvent(document, "keypress", "J");
+ assert
+ .dom(selected.nextElementSibling)
+ .isFocused("J moves to the next topic");
+ assert
+ .dom(selected)
+ .doesNotHaveClass("selected", "the previous selection is cleared");
+
+ await triggerKeyEvent(document, "keypress", "K");
+ assert.dom(selected).isFocused("K returns to the original topic");
+ });
+});