Skip to content

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

UX: Improvements to modal animation (#35740)

Keegan George5263c1632738
diff --git a/app/assets/stylesheets/common/base/modal.scss b/app/assets/stylesheets/common/base/modal.scss
index 387b584ba73..8fe69d22aca 100644
--- a/app/assets/stylesheets/common/base/modal.scss
+++ b/app/assets/stylesheets/common/base/modal.scss
@@ -68,6 +68,9 @@ html:not(.keyboard-visible).mobile-device {
width: 100%;
max-width: 100%;
max-height: calc(var(--composer-vh, 1dvh) * 85);
+
+ @include enter-animation(modal-enter-mobile, 250ms, ease);
+ @include exit-animation(modal-exit-mobile, 250ms, ease);
}
@include viewport.from(sm) {
@@ -82,6 +85,17 @@ html:not(.keyboard-visible).mobile-device {
.d-modal.-max & {
max-width: 90vw;
}
+
+ @include enter-animation(
+ modal-enter-desktop,
+ 250ms,
+ cubic-bezier(0.4, 0, 0.2, 1)
+ );
+ @include exit-animation(
+ modal-exit-desktop,
+ 250ms,
+ cubic-bezier(0.4, 0, 0.2, 1)
+ );
}
}
@@ -215,6 +229,12 @@ html:not(.keyboard-visible).mobile-device {
@media (prefers-reduced-motion) {
animation-duration: 0s;
}
+
+ @include exit-animation(
+ backdrop-fade-out,
+ 250ms,
+ cubic-bezier(0.4, 0, 0.2, 1)
+ );
}
#modal-alert {
@@ -282,6 +302,61 @@ html:not(.keyboard-visible).mobile-device {
}
}
+// Modal animations
+@keyframes modal-enter-desktop {
+ from {
+ transform: scale(0.7);
+ opacity: 0;
+ }
+
+ to {
+ transform: scale(1);
+ opacity: 1;
+ }
+}
+
+@keyframes modal-exit-desktop {
+ from {
+ transform: scale(1);
+ opacity: 1;
+ }
+
+ to {
+ transform: scale(0.7);
+ opacity: 0;
+ }
+}
+
+@keyframes modal-enter-mobile {
+ from {
+ transform: translateY(100%);
+ }
+
+ to {
+ transform: translateY(0);
+ }
+}
+
+@keyframes modal-exit-mobile {
+ from {
+ transform: translateY(0);
+ }
+
+ to {
+ transform: translateY(100%);
+ }
+}
+
+@keyframes backdrop-fade-out {
+ from {
+ opacity: 0.6;
+ }
+
+ to {
+ opacity: 0;
+ }
+}
+
.modal-form {
margin-bottom: 0;
}
diff --git a/app/assets/stylesheets/common/foundation/mixins.scss b/app/assets/stylesheets/common/foundation/mixins.scss
index 073005d3420..11c6642d897 100644
--- a/app/assets/stylesheets/common/foundation/mixins.scss
+++ b/app/assets/stylesheets/common/foundation/mixins.scss
@@ -151,6 +151,26 @@ $hpad: 0.65em;
animation: float-up ease 0.25s 1 forwards;
}
+@mixin enter-animation($name, $duration, $easing: ease) {
+ &.is-entering {
+ animation: $name $duration $easing forwards;
+
+ @media (prefers-reduced-motion) {
+ animation-duration: 0s;
+ }
+ }
+}
+
+@mixin exit-animation($name, $duration, $easing: ease) {
+ &.is-exiting {
+ animation: $name $duration $easing forwards;
+
+ @media (prefers-reduced-motion) {
+ animation-duration: 0s;
+ }
+ }
+}
+
@mixin default-focus() {
border-color: var(--d-input-focused-color);
outline: 2px solid var(--d-input-focused-color);
diff --git a/frontend/discourse/app/components/d-modal.gjs b/frontend/discourse/app/components/d-modal.gjs
index 650eabde987..4936458b7b9 100644
--- a/frontend/discourse/app/components/d-modal.gjs
+++ b/frontend/discourse/app/components/d-modal.gjs
@@ -60,24 +60,14 @@ export default class DModal extends Component {
this.handleDocumentKeydown
);
- if (this.site.mobileView) {
- this.animating = true;
-
- await waitForPromise(
- el.animate(
- [{ transform: "translateY(100%)" }, { transform: "translateY(0)" }],
- {
- duration: getMaxAnimationTimeMs(),
- easing: "ease",
- fill: "forwards",
- }
- ).finished
- );
+ this.wrapperElement = el;
+ this.animating = true;
- this.animating = false;
- }
+ this.modalContainer.classList.add("is-entering");
+ await this.#waitForAnimationEnd(this.modalContainer);
+ this.modalContainer.classList.remove("is-entering");
- this.wrapperElement = el;
+ this.animating = false;
}
@action
@@ -177,26 +167,24 @@ export default class DModal extends Component {
return;
}
- if (this.site.mobileView) {
+ try {
this.animating = true;
- this.#animateBackdropOpacity(window.innerHeight);
-
- await this.#animateWrapperPosition(this.modalContainer.clientHeight);
-
- this.animating = false;
- }
-
- if (this.site.desktopView) {
- try {
- this.animating = true;
+ if (this.site.desktopView) {
await this.#animatePopOff();
- } finally {
- this.animating = false;
+ } else {
+ const backdrop = this.wrapperElement.nextElementSibling;
+ this.modalContainer.classList.add("is-exiting");
+ if (backdrop) {
+ backdrop.classList.add("is-exiting");
+ }
+
+ await this.#waitForAnimationEnd(this.modalContainer);
}
+ } finally {
+ this.animating = false;
+ this.args.closeModal({ initiatedBy });
}
-
- this.args.closeModal({ initiatedBy });
}
@action
@@ -265,6 +253,31 @@ 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;
@@ -272,24 +285,13 @@ export default class DModal extends Component {
return;
}
+ this.modalContainer.classList.add("is-exiting");
+ backdrop.classList.add("is-exiting");
+
await waitForPromise(
Promise.all([
- this.modalContainer.animate(
- [
- { transform: "scale(1)", opacity: 1, offset: 0 },
- { transform: "scale(0)", opacity: 0, offset: 1 },
- ],
- {
- fill: "forwards",
- duration: getMaxAnimationTimeMs(300),
- easing: "cubic-bezier(0.4, 0, 0.2, 1)",
- }
- ).finished,
- backdrop.animate([{ opacity: 0.6 }, { opacity: 0 }], {
- fill: "forwards",
- duration: getMaxAnimationTimeMs(300),
- easing: "cubic-bezier(0.4, 0, 0.2, 1)",
- }).finished,
+ this.#waitForAnimationEnd(this.modalContainer),
+ this.#waitForAnimationEnd(backdrop),
])
);
}