diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ccd5abf8..aa5d2d84a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Unreleased +### Bug Fixes + +- Page navigation now ensures the current page is visible when the page is first loaded, #2626. + ## v0.26.3 (2024-06-28) ### Features diff --git a/src/lib/output/themes/default/assets/typedoc/Application.ts b/src/lib/output/themes/default/assets/typedoc/Application.ts index 66fb784f8..45296947b 100644 --- a/src/lib/output/themes/default/assets/typedoc/Application.ts +++ b/src/lib/output/themes/default/assets/typedoc/Application.ts @@ -107,13 +107,15 @@ export class Application { iter = iter.parentElement; } - if (pageLink && !pageLink.checkVisibility()) { + if (pageLink && !checkVisible(pageLink)) { const top = pageLink.getBoundingClientRect().top - document.documentElement.clientHeight / 4; // If we are showing three columns, this will scroll the site menu down to // show the page we just loaded in the navigation. document.querySelector(".site-menu")!.scrollTop = top; + // If we are showing two columns + document.querySelector(".col-sidebar")!.scrollTop = top; } } @@ -218,3 +220,13 @@ export class Application { }); } } + +// https://stackoverflow.com/a/5354536/7186598 +function checkVisible(elm: Element) { + const rect = elm.getBoundingClientRect(); + const viewHeight = Math.max( + document.documentElement.clientHeight, + window.innerHeight, + ); + return !(rect.bottom < 0 || rect.top - viewHeight >= 0); +}