Skip to content

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

UX: Add animations to closing DMenu content (#36737)

Jordan Vidrine9cc884d2899d
diff --git a/app/assets/stylesheets/common/base/discourse.scss b/app/assets/stylesheets/common/base/discourse.scss
index 77d517c58bd..433385dd423 100644
--- a/app/assets/stylesheets/common/base/discourse.scss
+++ b/app/assets/stylesheets/common/base/discourse.scss
@@ -107,6 +107,30 @@
}
}
+@keyframes dissolve-up {
+ 0% {
+ opacity: 1;
+ transform: translateY(0);
+ }
+
+ 100% {
+ opacity: 0;
+ transform: translateY(-10px);
+ }
+}
+
+@keyframes dissolve-down {
+ 0% {
+ opacity: 1;
+ transform: translateY(0);
+ }
+
+ 100% {
+ opacity: 0;
+ transform: translateY(10px);
+ }
+}
+
@keyframes float-left {
0% {
opacity: 0;
diff --git a/app/assets/stylesheets/common/float-kit/d-menu.scss b/app/assets/stylesheets/common/float-kit/d-menu.scss
index b4e5a0edc6b..fad8fb4ce8e 100644
--- a/app/assets/stylesheets/common/float-kit/d-menu.scss
+++ b/app/assets/stylesheets/common/float-kit/d-menu.scss
@@ -14,15 +14,23 @@
}
&.-animated {
- @include d-animation(float-down, 0.25s, ease);
+ @include d-animation(float-down, 250ms, ease);
+
+ &.-closing {
+ @include d-animation(dissolve-up, 125ms, ease);
+ }
&[data-placement^="bottom"] {
transform-origin: top center;
}
&[data-placement^="top"] {
- @include d-animation(float-up, 0.25s, ease);
+ @include d-animation(float-up, 250ms, ease);
transform-origin: bottom center;
+
+ &.-closing {
+ @include d-animation(dissolve-down, 125ms, ease);
+ }
}
&[data-placement^="right"] {
diff --git a/app/assets/stylesheets/common/rich-editor/rich-editor.scss b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
index b45492fa919..909f9465009 100644
--- a/app/assets/stylesheets/common/rich-editor/rich-editor.scss
+++ b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
@@ -293,7 +293,6 @@
.fk-d-menu[data-identifier="composer-image-alt-text"] {
user-select: none;
z-index: z("composer", "dropdown");
- animation: fade-in ease 0.25s 1 forwards !important;
.fk-d-menu__inner-content {
overflow: visible;
diff --git a/frontend/discourse/app/components/d-modal.gjs b/frontend/discourse/app/components/d-modal.gjs
index 399883350e9..b511bf3603d 100644
--- a/frontend/discourse/app/components/d-modal.gjs
+++ b/frontend/discourse/app/components/d-modal.gjs
@@ -13,6 +13,7 @@ import FlashMessage from "discourse/components/flash-message";
import concatClass from "discourse/helpers/concat-class";
import element from "discourse/helpers/element";
import htmlClass from "discourse/helpers/html-class";
+import { waitForAnimationEnd } from "discourse/lib/animation-utils";
import { lock, unlock } from "discourse/lib/body-scroll-lock";
import { getMaxAnimationTimeMs } from "discourse/lib/swipe-events";
import swipe from "discourse/modifiers/swipe";
@@ -62,7 +63,7 @@ export default class DModal extends Component {
this.animating = true;
this.modalContainer.classList.add("is-entering");
- await this.#waitForAnimationEnd(this.modalContainer);
+ await waitForAnimationEnd(this.modalContainer);
this.modalContainer.classList.remove("is-entering");
this.animating = false;
@@ -180,7 +181,7 @@ export default class DModal extends Component {
backdrop.classList.add("is-exiting");
}
- await this.#waitForAnimationEnd(this.modalContainer);
+ await waitForAnimationEnd(this.modalContainer);
}
} finally {
this.animating = false;
@@ -260,31 +261,6 @@ export default class DModal extends Component {
);
}
- #waitForAnimationEnd(el) {
- return new Promise((resolve) => {
- const style = window.getComputedStyle(el);
- const duration = parseFloat(style.animationDuration) * 1000 || 0;
- const delay = parseFloat(style.animationDelay) * 1000 || 0;
- const totalTime = duration + delay;
-
- const timeoutId = setTimeout(
- () => {
- el.removeEventListener("animationend", handleAnimationEnd);
- resolve();
- },
- Math.max(totalTime + 50, 50)
- );
-
- const handleAnimationEnd = () => {
- clearTimeout(timeoutId);
- el.removeEventListener("animationend", handleAnimationEnd);
- resolve();
- };
-
- el.addEventListener("animationend", handleAnimationEnd);
- });
- }
-
async #animatePopOff() {
const backdrop = this.wrapperElement.nextElementSibling;
@@ -297,8 +273,8 @@ export default class DModal extends Component {
await waitForPromise(
Promise.all([
- this.#waitForAnimationEnd(this.modalContainer),
- this.#waitForAnimationEnd(backdrop),
+ waitForAnimationEnd(this.modalContainer),
+ waitForAnimationEnd(backdrop),
])
);
}
diff --git a/frontend/discourse/app/lib/animation-utils.js b/frontend/discourse/app/lib/animation-utils.js
new file mode 100644
index 00000000000..32b7f01c7c0
--- /dev/null
+++ b/frontend/discourse/app/lib/animation-utils.js
@@ -0,0 +1,40 @@
+import { waitForPromise } from "@ember/test-waiters";
+import { isTesting } from "discourse/lib/environment";
+import { prefersReducedMotion } from "discourse/lib/utilities";
+
+export async function waitForAnimationEnd(element) {
+ return new Promise((resolve) => {
+ const style = window.getComputedStyle(element);
+ const duration = parseFloat(style.animationDuration) * 1000 || 0;
+ const delay = parseFloat(style.animationDelay) * 1000 || 0;
+ const totalTime = duration + delay;
+
+ const handleAnimationEnd = () => {
+ clearTimeout(timeoutId);
+ element.removeEventListener("animationend", handleAnimationEnd);
+ resolve();
+ };
+
+ const timeoutId = setTimeout(
+ () => {
+ element.removeEventListener("animationend", handleAnimationEnd);
+ resolve();
+ },
+ // The timeout acts as a fallback in case the "animationend" event does not fire (e.g., when no animation is present).
+ Math.max(totalTime + 50, 50)
+ );
+
+ element.addEventListener("animationend", handleAnimationEnd);
+ });
+}
+
+export async function animateClosing(element, className = "-closing") {
+ if (!element || prefersReducedMotion() || isTesting()) {
+ return;
+ }
+ element.classList.add(className);
+
+ await waitForPromise(waitForAnimationEnd(element));
+
+ element.classList.remove(className);
+}
diff --git a/frontend/discourse/float-kit/lib/d-menu-instance.js b/frontend/discourse/float-kit/lib/d-menu-instance.js
index 697341ac408..65cf4b80d72 100644
--- a/frontend/discourse/float-kit/lib/d-menu-instance.js
+++ b/frontend/discourse/float-kit/lib/d-menu-instance.js
@@ -5,6 +5,7 @@ import { getOwner, setOwner } from "@ember/owner";
import { service } from "@ember/service";
import { MENU } from "discourse/float-kit/lib/constants";
import FloatKitInstance from "discourse/float-kit/lib/float-kit-instance";
+import { animateClosing } from "discourse/lib/animation-utils";
export default class DMenuInstance extends FloatKitInstance {
@service menu;
@@ -68,6 +69,8 @@ export default class DMenuInstance extends FloatKitInstance {
return;
}
+ await animateClosing(this.content);
+
await super.close(...arguments);
if (this.site.mobileView && this.options.modalForMobile && this.expanded) {
diff --git a/spec/system/composer/prosemirror_links_spec.rb b/spec/system/composer/prosemirror_links_spec.rb
index 962df8102f0..f379b4eea7a 100644
--- a/spec/system/composer/prosemirror_links_spec.rb
+++ b/spec/system/composer/prosemirror_links_spec.rb
@@ -155,10 +155,10 @@ describe "Composer - ProseMirror - Links", type: :system do
open_composer
composer.type_content("Text before [Example](https://example.com),")
composer.send_keys(:left)
- expect(page).to have_css("[data-identifier='composer-link-toolbar']")
+ wait_for { page.has_css?("[data-identifier='composer-link-toolbar']") }
expect(page).to have_css("a.composer-link-toolbar__visit", text: "example.com")
composer.send_keys(:right)
- expect(page).to have_no_css("[data-identifier='composer-link-toolbar']")
+ wait_for { page.has_no_css?("[data-identifier='composer-link-toolbar']") }
end
it "preserves emojis when editing a link via toolbar" do