Skip to content

doc: inject sidebar active link scroll into view script #59007

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/api_assets/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@
});
}

/**
* Scrolls the active link into view, centering it in the viewport.
* This is useful for documentation pages where the active link should be visible
* when the page loads or when navigating to a section.
*/
function scrollIntoViewWithActiveLink() {
const activeLInk = document.querySelector('.interior ul .active');
if (activeLInk) {
activeLInk.scrollIntoView({
behavior: 'smooth',
block: 'center',
});
}
}

function bootstrap() {
// Check if we have JavaScript support.
document.documentElement.classList.add('has-js');
Expand All @@ -206,6 +221,8 @@
setupFlavorToggles();

setupCopyButton();

scrollIntoViewWithActiveLink();
}

if (document.readyState === 'loading') {
Expand Down
Loading