Skip to content

Build your personal knowledge base with Trilium Notes

feat(share): keep the navigation pane's header in view while the tree scrolls

Elian Doran36e34efca0ef
diff --git a/packages/share-theme/src/page/navigation.css b/packages/share-theme/src/page/navigation.css
index 4fb8c57419..517e4c5069 100644
--- a/packages/share-theme/src/page/navigation.css
+++ b/packages/share-theme/src/page/navigation.css
@@ -1,14 +1,26 @@
#navigation {
+ --site-header-fade: 16px;
+
display: flex;
flex-direction: column;
- padding: var(--page-top-spacing) 25px 25px;
- gap: 10px;
+ padding: 0 25px 25px;
}
+/* Stays at the top of the pane while the tree scrolls, which fades out under its bottom edge. */
#site-header {
+ position: sticky;
+ top: 0;
+ z-index: 1;
display: flex;
flex-direction: column;
gap: 20px;
+ margin-inline: -25px;
+ padding: var(--page-top-spacing) 25px var(--site-header-fade);
+ background: linear-gradient(
+ to bottom,
+ var(--background-secondary) calc(100% - var(--site-header-fade)),
+ transparent
+ );
}
#site-title-row {
diff --git a/packages/share-theme/src/page/navigation.spec.ts b/packages/share-theme/src/page/navigation.spec.ts
index b1a1b85e16..6c7dd6e268 100644
--- a/packages/share-theme/src/page/navigation.spec.ts
+++ b/packages/share-theme/src/page/navigation.spec.ts
@@ -200,6 +200,11 @@ describe("setupTreeState", () => {
renderPane("site1", { activeTop: 368 });
setupTreeState();
expect(pane().scrollTop).toBe(0);
+
+ // Below the pane's top but behind its 100px sticky header: 60 - 100 - (300 - 32) / 2
+ renderPane("site1", { activeTop: 60, headerHeight: 100 });
+ setupTreeState();
+ expect(pane().scrollTop).toBe(-174);
});
it("ignores a malformed position and works with blocked storage or without a pane", () => {
@@ -230,11 +235,20 @@ describe("setupTreeState", () => {
* Renders a 400px tall pane at the top of the window, with a current note of 32px at `activeTop`
* when it is given; happy-dom has no layout, so the geometry is set by hand.
*/
-function renderPane(siteId: string, options: { activeTop?: number; menu?: string } = {}) {
- const { activeTop, menu = NOTE } = options;
+function renderPane(
+ siteId: string, options: { activeTop?: number; menu?: string; headerHeight?: number } = {}
+) {
+ const { activeTop, menu = NOTE, headerHeight } = options;
document.body.dataset.ancestorNoteId = siteId;
- document.body.innerHTML = `<div id="left-pane"><nav id="menu">${menu}</nav></div>`;
+ document.body.innerHTML = `
+ <div id="left-pane"><div id="site-header"></div><nav id="menu">${menu}</nav></div>`;
const paneEl = pane();
+ const header = document.getElementById("site-header");
+ if (headerHeight === undefined) {
+ header?.remove();
+ } else if (header) {
+ header.getBoundingClientRect = () => new DOMRect(0, 0, 200, headerHeight);
+ }
Object.defineProperty(paneEl, "clientHeight", { value: 400 });
paneEl.getBoundingClientRect = () => new DOMRect(0, 0, 200, 400);
const active = paneEl.querySelector<HTMLElement>("a.active");
diff --git a/packages/share-theme/src/page/navigation.ts b/packages/share-theme/src/page/navigation.ts
index 322a03de4c..47ddb5eca2 100644
--- a/packages/share-theme/src/page/navigation.ts
+++ b/packages/share-theme/src/page/navigation.ts
@@ -83,9 +83,13 @@ export function setupTreeState() {
if (active) {
const paneRect = pane.getBoundingClientRect();
const activeRect = active.getBoundingClientRect();
- if (activeRect.top < paneRect.top || activeRect.bottom > paneRect.bottom) {
- const centered = (pane.clientHeight - activeRect.height) / 2;
- pane.scrollTop += activeRect.top - paneRect.top - centered;
+ // The sticky `#site-header` covers the top of the pane.
+ const visibleTop = pane.querySelector("#site-header")?.getBoundingClientRect().bottom
+ ?? paneRect.top;
+ if (activeRect.top < visibleTop || activeRect.bottom > paneRect.bottom) {
+ const visibleHeight = pane.clientHeight - (visibleTop - paneRect.top);
+ const centered = (visibleHeight - activeRect.height) / 2;
+ pane.scrollTop += activeRect.top - visibleTop - centered;
}
}