Skip to content

Commit

Permalink
feat: scroll selected sidebar entry into view (#471)
Browse files Browse the repository at this point in the history
Long sidebars did not scroll to show the selected entry. This made
working with such long sidebars quite confusing.
  • Loading branch information
yuri1969 authored Oct 12, 2024
1 parent 94624bc commit 97ea671
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion assets/js/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
document.addEventListener("DOMContentLoaded", function () {
scrollToActiveItem();
enableCollapsibles();
});

function enableCollapsibles() {
const buttons = document.querySelectorAll(".hextra-sidebar-collapsible-button");
buttons.forEach(function (button) {
button.addEventListener("click", function (e) {
Expand All @@ -9,4 +14,23 @@ document.addEventListener("DOMContentLoaded", function () {
}
});
});
});
}

function scrollToActiveItem() {
const sidebarScrollbar = document.querySelector("aside.sidebar-container > .hextra-scrollbar");
const activeItems = document.querySelectorAll(".sidebar-active-item");
const visibleActiveItem = Array.from(activeItems).find(function (activeItem) {
return activeItem.getBoundingClientRect().height > 0;
});

if (!visibleActiveItem) {
return;
}

const yOffset = visibleActiveItem.clientHeight;
const yDistance = visibleActiveItem.getBoundingClientRect().top - sidebarScrollbar.getBoundingClientRect().top;
sidebarScrollbar.scrollTo({
behavior: "instant",
top: yDistance - yOffset
});
}

0 comments on commit 97ea671

Please sign in to comment.