Skip to content

Commit

Permalink
microsite/docs: Scroll nav sidebar to view for loaded page
Browse files Browse the repository at this point in the history
  • Loading branch information
OrkoHunter committed Feb 2, 2021
1 parent bc18571 commit f573c57
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions microsite/siteConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const siteConfig = {
'https://unpkg.com/[email protected]/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.
Expand Down
23 changes: 23 additions & 0 deletions microsite/static/js/scroll-nav-to-view-in-docs.js
Original file line number Diff line number Diff line change
@@ -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;
}
});

0 comments on commit f573c57

Please sign in to comment.