Skip to content

docs: scroll active sidebar link into view on crosslink navigation#34

Merged
rachaelrenk merged 3 commits intomainfrom
docs/migration-ff/sidebar-scroll
May 8, 2026
Merged

docs: scroll active sidebar link into view on crosslink navigation#34
rachaelrenk merged 3 commits intomainfrom
docs/migration-ff/sidebar-scroll

Conversation

@rachaelrenk
Copy link
Copy Markdown
Contributor

@rachaelrenk rachaelrenk commented May 7, 2026

Summary

Scroll the active sidebar link into view when navigating via crosslink to a page in a different section of the sidebar.

Problem

When navigating via crosslink, Starlight correctly highlights the destination page with aria-current="page" in the sidebar, but the sidebar doesn't scroll to that item. Starlight's SidebarPersister restores scrollTop from sessionStorage, which is stale after a crosslink -- the user lands on the correct page but the sidebar shows a different section, causing disorientation.

Fix

Added an inline script to CustomSidebar.astro that calls scrollIntoView({ block: 'nearest' }) on the active sidebar link after page load. The script:

  • Runs after Starlight's inline scroll restoration (which fires synchronously before DOMContentLoaded)
  • Uses block: 'nearest' so it only scrolls if the active item isn't already visible (avoids fighting Starlight's restoration when it works correctly)
  • Desktop only (min-width: 50em) -- mobile sidebar is a drawer that scrolls independently

Context

Part of the docs v2 bug bash follow-up work. Tracks to Notion item: Migration FF: sidebar scroll-to-active on crosslink.

Co-Authored-By: Oz oz-agent@warp.dev

rachaelrenk and others added 2 commits May 7, 2026 12:00
After page load, call scrollIntoView({ block: 'nearest' }) on the
active sidebar link. Starlight's SidebarPersister restores scrollTop
from sessionStorage, but when navigating via crosslink to a different
section, the restored position is stale and the active link ends up
off-screen. block:'nearest' ensures we only scroll if the item isn't
already visible. Desktop only (min-width: 50em).

Co-Authored-By: Oz <oz-agent@warp.dev>
@cla-bot cla-bot Bot added the cla-signed label May 7, 2026
@vercel
Copy link
Copy Markdown

vercel Bot commented May 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment May 8, 2026 1:06am

Request Review

Copy link
Copy Markdown
Collaborator

@hongyi-chen hongyi-chen left a comment

Choose a reason for hiding this comment

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

Approving, but is there any way to make the location that we scroll to on the crosslinked page a bit more centered? e.g. I clicked code editor here, and I navigated to the linked page, but on the lefthand nav bar, the page is at the very bottom. Would be nice if it was up a bit more! lmk if that doesn't make sense

Image Image

Change scrollIntoView from block:'nearest' to block:'center' so
the active link appears in the middle of the sidebar viewport
rather than at the bottom edge. Better for orientation.

Co-Authored-By: Oz <oz-agent@warp.dev>
@rachaelrenk rachaelrenk merged commit f437c8d into main May 8, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants