fix: keep active tool visible and update generator defaults#81
Merged
Conversation
Deploying tools-collection with
|
| Latest commit: |
6019d05
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://3dd3ff61.tools-collection.pages.dev |
| Branch Preview URL: | https://fix-tool-sidebar-and-generat.tools-collection.pages.dev |
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a feature to automatically scroll the active tool into view within the sidebar and updates various generator components to default to a single output count. Feedback was provided to refine the sidebar scrolling logic by including a visibility check to prevent layout jumps and using a global guard to avoid registering duplicate event listeners when the component is rendered multiple times.
Comment on lines
+125
to
+141
| function scrollActiveToolIntoView() { | ||
| var sidebars = document.querySelectorAll('[data-tool-sidebar]') | ||
| for (var i = 0; i < sidebars.length; i++) { | ||
| var sidebar = sidebars[i] | ||
| var activeLink = sidebar.querySelector('a[aria-current="page"]') | ||
| if (activeLink) { | ||
| activeLink.scrollIntoView({ block: 'center', inline: 'nearest' }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| initSidebarFavorites() | ||
| scrollActiveToolIntoView() | ||
| document.addEventListener('astro:page-load', initSidebarFavorites) | ||
| document.addEventListener('astro:page-load', scrollActiveToolIntoView) | ||
| window.addEventListener('favorites-updated', initSidebarFavorites) | ||
| window.addEventListener('favorites-updated', scrollActiveToolIntoView) |
Contributor
There was a problem hiding this comment.
The implementation of scrollActiveToolIntoView and its event registration has a few areas for improvement:
- Visibility Check: Calling
scrollIntoViewon elements within a hidden container (like the mobile sidebar when on desktop) can cause unexpected scrolling behavior or layout jumps in some browsers. Adding a check foroffsetParentensures we only scroll the visible sidebar. - Duplicate Event Listeners: Since this script is
is:inlineand part of a component that is instantiated twice (once for mobile, once for desktop), the script runs twice. This results in duplicate event listeners being added to thewindowanddocumentobjects. Using a global guard variable prevents this memory leak and redundant execution. - Redundant Execution: The functions are called both directly and via
astro:page-loadon the initial load. While callinginitSidebarFavoritesimmediately is beneficial to avoid a flash of empty content (since the favorites list is populated client-side),scrollActiveToolIntoViewcan be redundant when triggered twice in quick succession.
function scrollActiveToolIntoView() {
var sidebars = document.querySelectorAll('[data-tool-sidebar]')
for (var i = 0; i < sidebars.length; i++) {
var sidebar = sidebars[i]
var activeLink = sidebar.querySelector('a[aria-current="page"]')
// Only scroll if the link exists and is currently visible
if (activeLink && activeLink.offsetParent !== null) {
activeLink.scrollIntoView({ block: 'center', inline: 'nearest' })
}
}
}
if (!window.__sidebarInitialized) {
document.addEventListener('astro:page-load', initSidebarFavorites)
document.addEventListener('astro:page-load', scrollActiveToolIntoView)
window.addEventListener('favorites-updated', initSidebarFavorites)
window.addEventListener('favorites-updated', scrollActiveToolIntoView)
window.__sidebarInitialized = true
}
// Initial execution for the current page state
initSidebarFavorites()
scrollActiveToolIntoView()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
1where appropriate.Notes