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

[FIX] website: make anchor links work in offcanvas mobile menu #146132

Closed

Conversation

rdeodoo
Copy link
Contributor

@rdeodoo rdeodoo commented Dec 13, 2023

A anchor menu in the mobile offcanvas related to an element in the
current page doesn't work in offcanvas.

This is because the menu anchor navigation is hooked to use our own
scrolling behavior instead of the browser one.

Doing so, we preventDefault, which prevent the offcanvas menu to close
itself when clicking on a anchor menu.

This commit simply manually close the offcanvas and once the closing
animation is complete, starts our own smooth scrolling.

Another possibility would have been to just close manually the offcanvas
without a preventDefault and without a call to our custom scrolling
method.
Doing so, the browser would naturally scroll to the element while we
close the offcanvas but it would be less elegant as you wouldn't see the
scrolling animation.

Note that the offcanvas was introduced with commit 1.

opw-3604963

@rdeodoo
Copy link
Contributor Author

rdeodoo commented Dec 13, 2023

Alternative mentioned in commit message would be about just this line if the if () condition:

            Offcanvas.getInstance(offcanvasEl).hide();

@rdeodoo rdeodoo requested a review from qsm-odoo December 13, 2023 11:41
@robodoo
Copy link
Contributor

robodoo commented Dec 13, 2023

Pull request status dashboard.

@C3POdoo C3POdoo requested a review from a team December 13, 2023 11:41
@rdeodoo rdeodoo force-pushed the 17.0-fix-offcanvas-mobile-anchor-rde branch from 3d99df3 to 63cfea3 Compare December 13, 2023 11:42
@C3POdoo C3POdoo added the OE the report is linked to a support ticket (opw-...) label Dec 13, 2023
@rdeodoo rdeodoo force-pushed the 17.0-fix-offcanvas-mobile-anchor-rde branch 3 times, most recently from f59769f to 423cf77 Compare December 13, 2023 12:50
@rdeodoo
Copy link
Contributor Author

rdeodoo commented Dec 14, 2023

@qsm-odoo opw PR ready, feel free to delegate it should you prefer

@rdeodoo rdeodoo force-pushed the 17.0-fix-offcanvas-mobile-anchor-rde branch from 423cf77 to 5413854 Compare December 15, 2023 12:14
@C3POdoo C3POdoo requested review from a team, FrancoisGe and BastienFafchamps and removed request for a team December 15, 2023 12:16
@rdeodoo
Copy link
Contributor Author

rdeodoo commented Dec 19, 2023

I'll retarget to 16, here is the diff needed where offcanvas was introduced (probably 17)

diff --git a/addons/web/tooling/_eslintrc.json b/addons/web/tooling/_eslintrc.json
index 467b2ff2ab785..016030f9cf324 100644
--- a/addons/web/tooling/_eslintrc.json
+++ b/addons/web/tooling/_eslintrc.json
@@ -43,24 +43,26 @@
     "_": "readonly",
     "Chart": "readonly",
     "fuzzy": "readonly",
-    "Popover": "readonly",
     "StackTrace": "readonly",
     "QUnit": "readonly",
     "luxon": "readonly",
     "py": "readonly",
     "FullCalendar": "readonly",
     "globalThis": "readonly",
-    "Modal": "readonly",
-    "Dropdown": "readonly",
     "ScrollSpy": "readonly",
-    "Tooltip": "readonly",
-    "Collapse": "readonly",
-    "Alert": "readonly",
     "module": "readonly",
     "chai": "readonly",
     "describe": "readonly",
     "it": "readonly",
     "mocha": "readonly",
-    "DOMPurify": "readonly"
+    "DOMPurify": "readonly",
+
+    "Alert": "readonly",
+    "Collapse": "readonly",
+    "Dropdown": "readonly",
+    "Modal": "readonly",
+    "Offcanvas": "readonly",
+    "Popover": "readonly",
+    "Tooltip": "readonly"
   }
 }
 
diff --git a/addons/website/static/src/js/content/snippets.animation.js b/addons/website/static/src/js/content/snippets.animation.js
index d1203c79de4ac..c4541e0d2bddd 100644
--- a/addons/website/static/src/js/content/snippets.animation.js
+++ b/addons/website/static/src/js/content/snippets.animation.js
@@ -1039,8 +1039,23 @@ registry.anchorSlide = publicWidget.Widget.extend({
         if (!$anchor.length || !scrollValue) {
             return;
         }
-        ev.preventDefault();
-        this._scrollTo($anchor, scrollValue);
+
+        const offcanvasEl = document.querySelector('.offcanvas.o_navbar_mobile');
+        if (offcanvasEl && offcanvasEl.classList.contains('show')) {
+            // Special case for anchors in offcanvas in mobile: we can't just
+            // _scrollTo() after preventDefault because preventDefault would
+            // prevent the offcanvas to be closed. The choice is then to close
+            // it ourselves manually and once it's fully closed, then start our
+            // own smooth scrolling.
+            ev.preventDefault();
+            Offcanvas.getInstance(offcanvasEl).hide();
+            offcanvasEl.addEventListener('hidden.bs.offcanvas', () => {
+                this._scrollTo($anchor, scrollValue);
+            });
+        } else {
+            ev.preventDefault();
+            this._scrollTo($anchor, scrollValue);
+        }
     },
 });

Commit msg

