Skip to content

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

UX: open post reactions menu with initial filter (#39656)

David Battersby270e989802a5
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-counter.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-counter.gjs
index 6d921db4269..7b31d34cfd5 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-counter.gjs
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-counter.gjs
@@ -34,6 +34,13 @@ export default class DiscourseReactionsCounter extends Component {
}`;
}
+ get hasOpenMenuForThisPost() {
+ const menu = this.menu.getByIdentifier(MENU_IDENTIFIER);
+ return (
+ !!menu?.expanded && menu.options.data?.post?.id === this.args.post.id
+ );
+ }
+
reactionsChanged(data) {
uniqueItemsFromArray(data.reactions).forEach((reaction) => {
this.getUsers(reaction);
@@ -58,6 +65,17 @@ export default class DiscourseReactionsCounter extends Component {
event.stopImmediatePropagation();
}
+ @action
+ pointerDown(event) {
+ if (!this.useNewMenu) {
+ return;
+ }
+
+ if (this.hasOpenMenuForThisPost) {
+ event.stopPropagation();
+ }
+ }
+
@action
mouseUp(event) {
event.stopImmediatePropagation();
@@ -96,7 +114,17 @@ export default class DiscourseReactionsCounter extends Component {
event.stopPropagation();
event.preventDefault();
- this.#toggleMenu(event.currentTarget);
+ const reactionEl = event.target.closest(
+ ".discourse-reactions-list-emoji[data-reaction-id]"
+ );
+ const reactionId = reactionEl?.dataset.reactionId;
+
+ if (this.hasOpenMenuForThisPost) {
+ this.#switchMenuFilter(reactionId ?? "all");
+ return;
+ }
+
+ this.#toggleMenu(event.currentTarget, reactionId);
return;
}
@@ -222,7 +250,15 @@ export default class DiscourseReactionsCounter extends Component {
);
}
- #toggleMenu(trigger) {
+ #switchMenuFilter(filter) {
+ document
+ .querySelector(
+ `[data-identifier="${MENU_IDENTIFIER}"] [data-reaction-filter="${filter}"]`
+ )
+ ?.click();
+ }
+
+ #toggleMenu(trigger, initialFilter = null) {
const virtualElement = {
getBoundingClientRect: () => trigger.getBoundingClientRect(),
};
@@ -235,7 +271,7 @@ export default class DiscourseReactionsCounter extends Component {
arrow: true,
placement: "bottom",
offset: 15,
- data: { post: this.args.post },
+ data: { post: this.args.post, initialFilter },
});
}
@@ -250,6 +286,7 @@ export default class DiscourseReactionsCounter extends Component {
aria-label={{this.counterAriaLabel}}
{{on "mousedown" this.mouseDown}}
{{on "mouseup" this.mouseUp}}
+ {{on "pointerdown" this.pointerDown}}
{{on "click" this.click}}
{{on "keydown" this.keyDown}}
>
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-list-emoji.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-list-emoji.gjs
index db1404d9b54..be7b8003bb9 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-list-emoji.gjs
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-list-emoji.gjs
@@ -102,7 +102,11 @@ export default class DiscourseReactionsListEmoji extends Component {
<template>
{{#if this.useNewMenu}}
- <div class="discourse-reactions-list-emoji" id={{this.elementId}}>
+ <div
+ class="discourse-reactions-list-emoji"
+ id={{this.elementId}}
+ data-reaction-id={{@reaction.id}}
+ >
{{#if @reaction.count}}
{{emoji
@reaction.id
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-users-menu.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-users-menu.gjs
index b5d8bd023a0..9f9a9bd8f5b 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-users-menu.gjs
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-users-menu.gjs
@@ -12,7 +12,7 @@ import { i18n } from "discourse-i18n";
import CustomReaction from "../models/discourse-reactions-custom-reaction";
export default class DiscourseReactionsUsersMenu extends Component {
- @tracked activeFilter = null;
+ @tracked activeFilter = this.args.data.initialFilter ?? null;
fetchUsers = async (page, pageSize) => {
const filter = this.activeFilter;
diff --git a/plugins/discourse-reactions/assets/stylesheets/common/discourse-reactions.scss b/plugins/discourse-reactions/assets/stylesheets/common/discourse-reactions.scss
index 286ff660708..fd4e2502897 100644
--- a/plugins/discourse-reactions/assets/stylesheets/common/discourse-reactions.scss
+++ b/plugins/discourse-reactions/assets/stylesheets/common/discourse-reactions.scss
@@ -19,6 +19,7 @@ html.discourse-reactions-no-select {
display: flex;
align-items: center;
justify-content: center;
+ padding: 0.2em 0;
&:nth-of-type(1n + 4) {
display: none;
diff --git a/plugins/discourse-reactions/spec/system/page_objects/post_reactions_list.rb b/plugins/discourse-reactions/spec/system/page_objects/post_reactions_list.rb
index c39117d9e94..bf86265724f 100644
--- a/plugins/discourse-reactions/spec/system/page_objects/post_reactions_list.rb
+++ b/plugins/discourse-reactions/spec/system/page_objects/post_reactions_list.rb
@@ -41,6 +41,14 @@ module PageObjects
usernames.join("\n"),
)
end
+
+ def click_counter
+ context_component.find(".discourse-reactions-counter").click
+ end
+
+ def click_counter_number
+ context_component.find(".discourse-reactions-counter .reactions-counter").click
+ end
end
end
end
diff --git a/plugins/discourse-reactions/spec/system/page_objects/post_reactions_popup.rb b/plugins/discourse-reactions/spec/system/page_objects/post_reactions_popup.rb
new file mode 100644
index 00000000000..f1dfb1a62b0
--- /dev/null
+++ b/plugins/discourse-reactions/spec/system/page_objects/post_reactions_popup.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+module PageObjects
+ module Components
+ class PostReactionsPopup < PageObjects::Components::Base
+ SELECTOR = ".post-users-popup"
+
+ def open?
+ page.has_css?(SELECTOR)
+ end
+
+ def closed?
+ page.has_no_css?(SELECTOR)
+ end
+
+ def click_filter(reaction)
+ find("#{SELECTOR} [data-reaction-filter=#{reaction}]").click
+ end
+
+ def has_active_filter?(reaction)
+ page.has_css?(
+ "#{SELECTOR} .post-users-popup__filter[data-reaction-filter=#{reaction}].is-active",
+ )
+ end
+
+ def has_no_active_filter?(reaction)
+ page.has_no_css?(
+ "#{SELECTOR} .post-users-popup__filter[data-reaction-filter=#{reaction}].is-active",
+ )
+ end
+
+ def has_user?(username)
+ page.has_css?("#{SELECTOR} .post-users-popup__name[data-user-card=#{username}]")
+ end
+
+ def has_no_user?(username)
+ page.has_no_css?("#{SELECTOR} .post-users-popup__name[data-user-card=#{username}]")
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-reactions/spec/system/reactions_post_list_spec.rb b/plugins/discourse-reactions/spec/system/reactions_post_list_spec.rb
index a2299c65005..9e77760cfcf 100644
--- a/plugins/discourse-reactions/spec/system/reactions_post_list_spec.rb
+++ b/plugins/discourse-reactions/spec/system/reactions_post_list_spec.rb
@@ -9,6 +9,7 @@ describe "Reactions | Post reaction user list" do
let(:reactions_list) do
PageObjects::Components::PostReactionsList.new("#post_#{post.post_number}")
end
+ let(:popup) { PageObjects::Components::PostReactionsPopup.new }
before do
SiteSetting.discourse_reactions_enabled = true
@@ -31,9 +32,9 @@ describe "Reactions | Post reaction user list" do
visit(post.url)
expect(reactions_list).to have_reaction("heart")
- find(".discourse-reactions-counter").click
+ reactions_list.click_counter
- expect(page).to have_css(".post-users-popup")
+ expect(popup).to be_open
find(".post-users-popup .post-users-popup__name[data-user-card=#{user_2.username}]").click
expect(page).to have_css(".user-card.user-card-#{user_2.username}")
@@ -46,7 +47,7 @@ describe "Reactions | Post reaction user list" do
visit(post.url)
expect(reactions_list).to have_reaction("heart")
- find(".discourse-reactions-counter").click
+ reactions_list.click_counter
expect(page).to have_css(
".post-users-popup__name[data-user-card=#{user_2.username}]",
@@ -62,7 +63,7 @@ describe "Reactions | Post reaction user list" do
visit(post.url)
expect(reactions_list).to have_reaction("heart")
- find(".discourse-reactions-counter").click
+ reactions_list.click_counter
expect(page).to have_css(
".post-users-popup__name[data-user-card=#{user_2.username}]",
@@ -71,29 +72,78 @@ describe "Reactions | Post reaction user list" do
expect(page).to have_no_css(".post-users-popup__username")
end
+ it "opens the users popup pre-filtered when clicking a specific reaction emoji" do
+ sign_in(current_user)
+ visit(post.url)
+ expect(reactions_list).to have_reaction("heart")
+
+ reactions_list.click_reaction("clap")
+
+ expect(popup).to be_open
+ expect(popup).to have_active_filter("clap")
+ expect(popup).to have_no_active_filter("heart")
+ expect(popup).to have_user(user_3.username)
+ expect(popup).to have_no_user(user_2.username)
+ end
+
+ it "opens the users popup with no filter when clicking the counter number" do
+ sign_in(current_user)
+ visit(post.url)
+ expect(reactions_list).to have_reaction("heart")
+
+ reactions_list.click_counter_number
+
+ expect(popup).to be_open
+ expect(popup).to have_active_filter("all")
+ end
+
+ it "switches the active filter without reopening the menu when another emoji is clicked" do
+ sign_in(current_user)
+ visit(post.url)
+ expect(reactions_list).to have_reaction("heart")
+
+ reactions_list.click_reaction("heart")
+ expect(popup).to have_active_filter("heart")
+
+ reactions_list.click_reaction("clap")
+ expect(popup).to have_active_filter("clap")
+ expect(popup).to have_no_active_filter("heart")
+ end
+
+ it "switches to the all filter without reopening the menu when the counter number is clicked" do
+ sign_in(current_user)
+ visit(post.url)
+ expect(reactions_list).to have_reaction("heart")
+
+ reactions_list.click_reaction("heart")
+ expect(popup).to have_active_filter("heart")
+
+ reactions_list.click_counter_number
+ expect(popup).to have_active_filter("all")
+ expect(popup).to have_no_active_filter("heart")
+ end
+
it "filters the users popup by reaction" do
sign_in(current_user)
visit(post.url)
expect(reactions_list).to have_reaction("heart")
- find(".discourse-reactions-counter").click
- expect(page).to have_css(".post-users-popup")
+ reactions_list.click_counter_number
+ expect(popup).to be_open
- within(".post-users-popup") do
- expect(page).to have_css(".post-users-popup__name[data-user-card=#{user_2.username}]")
- expect(page).to have_css(".post-users-popup__name[data-user-card=#{user_3.username}]")
+ expect(popup).to have_user(user_2.username)
+ expect(popup).to have_user(user_3.username)
- find("[data-reaction-filter=heart]").click
- expect(page).to have_css(".post-users-popup__name[data-user-card=#{user_2.username}]")
- expect(page).to have_no_css(".post-users-popup__name[data-user-card=#{user_3.username}]")
+ popup.click_filter("heart")
+ expect(popup).to have_user(user_2.username)
+ expect(popup).to have_no_user(user_3.username)
- find("[data-reaction-filter=clap]").click
- expect(page).to have_css(".post-users-popup__name[data-user-card=#{user_3.username}]")
- expect(page).to have_no_css(".post-users-popup__name[data-user-card=#{user_2.username}]")
+ popup.click_filter("clap")
+ expect(popup).to have_user(user_3.username)
+ expect(popup).to have_no_user(user_2.username)
- find("[data-reaction-filter=all]").click
- expect(page).to have_css(".post-users-popup__name[data-user-card=#{user_2.username}]")
- expect(page).to have_css(".post-users-popup__name[data-user-card=#{user_3.username}]")
- end
+ popup.click_filter("all")
+ expect(popup).to have_user(user_2.username)
+ expect(popup).to have_user(user_3.username)
end
end