Skip to content

fix: keep active tool visible and update generator defaults#81

Merged
gioxx merged 2 commits into
mainfrom
fix/tool-sidebar-and-generator-defaults
May 21, 2026
Merged

fix: keep active tool visible and update generator defaults#81
gioxx merged 2 commits into
mainfrom
fix/tool-sidebar-and-generator-defaults

Conversation

@gioxx
Copy link
Copy Markdown
Collaborator

@gioxx gioxx commented May 21, 2026

Summary

  • Keep the selected tool centered in the left sidebar after navigation.
  • Lower several generator defaults to 1 where appropriate.

Notes

  • No automated tests were run for this change.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 21, 2026

Deploying tools-collection with  Cloudflare Pages  Cloudflare Pages

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

View logs

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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 thread src/components/Sidebar.astro Outdated
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The implementation of scrollActiveToolIntoView and its event registration has a few areas for improvement:

  1. Visibility Check: Calling scrollIntoView on 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 for offsetParent ensures we only scroll the visible sidebar.
  2. Duplicate Event Listeners: Since this script is is:inline and 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 the window and document objects. Using a global guard variable prevents this memory leak and redundant execution.
  3. Redundant Execution: The functions are called both directly and via astro:page-load on the initial load. While calling initSidebarFavorites immediately is beneficial to avoid a flash of empty content (since the favorites list is populated client-side), scrollActiveToolIntoView can 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()

@gioxx gioxx merged commit ffca6f6 into main May 21, 2026
2 checks passed
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.

1 participant