[FIX] website: make anchor links work in offcanvas mobile menu           
                                                                         
A anchor menu in the mobile offcanvas related to an element in the       
current page doesn't work in offcanvas:                                  
- The offcanvas doesn't close                                            
- The page doesn't scroll to the clicked location                        
                                                                         
This is because the menu anchor navigation is hooked to use our own      
scrolling behavior instead of the browser one.                           
                                                                         
Doing so, we preventDefault, which prevent the offcanvas menu to close   
itself when clicking on a anchor menu.                                   
                                                                         
This commit simply manually closes the offcanvas and once the closing    
animation is complete, starts our own smooth scrolling.                  
It also targets the desktop offcanvas menu (when hamburger layout is     
selected) so it got a smoother UX: it closes then scrolls, instead of    
scrolling but not closing.                                               
                                                                         
Another possibility would have been to just close manually the offcanvas 
without a preventDefault and without a call to our custom scrolling      
method.                                                                  
Doing so, the browser would naturally scroll to the element while we     
close the offcanvas but it would be less elegant as you wouldn't see the 
scrolling animation.                                                     
                                                                         
Note that the offcanvas was introduced with commit [1].                  
                                                                         
[1]: https://github.com/odoo/odoo/commit/bc13176de8d66bbdc1c536017b1f046c5fd31a86
                                                                         
opw-3604963

@rdeodoo rdeodoo force-pushed the 17.0-fix-offcanvas-mobile-anchor-rde branch from 5413854 to 8ef0cb0 Compare December 19, 2023 12:53
@rdeodoo rdeodoo changed the base branch from 17.0 to 16.0 December 19, 2023 12:53
@rdeodoo
Copy link
Contributor Author

rdeodoo commented Dec 19, 2023

@qsm-odoo this is ready, retargeted 16.0 as discussed here: #146132 (comment)
In Odoo 17 the diff will be different, see my previous message (for the diff and commit msg), as Offcanvas makes it worst.

@qsm-odoo qsm-odoo force-pushed the 17.0-fix-offcanvas-mobile-anchor-rde branch from 8ef0cb0 to d511d91 Compare December 19, 2023 15:28
A anchor menu in the mobile collapse related to an element in the
current page doesn't work correctly:
- The collapse-menu doesn't close
- If the menu is large enough, on mobile, you actually don't even see
  the page scrolling behind it so it looks like the click did nothing.

This is probably because of the switch from BS4 to BS5.
In later version, it's even worse, as we are using bootstrap Offcanvas
instead of collapse, creating another bug:
- The page doesn't scroll at all

This will be fixed in the forward port.

This commit simply manually closes the collapse before starting  our own
smooth scrolling.
It also targets the desktop offcanvas menu (when hamburger layout is
selected).

opw-3604963
@qsm-odoo qsm-odoo force-pushed the 17.0-fix-offcanvas-mobile-anchor-rde branch from d511d91 to 9633a7e Compare December 19, 2023 15:29
Copy link
Contributor

@qsm-odoo qsm-odoo left a comment

Choose a reason for hiding this comment

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

  • Made this change
  • Fixed a small typo in commit message "worst" instead of "worse"

@robodoo r+

robodoo pushed a commit that referenced this pull request Dec 19, 2023
A anchor menu in the mobile collapse related to an element in the
current page doesn't work correctly:
- The collapse-menu doesn't close
- If the menu is large enough, on mobile, you actually don't even see
  the page scrolling behind it so it looks like the click did nothing.

This is probably because of the switch from BS4 to BS5.
In later version, it's even worse, as we are using bootstrap Offcanvas
instead of collapse, creating another bug:
- The page doesn't scroll at all

This will be fixed in the forward port.

This commit simply manually closes the collapse before starting  our own
smooth scrolling.
It also targets the desktop offcanvas menu (when hamburger layout is
selected).

opw-3604963

closes #146132

Signed-off-by: Quentin Smetz (qsm) <[email protected]>
@robodoo robodoo closed this Dec 19, 2023
@qsm-odoo qsm-odoo deleted the 17.0-fix-offcanvas-mobile-anchor-rde branch December 19, 2023 17:52
@fw-bot
Copy link
Contributor

fw-bot commented Dec 23, 2023

3 similar comments
@fw-bot
Copy link
Contributor

fw-bot commented Dec 24, 2023

@fw-bot
Copy link
Contributor

fw-bot commented Dec 25, 2023

@fw-bot
Copy link
Contributor

fw-bot commented Dec 26, 2023

luanjubica pushed a commit to luanjubica/odoo-code that referenced this pull request Feb 14, 2024
A anchor menu in the mobile collapse related to an element in the
current page doesn't work correctly:
- The collapse-menu doesn't close
- If the menu is large enough, on mobile, you actually don't even see
  the page scrolling behind it so it looks like the click did nothing.

This is probably because of the switch from BS4 to BS5.
In later version, it's even worse, as we are using bootstrap Offcanvas
instead of collapse, creating another bug:
- The page doesn't scroll at all

This will be fixed in the forward port.

This commit simply manually closes the collapse before starting  our own
smooth scrolling.
It also targets the desktop offcanvas menu (when hamburger layout is
selected).

opw-3604963

closes odoo#146132

Signed-off-by: Quentin Smetz (qsm) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OE the report is linked to a support ticket (opw-...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants