Fix navigation in a page using the table of contents

Signed-off-by: Oliver Sand <oliver.sand@sda-se.com>
This commit is contained in:
Oliver Sand
2021-05-04 14:14:02 +02:00
parent 2ffa996ccc
commit 2e05277e0d
2 changed files with 15 additions and 1 deletions
@@ -0,0 +1,5 @@
---
'@backstage/plugin-techdocs': patch
---
Fix navigation in a page using the table of contents.
@@ -264,10 +264,14 @@ export const Reader = ({ entityId, onReady }: Props) => {
);
shadowRoot.appendChild(transformedElement);
// Scroll to top after render
window.scroll({ top: 0 });
// Post-render
transformer(shadowRoot.children[0], [
dom => {
setTimeout(() => {
// Scoll to the desired anchor on initial navigation
if (window.location.hash) {
const hash = window.location.hash.slice(1);
shadowRoot?.getElementById(hash)?.scrollIntoView();
@@ -278,14 +282,19 @@ export const Reader = ({ entityId, onReady }: Props) => {
addLinkClickListener({
baseUrl: window.location.origin,
onClick: (_: MouseEvent, url: string) => {
window.scroll({ top: 0 });
const parsedUrl = new URL(url);
if (newerDocsExist && isSynced) {
// link navigation will load newer docs
setNewerDocsExist(false);
}
if (parsedUrl.hash) {
navigate(`${parsedUrl.pathname}${parsedUrl.hash}`);
// Scroll to hash if it's on the current page
shadowRoot
?.getElementById(parsedUrl.hash.slice(1))
?.scrollIntoView();
} else {
navigate(parsedUrl.pathname);
}