Skip to content

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

FEATURE: mark topic as solved confetti (#37403)

David Battersby93eec3c454bf
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/connectors/topic-navigation/no-answer.gjs b/plugins/discourse-solved/assets/javascripts/discourse/connectors/topic-navigation/no-answer.gjs
index bd8dffcddf7..64a2e175b39 100644
--- a/plugins/discourse-solved/assets/javascripts/discourse/connectors/topic-navigation/no-answer.gjs
+++ b/plugins/discourse-solved/assets/javascripts/discourse/connectors/topic-navigation/no-answer.gjs
@@ -9,6 +9,7 @@ import { i18n } from "discourse-i18n";
const ONE_WEEK = 7 * 24 * 60 * 60 * 1000; // milliseconds
const MAX_DURATION_WITH_NO_ANSWER = ONE_WEEK;
const DISPLAY_DELAY = isTesting() ? 0 : 2000;
+const CONFETTI_PARTICLE_COUNT = 50;
@tagName("div")
@classNames("topic-navigation-outlet", "no-answer")
@@ -19,10 +20,10 @@ export default class NoAnswer extends Component {
init() {
super.init(...arguments);
- this.set("show", false);
this.setProperties({
oneWeek: ONE_WEEK,
show: false,
+ showConfetti: false,
});
this.appEvents.on(
@@ -68,10 +69,29 @@ export default class NoAnswer extends Component {
}
hidePopup() {
+ if (!this.show) {
+ return;
+ }
+
this.set("show", false);
+
+ if (!window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
+ this.set("showConfetti", true);
+ }
+ }
+
+ get confettiParticles() {
+ return Array.from({ length: CONFETTI_PARTICLE_COUNT }, (_, i) => i);
}
<template>
+ {{#if this.showConfetti}}
+ <div class="solved-confetti">
+ {{#each this.confettiParticles}}
+ <div class="solved-confetti-particle"></div>
+ {{/each}}
+ </div>
+ {{/if}}
{{#if this.show}}
<TopicNavigationPopup
@popupId="solved-notice"
diff --git a/plugins/discourse-solved/assets/stylesheets/solutions.scss b/plugins/discourse-solved/assets/stylesheets/solutions.scss
index f5b7443f50a..5990daf78a6 100644
--- a/plugins/discourse-solved/assets/stylesheets/solutions.scss
+++ b/plugins/discourse-solved/assets/stylesheets/solutions.scss
@@ -1,3 +1,5 @@
+@use "sass:math";
+
$solved-color: var(--success);
.select-kit {
@@ -137,3 +139,50 @@ aside.quote.accepted-answer[data-expanded="false"] {
}
}
}
+
+.solved-confetti {
+ position: fixed;
+ top: 200px;
+ right: 200px;
+ width: 0;
+ height: 0;
+ z-index: z("header") + 1;
+ overflow: visible;
+ pointer-events: none;
+}
+
+.solved-confetti-particle {
+ position: absolute;
+ width: 8px;
+ height: 8px;
+ border-radius: 2px;
+ animation: solved-confetti-burst 1.5s ease-out forwards;
+
+ @for $i from 1 through 50 {
+ &:nth-child(#{$i}) {
+ --angle: #{math.random(360)}deg;
+ --distance: #{80 + math.random(150)}px;
+ --rotation: #{math.random(720) - 360}deg;
+ --delay: #{math.random(250) * 0.001}s;
+ --color: #{hsl(math.random(360), 80%, 60%)};
+ background-color: var(--color);
+ animation-delay: var(--delay);
+ }
+ }
+}
+
+@keyframes solved-confetti-burst {
+ 0% {
+ transform: translate(-50%, -50%) rotate(0deg);
+ opacity: 1;
+ }
+
+ 100% {
+ transform: translate(
+ calc(-50% + cos(var(--angle)) * var(--distance)),
+ calc(-50% + sin(var(--angle)) * var(--distance) + 50px)
+ )
+ rotate(var(--rotation));
+ opacity: 0;
+ }
+}
diff --git a/plugins/discourse-solved/test/javascripts/acceptance/no-answer-prompt-test.js b/plugins/discourse-solved/test/javascripts/acceptance/no-answer-prompt-test.js
index 9c8ce573028..92b840d3f37 100644
--- a/plugins/discourse-solved/test/javascripts/acceptance/no-answer-prompt-test.js
+++ b/plugins/discourse-solved/test/javascripts/acceptance/no-answer-prompt-test.js
@@ -1,5 +1,6 @@
import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
+import sinon from "sinon";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { topicWithNoAnswer } from "../helpers/discourse-solved-helpers";
@@ -11,9 +12,8 @@ acceptance("Discourse Solved - No Answer Prompt", function (needs) {
allow_solved_on_all_topics: true,
});
- test("hides the no answer prompt when clicking the solution button", async function (assert) {
+ needs.hooks.beforeEach(function () {
pretender.get("/t/100.json", () => response(topicWithNoAnswer(1)));
-
pretender.post("/solution/accept", () =>
response({
success: "OK",
@@ -22,7 +22,9 @@ acceptance("Discourse Solved - No Answer Prompt", function (needs) {
excerpt: "<p>Here is a potential answer</p>",
})
);
+ });
+ test("hides the no answer prompt when clicking the solution button", async function (assert) {
await visit("/t/test-topic-no-answer/100");
assert
@@ -35,4 +37,36 @@ acceptance("Discourse Solved - No Answer Prompt", function (needs) {
.dom(".topic-navigation-popup")
.doesNotExist("no answer prompt is hidden after accepting solution");
});
+
+ test("shows confetti when accepting a solution", async function (assert) {
+ await visit("/t/test-topic-no-answer/100");
+
+ assert.dom(".solved-confetti").doesNotExist("confetti is not shown yet");
+
+ await click(".post-action-menu__solved-unaccepted");
+
+ assert.dom(".solved-confetti").exists("confetti is shown after accepting");
+ });
+
+ test("does not show confetti when user prefers reduced motion", async function (assert) {
+ const originalMatchMedia = window.matchMedia;
+
+ sinon.stub(window, "matchMedia").callsFake((query) => {
+ const result = originalMatchMedia.call(window, query);
+ if (query === "(prefers-reduced-motion: reduce)") {
+ return { ...result, matches: true };
+ }
+ return result;
+ });
+
+ await visit("/t/test-topic-no-answer/100");
+
+ assert.dom(".solved-confetti").doesNotExist("confetti is not shown yet");
+
+ await click(".post-action-menu__solved-unaccepted");
+
+ assert
+ .dom(".solved-confetti")
+ .doesNotExist("confetti is not shown when reduced motion is preferred");
+ });
});