Skip to content

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

FIX: J/K post stream navigation inconsistencies (#16171)

Penar Musaraj0400d1090705
diff --git a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js
index 1df3d213cb2..ab11910b458 100644
--- a/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js
+++ b/app/assets/javascripts/discourse/app/lib/keyboard-shortcuts.js
@@ -661,8 +661,8 @@ export default {
.toArray()
.find((article) =>
direction > 0
- ? article.getBoundingClientRect().top > offset
- : article.getBoundingClientRect().bottom > offset
+ ? article.getBoundingClientRect().top >= offset
+ : article.getBoundingClientRect().bottom >= offset
);
if (!$selected) {
$selected = $articles[$articles.length - 1];
diff --git a/app/assets/javascripts/discourse/tests/acceptance/keyboard-shortcuts-test.js b/app/assets/javascripts/discourse/tests/acceptance/keyboard-shortcuts-test.js
index 1fa248cbc68..4056dabbfdf 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/keyboard-shortcuts-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/keyboard-shortcuts-test.js
@@ -48,6 +48,27 @@ acceptance("Keyboard Shortcuts - Anonymous Users", function (needs) {
await triggerKeyEvent(document, "keypress", "s".charCodeAt(0));
assert.strictEqual(currentURL(), "/t/keyboard-shortcuts-are-awesome/27331");
});
+
+ test("j/k navigation moves selection up/down", async function (assert) {
+ await visit("/t/this-is-a-test-topic/9");
+ await triggerKeyEvent(document, "keypress", "j".charCodeAt(0));
+ assert.ok(
+ exists(".post-stream .topic-post.selected #post_1"),
+ "first post is selected"
+ );
+
+ await triggerKeyEvent(document, "keypress", "j".charCodeAt(0));
+ assert.ok(
+ exists(".post-stream .topic-post.selected #post_2"),
+ "pressing j moves selection to next post"
+ );
+
+ await triggerKeyEvent(document, "keypress", "k".charCodeAt(0));
+ assert.ok(
+ exists(".post-stream .topic-post.selected #post_1"),
+ "pressing k moves selection to previous post"
+ );
+ });
});
acceptance("Keyboard Shortcuts - Authenticated Users", function (needs) {