-
Notifications
You must be signed in to change notification settings - Fork 440
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
Conversation
94ef2b5
to
e1a6d3f
Compare
e1a6d3f
to
1bdd6eb
Compare
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? |
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.
1bdd6eb
to
1d8ae26
Compare
There was a problem hiding this 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!
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.
(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
(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
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.