Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): animate the preview expansion

Elian Dorand9313d1ba206
diff --git a/apps/client/src/widgets/collections/search/SearchResultCard.tsx b/apps/client/src/widgets/collections/search/SearchResultCard.tsx
index 27e9bf36c4..a85cec14e8 100644
--- a/apps/client/src/widgets/collections/search/SearchResultCard.tsx
+++ b/apps/client/src/widgets/collections/search/SearchResultCard.tsx
@@ -1,7 +1,7 @@
import type { HighlightedTokenInfo, SearchResultDetails } from "@triliumnext/commons";
import clsx from "clsx";
import { Fragment } from "preact";
-import { useEffect, useState } from "preact/hooks";
+import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
import type FNote from "../../../entities/fnote";
import { t } from "../../../services/i18n";
@@ -42,11 +42,43 @@ export default function SearchResultCard({ noteId, details, loading, highlighted
const hasDetails = !!details || loading;
const canExpand = !!details?.contentSnippetTruncated;
const [ expanded, setExpanded ] = useState(false);
+ const [ contentReady, setContentReady ] = useState(false);
+ const contentRef = useRef<HTMLDivElement>(null);
+ const pendingHeight = useRef<number | null>(null);
useEffect(() => {
- if (!canExpand) setExpanded(false);
+ if (!canExpand) {
+ setExpanded(false);
+ setContentReady(false);
+ }
}, [ canExpand ]);
+ useEffect(() => {
+ // A fresh expansion waits for the note to render before it replaces the excerpt.
+ if (!expanded) setContentReady(false);
+ }, [ expanded ]);
+
+ useLayoutEffect(() => {
+ const element = contentRef.current;
+ if (!element) return;
+
+ if (expanded && !contentReady) {
+ // The excerpt stands in for the note: it is where the expansion starts from.
+ pendingHeight.current = element.offsetHeight;
+ return;
+ }
+
+ const from = pendingHeight.current;
+ if (from === null) return;
+ pendingHeight.current = null;
+ animateHeight(element, from, element.offsetHeight);
+ }, [ expanded, contentReady ]);
+
+ function toggleExpanded() {
+ pendingHeight.current = contentRef.current?.offsetHeight ?? null;
+ setExpanded(!expanded);
+ }
+
return (
<div
className={clsx("tn-card-frame", "note-book-card", "search-result-card", "no-tooltip-preview",
@@ -66,22 +98,26 @@ export default function SearchResultCard({ noteId, details, loading, highlighted
</h5>
{hasDetails && (
- <div className="search-result-card-content">
+ <div className="search-result-card-content" ref={contentRef}>
{expanded && note ? (
- <NoteContent
- note={note}
- noChildrenList
- highlightedTokens={highlightedTokens}
- includeArchivedNotes={false}
- showTextRepresentation={showTextRepresentation}
- />
+ <>
+ {!contentReady && <SearchResultSnippet details={details} loading={loading} />}
+ <NoteContent
+ note={note}
+ noChildrenList
+ highlightedTokens={highlightedTokens}
+ includeArchivedNotes={false}
+ showTextRepresentation={showTextRepresentation}
+ onReady={() => setContentReady(true)}
+ />
+ </>
) : (
<SearchResultSnippet details={details} loading={loading} />
)}
{canExpand && (
<LinkButton
className="search-result-card-more"
- onClick={() => setExpanded(!expanded)}
+ onClick={toggleExpanded}
text={<>
<Icon icon={expanded ? "bx bx-chevron-up" : "bx bx-chevron-down"} />
<span className="search-result-card-more-label">
@@ -100,6 +136,35 @@ export default function SearchResultCard({ noteId, details, loading, highlighted
);
}
+/** The height animation in flight per card, so a second toggle cancels the first instead of racing it. */
+const heightAnimations = new WeakMap<HTMLElement, Animation>();
+
+/**
+ * Grows or shrinks the card between two measured heights, then hands the height back to CSS. The
+ * wrapper keeps its natural height, so content that renders later (images) is not clipped.
+ */
+function animateHeight(element: HTMLElement, from: number, to: number) {
+ heightAnimations.get(element)?.cancel();
+ if (from === to || window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) {
+ return;
+ }
+
+ element.style.overflow = "hidden";
+ const animation = element.animate(
+ [ { height: `${from}px` }, { height: `${to}px` } ],
+ { duration: 200, easing: "ease" }
+ );
+ heightAnimations.set(element, animation);
+
+ const settle = () => {
+ if (heightAnimations.get(element) !== animation) return;
+ heightAnimations.delete(element);
+ element.style.overflow = "";
+ };
+ animation.onfinish = settle;
+ animation.oncancel = settle;
+}
+
function SearchResultSnippet({ details, loading }: { details: SearchResultDetails | undefined; loading: boolean }) {
if (!details) {
// Details for this page haven't arrived yet: show a skeleton, or nothing once settled.
diff --git a/apps/client/src/widgets/collections/search/SearchResultsList.css b/apps/client/src/widgets/collections/search/SearchResultsList.css
index d11594b4e3..cd81801639 100644
--- a/apps/client/src/widgets/collections/search/SearchResultsList.css
+++ b/apps/client/src/widgets/collections/search/SearchResultsList.css
@@ -75,6 +75,7 @@
/* The expanded render supplies its own content wrapper; the card's padding keeps it aligned. */
.search-result-card-content .note-book-content {
font-size: inherit;
+ animation: none;
}
.search-result-card-content .note-book-content .rendered-content {
diff --git a/apps/client/src/widgets/collections/search/SearchResultsList.spec.tsx b/apps/client/src/widgets/collections/search/SearchResultsList.spec.tsx
index 5561907930..e7a5913640 100644
--- a/apps/client/src/widgets/collections/search/SearchResultsList.spec.tsx
+++ b/apps/client/src/widgets/collections/search/SearchResultsList.spec.tsx
@@ -20,9 +20,25 @@ vi.mock("../../../services/i18n", () => ({
}));
// The expanded view renders the full note through the note list; the toggle is what is under test.
-vi.mock("../legacy/ListOrGridView", () => ({
- NoteContent: () => "EXPANDED"
+const noteContentState = vi.hoisted(() => ({
+ ready: true,
+ reportReady: undefined as (() => void) | undefined
}));
+vi.mock("../legacy/ListOrGridView", async () => {
+ const { useEffect } = await import("preact/hooks");
+ return {
+ NoteContent: ({ onReady }: { onReady?: () => void }) => {
+ useEffect(() => {
+ if (noteContentState.ready) {
+ onReady?.();
+ } else {
+ noteContentState.reportReady = onReady;
+ }
+ }, []);
+ return "EXPANDED";
+ }
+ };
+});
import Component from "../../../components/component";
import type FNote from "../../../entities/fnote";
@@ -53,6 +69,8 @@ let container: HTMLElement;
let parent: Component;
beforeEach(() => {
+ noteContentState.ready = true;
+ noteContentState.reportReady = undefined;
parent = new Component();
container = document.createElement("div");
document.body.appendChild(container);
@@ -166,6 +184,27 @@ describe("SearchResultCard", () => {
expect(container.querySelector(".search-result-card-content")?.textContent).toContain("calendar");
});
+ it("keeps the excerpt while the note it expands to is still rendering", async () => {
+ noteContentState.ready = false;
+ const note = buildNote({ id: "note1", title: "Meeting notes", type: "text" });
+ await mount(
+ <SearchResultCard
+ noteId={note.noteId}
+ details={makeDetails({ contentSnippetTruncated: true })}
+ loading={false}
+ highlightedTokens={null}
+ />
+ );
+
+ const more = container.querySelector<HTMLAnchorElement>(".search-result-card-more");
+ await act(async () => more?.click());
+ expect(container.querySelector(".search-result-card-snippet")).not.toBeNull();
+ expect(container.querySelector(".search-result-card-more")?.textContent).toContain("search_result.show_less");
+
+ await act(async () => noteContentState.reportReady?.());
+ expect(container.querySelector(".search-result-card-snippet")).toBeNull();
+ });
+
it("expands to the full note content when the snippet leaves more behind", async () => {
const note = buildNote({ id: "note1", title: "Meeting notes", type: "text" });
await mount(