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 4 commits 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);
   });
 });

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for throwing one more option into the mix, but what about hooking to the transitionend event instead of using setTimeOut? https://developer.mozilla.org/en-US/docs/Web/API/Element/transitionend_event

That way we don't have to dupicate the transition duration between the CSS and js.

It'd look something like this:

// Fade out and remove message elements when close icon is clicked
document.querySelectorAll('.messages li .close').forEach(function (el) {
  el.addEventListener('click', function (e) {
    this.parentElement.addEventListener('transitionend', function (e) {
        this.style.display = 'none';
    })
    this.parentElement.classList.add('fade-out');
  });
});

Copy link
Member Author

Choose a reason for hiding this comment

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

Wow, much better, thanks! I didn't know that event existed. I pushed a commit.

@adamzap adamzap force-pushed the refactor-messages-js branch from 67f5e5f to ab1c702 Compare January 17, 2025 17:28
Copy link
Member

@bmispelon bmispelon left a comment

Choose a reason for hiding this comment

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

A pure CSS solution would be nice, but if it's not cross-browser compatible then it's less ideal.

The current approach seems like a good compromise, and the transitionend js can be swapped out for transition-behavior: allow-discrete once that's cross-browser.

I've found a few nitpicks, but other than that I think that this is good to merge.

Thanks!

@@ -3416,6 +3416,8 @@ ul.corporate-members li {
color: $green-dark;
border: 1px solid $green-dark;
border-radius: 4px;
opacity: 1;
Copy link
Member

Choose a reason for hiding this comment

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

Looking at the MDN documentation for opacity it seems that 1 is the default value, so I think we don't need to specify it here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! Done in 1bed277.

@@ -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

Choose a reason for hiding this comment

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

I'd move this rule to the &.fade-out selector later, just so that all the fading/animation logic all lives together.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! Fixed in 9c5a059.

@pauloxnet
Copy link
Member

A pure CSS solution would be nice, but if it's not cross-browser compatible then it's less ideal.

I can be wrong but it seems that the transition-behavior: allow-discrete; is available on all browser:
https://developer.mozilla.org/en-US/docs/Web/CSS/transition-behavior

I tried only with Firefox Mobile for Android.

@adamzap can you also double check it?

The current approach seems like a good compromise, and the transitionend js can be swapped out for transition-behavior: allow-discrete once that's cross-browser.

If MDN is wrong about transition-behavior: allow-discrete; I also agree with Baptist that this solution can be merged.

@adamzap
Copy link
Member Author

adamzap commented Jan 17, 2025

@pauloxnet There's a special page for the display property in that context: https://caniuse.com/mdn-css_properties_display_is_transitionable

I tried it locally, and the fade doesn't happen on Firefox for macOS.

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.

3 participants