Ensure link to current page is visible in sidebar #2012
Closed
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.
Description
When navigating sites with long sidebars (like the Astro docs), it can be quite disorientating when you click on a link and suddenly lose the context of where you are in the sidebar because it disappears. I see this behaviour in Firefox 126 and Chrome canary, so I assume it isn't just a browser quirk.
This PR solves the problem by adding a little script that is injected with the top-level sidebar. The script checks if the sidebar link to the current page is visible, and if it isn't, it is scrolled into view.
I have it set to jump instantly, but it could be jarring if the page loads really slowly. If this proves to be an issue, you could consider changing the behaviour to
"smooth"
. This can be annoying if you see it happen on every page load though.It seems like Vite isn't processing the script right now, which is probably because of my conditional rendering of it. I assume we want Vite to process it, but it could also be bad if it causes the JS to be loaded externally. I'd appreciate some guidance here.