forked from backstage/backstage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
microsite/docs: Scroll nav sidebar to view for loaded page
- Loading branch information
1 parent
bc18571
commit f573c57
Showing
2 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); |