Merge pull request #4352 from backstage/orkohunter/microsite-docs-scroll-nav-sidebar

microsite/docs: Scroll nav sidebar to view to improve discoverability
This commit is contained in:
Himanshu Mishra
2021-02-02 12:10:55 +01:00
committed by GitHub
2 changed files with 24 additions and 0 deletions
+1
View File
@@ -83,6 +83,7 @@ const siteConfig = {
'https://unpkg.com/medium-zoom@1.0.6/dist/medium-zoom.min.js',
'/js/medium-zoom.js',
'/js/dismissable-banner.js',
'/js/scroll-nav-to-view-in-docs.js',
],
// On page navigation for the current documentation page.
@@ -0,0 +1,23 @@
// On backstage.io/docs pages, scroll the Nav sidebar to focus on
// the page being viewed. Helpful when the Nav is large enough that
// the selected page is hidden somewhere at bottom.
// Credits: https://github.com/facebook/docusaurus/issues/823#issuecomment-421152269
document.addEventListener('DOMContentLoaded', () => {
// Find the active nav item in the sidebar
const item = document.getElementsByClassName('navListItemActive')[0];
if (!item) {
return;
}
const bounding = item.getBoundingClientRect();
if (
bounding.top >= 0 &&
bounding.bottom <=
(window.innerHeight || document.documentElement.clientHeight)
) {
// Already visible. Do nothing.
} else {
// Not visible. Scroll sidebar.
item.scrollIntoView({ block: 'start', inline: 'nearest' });
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
});