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:
@@ -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;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user