Skip to content

Build your personal knowledge base with Trilium Notes

feat(menus): play a short closing animation as menus close

Elian Doran0d8fe9aae42b
diff --git a/apps/client/src/widgets/react/Popup.css b/apps/client/src/widgets/react/Popup.css
index ade8f3745e..f375d42e58 100644
--- a/apps/client/src/widgets/react/Popup.css
+++ b/apps/client/src/widgets/react/Popup.css
@@ -66,6 +66,20 @@ body .tn-popup {
&.mobile-bottom-menu[data-reveal="stylesheet"] {
animation: tn-popup-sheet 200ms cubic-bezier(0.2, 0, 0, 1);
}
+
+ /* The copy `playClosing()` leaves in Popup.tsx. */
+ &[data-closing] {
+ pointer-events: none;
+ animation: tn-popup-conceal 90ms cubic-bezier(0.4, 0, 1, 1) forwards;
+ }
+
+ &[data-reveal="stylesheet"][data-closing] {
+ animation: tn-popup-sink 120ms cubic-bezier(0.4, 0, 1, 1) forwards;
+ }
+
+ &.mobile-bottom-menu[data-reveal="stylesheet"][data-closing] {
+ animation: tn-popup-sheet-out 160ms cubic-bezier(0.4, 0, 1, 1) forwards;
+ }
}
/* Floating UI lines a start or end alignment up with the other edge in a right-to-left page. */
@@ -98,6 +112,27 @@ body[dir=rtl] .tn-popup[data-reveal]:is([data-reveal="bottom-end"], [data-reveal
}
}
+@keyframes tn-popup-conceal {
+ to {
+ opacity: 0;
+ translate: var(--tn-reveal-x) var(--tn-reveal-y);
+ scale: 0.96;
+ }
+}
+
+@keyframes tn-popup-sink {
+ to {
+ opacity: 0;
+ translate: 0 16px;
+ }
+}
+
+@keyframes tn-popup-sheet-out {
+ to {
+ translate: 0 100%;
+ }
+}
+
/*
* A menu's or a scrollable panel's frame does not scroll, so the blur the theme puts on its
* `::before` covers it whatever its content scrolls to. The content scrolls inside, in the height
diff --git a/apps/client/src/widgets/react/Popup.spec.tsx b/apps/client/src/widgets/react/Popup.spec.tsx
index f62e94264b..a9893e2db1 100644
--- a/apps/client/src/widgets/react/Popup.spec.tsx
+++ b/apps/client/src/widgets/react/Popup.spec.tsx
@@ -173,6 +173,45 @@ describe("Popup", () => {
expect((await open()).dataset.reveal).toBeUndefined();
});
+ it("leaves an inert copy in its wrapper to play its closing animation, gone once that ends", async () => {
+ anchorAt(100, 50);
+ const popup = await open({ reveal: true, portalClassName: "tn-dropdown-portal global-menu", id: "the-menu" });
+ popup.scrollTop = 40;
+ let finish = () => {};
+ const finished = new Promise<void>((resolve) => {
+ finish = resolve;
+ });
+ vi.spyOn(HTMLElement.prototype, "getAnimations").mockImplementation(function (this: HTMLElement) {
+ return this.dataset.closing !== undefined ? [ { finished } as unknown as Animation ] : [];
+ });
+
+ render(null, host);
+ const copy = document.querySelector<HTMLElement>(".tn-popup");
+ expect(copy).not.toBeNull();
+ expect(copy).not.toBe(popup);
+ expect(copy?.dataset.closing).toBe("");
+ expect(copy?.dataset.reveal).toBe("bottom-start");
+ expect([ copy?.hasAttribute("inert"), copy?.getAttribute("aria-hidden"), copy?.id ]).toEqual([ true, "true", "" ]);
+ expect(copy?.scrollTop).toBe(40);
+ expect(copy?.parentElement?.className).toBe("tn-dropdown-portal global-menu");
+ expect(copy?.parentElement?.parentElement).toBe(document.body);
+
+ finish();
+ await vi.waitFor(() => expect(document.querySelector(".tn-dropdown-portal")).toBeNull());
+ });
+
+ it("leaves no copy behind without a reveal, or with nothing to play", async () => {
+ anchorAt(100, 50);
+ await open();
+ render(null, host);
+ expect(document.querySelector(".tn-popup")).toBeNull();
+
+ // As with motion turned off, which leaves no animation.
+ await open({ reveal: true });
+ render(null, host);
+ expect(document.querySelector(".tn-popup")).toBeNull();
+ });
+
it("says nothing of a placement that settles once it has closed", async () => {
anchorAt(100, 50);
const onPlaced = vi.fn();
diff --git a/apps/client/src/widgets/react/Popup.tsx b/apps/client/src/widgets/react/Popup.tsx
index bb81002084..690be25e74 100644
--- a/apps/client/src/widgets/react/Popup.tsx
+++ b/apps/client/src/widgets/react/Popup.tsx
@@ -145,6 +145,15 @@ export default function Popup({ anchor, placement = "bottom-start", offset: gap
};
}, [ "x" in anchor ? undefined : anchor, anchorX, anchorY, placement, gap, capHeight, placedByStylesheet ]);
+ const inWrapper = useRef(!!portalClassName);
+ inWrapper.current = !!portalClassName;
+ useLayoutEffect(() => {
+ const popup = popupRef.current;
+ return () => {
+ if (popup) playClosing(popup, inWrapper.current);
+ };
+ }, []);
+
const dismiss = useRef(onDismiss);
dismiss.current = onDismiss;
const escapes = useRef(escapeDismisses);
@@ -222,6 +231,48 @@ function swallowStrayClick(pressed: Node) {
window.addEventListener("pointerdown", stop, true);
}
+/**
+ * Leaves an inert copy of `popup`, which is being removed, to play its closing animation (see
+ * Popup.css), and removes the copy once that ends. The popup itself goes at once, so focus and
+ * presses never wait on the animation. The copy keeps the wrapper the popup stands in, which CSS
+ * rules that place it are scoped under.
+ */
+function playClosing(popup: HTMLElement, inWrapper: boolean) {
+ if (popup.dataset.reveal === undefined || popup.style.visibility !== "visible") return;
+ const root = inWrapper ? popup.parentElement : popup;
+ const container = root?.parentElement;
+ if (!root || !container) return;
+
+ const copy = root.cloneNode(true) as HTMLElement;
+ const copiedPopup = inWrapper ? copy.querySelector<HTMLElement>(":scope > .tn-popup") : copy;
+ if (!copiedPopup) return;
+ copiedPopup.dataset.closing = "";
+ copiedPopup.setAttribute("inert", "");
+ copiedPopup.setAttribute("aria-hidden", "true");
+ for (const element of [ copy, ...copy.querySelectorAll("[id]") ]) {
+ element.removeAttribute("id");
+ }
+ // A copied frame or media element would load again.
+ for (const element of copy.querySelectorAll(":scope > .tn-dropdown-backdrop, iframe, video, audio")) {
+ element.remove();
+ }
+ container.append(copy);
+
+ const scrollers = (element: HTMLElement) => [ element, ...element.querySelectorAll<HTMLElement>(".tn-menu-scroll, .tn-panel-scroll") ];
+ const copiedScrollers = scrollers(copiedPopup);
+ for (const [ index, scroller ] of scrollers(popup).entries()) {
+ const copied = copiedScrollers.at(index);
+ if (copied) copied.scrollTop = scroller.scrollTop;
+ }
+
+ const animations = copiedPopup.getAnimations();
+ if (!animations.length) {
+ copy.remove();
+ return;
+ }
+ void Promise.allSettled(animations.map((animation) => animation.finished)).then(() => copy.remove());
+}
+
/** Removes what `placeFloating()` wrote on `popup`, so the stylesheet places it. */
function clearPlacement(popup: HTMLElement) {
popup.style.removeProperty("left");