Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored messages.js #1852

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

adamzap
Copy link
Member

@adamzap adamzap commented Dec 28, 2024

  • Simplified code using a combination of CSS and JavaScript
  • Stopped using jQuery
  • Moved refactored code to djangoproject.js

This patch should bring no user-facing changes.

Refs #1827

The easiest way I found to test with was to add the following code to the bottom of blog.views.BlogViewMixin right before the return:

        from django.contrib import messages

        messages.info(self.request, 'abc')
        messages.success(self.request, 'def')

Then test this way:

  1. Navigate to the "News" (/weblog/) section of the site
  2. Dismiss the two message

- Simplified code using a combination of CSS and JavaScript
- Stopped using jQuery
- Moved refactored code to `djangoproject.js`

This patch should bring no user-facing changes.

Refs django#1827
@@ -3416,6 +3416,8 @@ ul.corporate-members li {
color: $green-dark;
border: 1px solid $green-dark;
border-radius: 4px;
opacity: 1;
transition: opacity 400ms;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 400ms value here matches the value in the JavaScript. I'd much prefer using a CSS transition than trying to fade the element out in JavaScript.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's possible to have the same UX using only CSS?

Copy link
Member Author

@adamzap adamzap Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! The display property is not animatable by default, but by using transition-behavior: allow-discrete, we could get the desired UX in all modern browsers except for Firefox. In my testing, Firefox would only lose the fade effect, so it seems to degrade gracefully.

We'd still need one line of JavaScript to apply the fade-out class unless we wanted to resort to other kinds of hacks in the HTML.

Here's the diff if you are curious or want to try it locally. Let me know if you want me to push a commit for this:

diff --git a/djangoproject/scss/_style.scss b/djangoproject/scss/_style.scss
index 0c3395a2..222f43b2 100644
--- a/djangoproject/scss/_style.scss
+++ b/djangoproject/scss/_style.scss
@@ -3417,7 +3417,8 @@ ul.corporate-members li {
         border: 1px solid $green-dark;
         border-radius: 4px;
         opacity: 1;
-        transition: opacity 400ms;
+        transition: opacity 400ms, display 400ms;
+        transition-behavior: allow-discrete;
 
         &::before {
             @include fa-icon();
@@ -3431,6 +3432,7 @@ ul.corporate-members li {
 
         &.fade-out {
             opacity: 0;
+            display: none;
         }
 
         &.info {
diff --git a/djangoproject/static/js/djangoproject.js b/djangoproject/static/js/djangoproject.js
index 29256b31..3cb47a15 100644
--- a/djangoproject/static/js/djangoproject.js
+++ b/djangoproject/static/js/djangoproject.js
@@ -16,9 +16,5 @@ document.querySelectorAll('#doc-versions a').forEach(function (el) {
 document.querySelectorAll('.messages li .close').forEach(function (el) {
   el.addEventListener('click', function (e) {
     this.parentElement.classList.add('fade-out');
-
-    setTimeout(function () {
-      el.parentElement.style.display = 'none';
-    }, 400);
   });
 });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants