Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile): keep the tree's note preview from resizing on return

Elian Dorandb792c84ad46
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.css b/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.css
index 98bde8312d..2fd67cbbc4 100644
--- a/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.css
+++ b/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.css
@@ -302,7 +302,7 @@
/* Collapse the whole preview (and its flex gap) when the rendered content is empty,
so pure folders don't show a blank row beneath the title. */
-.mobile-navigator-current-tile:has(.note-book-content-ready:empty) .mobile-navigator-current-preview {
+.mobile-navigator-current-tile:has(.mobile-navigator-preview-slot:not([hidden]) > .note-book-content-ready:empty) .mobile-navigator-current-preview {
display: none;
}
@@ -382,16 +382,6 @@
cursor: progress;
}
-.mobile-navigator-preloader {
- position: absolute;
- width: 1px;
- height: 1px;
- overflow: hidden;
- clip-path: inset(50%);
- pointer-events: none;
- visibility: hidden;
-}
-
.mobile-navigator-row-icon {
flex: 0 0 auto;
font-size: 1.2em;
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.spec.tsx b/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.spec.tsx
index 899d1f83e2..fcd556d452 100644
--- a/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.spec.tsx
+++ b/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.spec.tsx
@@ -10,7 +10,7 @@ import search from "../../services/search";
import { buildNote, buildNotes } from "../../test/easy-froca";
import { renderInto } from "../../test/render";
import { ParentComponent } from "../react/react_utils";
-import { getListedChildCount, TitleWithIndicators, WorkspaceCards } from "./MobileNoteNavigator";
+import { getListedChildCount, getPreviewNoteIds, TitleWithIndicators, WorkspaceCards } from "./MobileNoteNavigator";
vi.mock("../../services/i18n", () => ({
t: (key: string, options?: object) => (options ? `${key} ${JSON.stringify(options)}` : key),
@@ -37,6 +37,16 @@ describe("getListedChildCount", () => {
});
});
+describe("getPreviewNoteIds", () => {
+ it("keeps the previews of the columns shown last, and the one about to be shown", () => {
+ expect(getPreviewNoteIds([], "a")).toEqual([ "a" ]);
+ expect(getPreviewNoteIds([ "a", "b" ], "b")).toEqual([ "b", "a" ]);
+ expect(getPreviewNoteIds([ "a", "b", "c", "d", "e" ], "f")).toEqual([ "f", "a", "b", "c", "d" ]);
+ expect(getPreviewNoteIds([ "a", "b", "c", "d", "e" ], "a", "f")).toEqual([ "a", "f", "b", "c", "d" ]);
+ expect(getPreviewNoteIds([ "a" ], undefined)).toEqual([ "a" ]);
+ });
+});
+
describe("TitleWithIndicators", () => {
function addParent(parent: FNote, child: FNote) {
const branchId = `${parent.noteId}_${child.noteId}`;
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.tsx b/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.tsx
index 27aa979ffb..04d3004414 100644
--- a/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.tsx
+++ b/apps/client/src/widgets/mobile_widgets/MobileNoteNavigator.tsx
@@ -91,7 +91,6 @@ export default function MobileNoteNavigator() {
const pendingPath = nextStack?.[nextStack.length - 1];
const pendingId = pendingPath ? getLastSegment(pendingPath) : undefined;
- const pendingNote = useNote(pendingId);
const pendingChildId = nextStack && nextStack.length > stack.length ? pendingId : undefined;
const pendingPop = !!nextStack && nextStack.length < stack.length;
@@ -124,15 +123,28 @@ export default function MobileNoteNavigator() {
const backTargetParentId = stack.length >= 3 ? getLastSegment(stack[stack.length - 3]) : undefined;
const backTargetTitle = useNoteTitle(backTargetId, backTargetParentId);
+ // The columns shown last keep their rendered preview, so going back to one (in another tab,
+ // or a step back) shows it laid out at once. The pending column renders its preview hidden,
+ // and the stack commits once it is ready.
+ const [previewIds, setPreviewIds] = useState<string[]>([]);
+ useEffect(() => {
+ if (currentParentId) setPreviewIds((prev) => getPreviewNoteIds(prev, currentParentId));
+ }, [currentParentId]);
+ const [readyPreviewIds, setReadyPreviewIds] = useState<ReadonlySet<string>>(() => new Set());
+ const setPreviewReady = useCallback((noteId: string, ready: boolean) => {
+ setReadyPreviewIds((prev) => {
+ if (prev.has(noteId) === ready) return prev;
+ const next = new Set(prev);
+ if (ready) next.add(noteId);
+ else next.delete(noteId);
+ return next;
+ });
+ }, []);
+
// Gate the visible body on the content preview being ready to avoid a layout shift
- // when the preview finishes rendering. Tied to the parent's noteId so a stale "ready"
- // flag from the previous column can't leak into the new one.
- const [readyForNoteId, setReadyForNoteId] = useState<string | null>(null);
- const previewReady = !!parentNote && readyForNoteId === parentNote.noteId;
+ // when the preview finishes rendering.
+ const previewReady = !!currentParentId && readyPreviewIds.has(currentParentId);
const bodyVisible = previewReady && isLoaded;
- const onPreviewReady = useCallback(() => {
- if (parentNote) setReadyForNoteId(parentNote.noteId);
- }, [parentNote]);
// Full-screen spinner only for the very first render — once a body has been shown,
// subsequent navigations swap views without a global placeholder.
@@ -140,27 +152,19 @@ export default function MobileNoteNavigator() {
if (bodyVisible) hasCommittedOnceRef.current = true;
const showInitialLoader = !bodyVisible && !hasCommittedOnceRef.current;
- // Preload state for the pending stack target. Once ready, commit the stack change.
- const [nextReadyNoteId, setNextReadyNoteId] = useState<string | null>(null);
- const onPendingReady = useCallback(() => {
- if (pendingNote) setNextReadyNoteId(pendingNote.noteId);
- }, [pendingNote]);
-
const scrollRef = useRef<HTMLDivElement>(null);
const bodyRef = useRef<HTMLDivElement>(null);
const directionRef = useRef<"forward" | "backward" | null>(null);
const [commitCounter, setCommitCounter] = useState(0);
useEffect(() => {
- if (!nextStack || !pendingId || nextReadyNoteId !== pendingId) return;
+ if (!nextStack || !pendingId || !readyPreviewIds.has(pendingId)) return;
commitStack(nextStack);
- setReadyForNoteId(pendingId);
setNextStack(null);
- setNextReadyNoteId(null);
setCommitCounter((c) => c + 1);
// Reset scroll so the committed tile is visible at the top of the column.
if (scrollRef.current) scrollRef.current.scrollTop = 0;
- }, [nextStack, pendingId, nextReadyNoteId, commitStack]);
+ }, [nextStack, pendingId, readyPreviewIds, commitStack]);
// Brief slide-in on forward / back so the swap feels directional without blocking the user.
useLayoutEffect(() => {
@@ -311,14 +315,15 @@ export default function MobileNoteNavigator() {
</button>
</div>
<div className="mobile-navigator-current-preview">
- <NoteContent
- note={parentNote}
- trim
- noChildrenList
- highlightedTokens={null}
- includeArchivedNotes={!hideArchived}
- onReady={onPreviewReady}
- />
+ {getPreviewNotes(getPreviewNoteIds(previewIds, currentParentId, pendingId)).map((note) => (
+ <PreviewSlot
+ key={note.noteId}
+ note={note}
+ shown={note.noteId === currentParentId}
+ hideArchived={hideArchived}
+ onReady={setPreviewReady}
+ />
+ ))}
</div>
</div>
)}
@@ -360,22 +365,48 @@ export default function MobileNoteNavigator() {
</div>
)}
</div>
+ </div>
+ );
+}
- {/* Hidden preloader: renders NoteContent for the pending target so we can
- commit the stack change only after its preview is ready. */}
- {pendingNote && (
- <div className="mobile-navigator-preloader" aria-hidden="true">
- <NoteContent
- key={pendingNote.noteId}
- note={pendingNote}
- trim
- noChildrenList
- highlightedTokens={null}
- includeArchivedNotes={!hideArchived}
- onReady={onPendingReady}
- />
- </div>
- )}
+/** How many columns keep their rendered preview. */
+const PREVIEW_CACHE_SIZE = 5;
+
+/**
+ * The columns whose preview stays rendered: the current one, the pending one and those shown
+ * last, most recent first.
+ */
+export function getPreviewNoteIds(recentIds: string[], currentId: string | undefined, pendingId?: string) {
+ const ids = [ currentId, pendingId, ...recentIds ].filter((id): id is string => !!id);
+ return [ ...new Set(ids) ].slice(0, PREVIEW_CACHE_SIZE);
+}
+
+/** Notes in a stable order, so a preview keeps its place in the DOM as the recency changes. */
+function getPreviewNotes(noteIds: string[]) {
+ return [ ...noteIds ].sort()
+ .map((noteId) => froca.getNoteFromCache(noteId))
+ .filter((note): note is FNote => !!note);
+}
+
+function PreviewSlot({ note, shown, hideArchived, onReady }: {
+ note: FNote;
+ shown: boolean;
+ hideArchived: boolean;
+ onReady: (noteId: string, ready: boolean) => void;
+}) {
+ const { noteId } = note;
+ useEffect(() => () => onReady(noteId, false), [ noteId, onReady ]);
+
+ return (
+ <div className="mobile-navigator-preview-slot" hidden={!shown}>
+ <NoteContent
+ note={note}
+ trim
+ noChildrenList
+ highlightedTokens={null}
+ includeArchivedNotes={!hideArchived}
+ onReady={() => onReady(noteId, true)}
+ />
</div>
);
}