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

Auto-hide navigation on link click #1238

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

sunblaze
Copy link
Contributor

Hide navigation sidebar when clicking anchor links on mobile devices. Previously, anchor links would change the page but the navigation sidebar would block the view.

@st0012 st0012 force-pushed the auto-hide-nav-on-link-click branch from 94ef2b5 to e1a6d3f Compare December 15, 2024 13:37
@sunblaze sunblaze force-pushed the auto-hide-nav-on-link-click branch from e1a6d3f to 1bdd6eb Compare December 16, 2024 21:09
@st0012
Copy link
Member

st0012 commented Dec 17, 2024

I think we can simplify the implementation to this:

--- a/lib/rdoc/generator/template/darkfish/js/darkfish.js
+++ b/lib/rdoc/generator/template/darkfish/js/darkfish.js
@@ -103,19 +103,18 @@ function hookSidebar() {
   if (isSmallViewport) {
     navigation.hidden = true;
     navigationToggle.ariaExpanded = false;
-  }
-}
 
-function hookAnchorLinks() {
-  document.addEventListener('click', (e) => {
-    const anchor = e.target.closest('#navigation a[href^="#"]');
-    if (anchor && window.matchMedia("(max-width: 1023px)").matches) {
-      const navigation = document.querySelector('#navigation');
-      const navigationToggle = document.querySelector('#navigation-toggle');
-      navigation.hidden = true;
-      navigationToggle.ariaExpanded = false;
-    }
-  });
+    // hide navigation on link click when on small viewport
+    // this saves the user from having to click the toggle button again to hide the navigation
+    document.addEventListener('click', (e) => {
+      if (e.target.closest('#navigation a[href]')) {
+        const navigation = document.querySelector('#navigation');
+        const navigationToggle = document.querySelector('#navigation-toggle');
+        navigation.hidden = true;
+        navigationToggle.ariaExpanded = false;
+      }
+    });
+  }
 }
 
 document.addEventListener('DOMContentLoaded', function() {
@@ -123,5 +122,4 @@ document.addEventListener('DOMContentLoaded', function() {
   hookSearch();
   hookFocus();
   hookSidebar();
-  hookAnchorLinks();
 });

Basically, we can register the event using the same small viewport check to make sure the condition is shared. WDYT?

@sunblaze
Copy link
Contributor Author

Yes, your simplification should work too. But you can also get rid of these lines as well since they're already defined

const navigation = document.querySelector('#navigation');
const navigationToggle = document.querySelector('#navigation-toggle');

Hide navigation sidebar when clicking anchor links on mobile devices.
Previously, anchor links would change the page but the navigation
sidebar would block the view.
@sunblaze sunblaze force-pushed the auto-hide-nav-on-link-click branch from 1bdd6eb to 1d8ae26 Compare December 17, 2024 23:35
Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

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

This is a great quality-of-life improvement. Thank you!

@st0012 st0012 merged commit f12a96b into ruby:master Dec 18, 2024
22 checks passed
st0012 added a commit to Shopify/ruby that referenced this pull request Dec 18, 2024
I was meant to merge this before the auto-syncing stops but was late
a little bit. It's a frontend change for RDoc so ideally we'd want to
test it on docs.ruby-lang.org for a few days before the 3.4 release.
@sunblaze sunblaze deleted the auto-hide-nav-on-link-click branch December 18, 2024 14:45
hsbt pushed a commit to hsbt/ruby that referenced this pull request Dec 18, 2024
(ruby/rdoc#1238)

Hide navigation sidebar when clicking anchor links on mobile devices.
Previously, anchor links would change the page but the navigation
sidebar would block the view.

ruby/rdoc@f12a96b7fa
hsbt pushed a commit to ruby/ruby that referenced this pull request Dec 18, 2024
(ruby/rdoc#1238)

Hide navigation sidebar when clicking anchor links on mobile devices.
Previously, anchor links would change the page but the navigation
sidebar would block the view.

ruby/rdoc@f12a96b7fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants