Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: use a slide animation for reordering cards

Adorian Dorane9439fcddda1
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index d777fdbbfa..679f8540b2 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -18,6 +18,7 @@ import ActionButton from "../../react/ActionButton";
import Icon from "../../react/Icon";
import { IconPickerButton } from "../../react/IconPicker";
import { useStaticTooltip } from "../../react/hooks";
+import { useFlip } from "../../react/flip";
import { useScrollFade } from "../../react/scroll_fade";
import NoteLink from "../../react/NoteLink";
import { BoardActionsContext, BoardDragStateContext, TitleEditor } from ".";
@@ -97,6 +98,8 @@ export default function Column({
const editorRef = useRef<HTMLInputElement>(null);
const contentRef = useRef<HTMLDivElement>(null);
const scrollFade = useScrollFade(contentRef);
+ // The gap opening and closing moves the cards below it, which they follow rather than jump to.
+ useFlip(contentRef, { selector: ".board-note" });
const { handleDragOver, handleDragLeave, handleDrop } = useDragging({
column, columnIndex, columnItems, isEditing, api, parentNote
});
diff --git a/apps/client/src/widgets/react/flip.spec.tsx b/apps/client/src/widgets/react/flip.spec.tsx
new file mode 100644
index 0000000000..0b50e64ee2
--- /dev/null
+++ b/apps/client/src/widgets/react/flip.spec.tsx
@@ -0,0 +1,122 @@
+/**
+ * happy-dom lays nothing out, so every child's place is declared and moved by hand: what these
+ * check is that a change of place becomes a slide, and that nothing else does.
+ */
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { useRef } from "preact/hooks";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { useFlip } from "./flip";
+
+describe("useFlip", () => {
+ let container: HTMLElement | undefined;
+ let frames: (() => void)[] = [];
+
+ beforeEach(() => {
+ frames = [];
+ vi.stubGlobal("requestAnimationFrame", (callback: () => void) => {
+ frames.push(callback);
+ return frames.length;
+ });
+ vi.stubGlobal("matchMedia", () => ({ matches: false }));
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ });
+
+ it("carries a child back to where it was, and lets go on the next frame", async () => {
+ const { items } = await draw([ "a", "b" ]);
+ place(items(), [ 0, 40 ]);
+ await redraw();
+
+ // The second one is pushed down by something appearing above it.
+ place(items(), [ 0, 90 ]);
+ await redraw();
+
+ const [ first, second ] = items();
+ expect(first.style.transform).toBe("");
+ expect(second.style.transform).toBe("translateY(-50px)");
+ expect(second.style.transition).toBe("none");
+
+ act(() => { for (const frame of frames.splice(0)) frame(); });
+
+ expect(second.style.transform).toBe("");
+ expect(second.style.transition).toBe("");
+ });
+
+ it("leaves alone a child that has not moved, and one with no place at all", async () => {
+ const { items } = await draw([ "a", "b" ]);
+ place(items(), [ 0, 40 ]);
+ await redraw();
+
+ // The first keeps its place; the second is taken out of the flow and put back elsewhere.
+ place(items(), [ 0, 40 ]);
+ Object.defineProperty(items()[1], "offsetParent", { value: null, configurable: true });
+ await redraw();
+ expect(items().every(item => item.style.transform === "")).toBe(true);
+
+ place(items(), [ 0, 200 ]);
+ await redraw();
+ expect(items()[1].style.transform).toBe("");
+ });
+
+ it("says nothing while it is switched off", async () => {
+ const { items } = await draw([ "a", "b" ], true);
+ place(items(), [ 0, 40 ]);
+ await redraw();
+ place(items(), [ 0, 90 ]);
+ await redraw();
+
+ expect(items()[1].style.transform).toBe("");
+ });
+
+ let rerender: () => Promise<void>;
+
+ async function draw(keys: string[], disabled = false) {
+ const mountPoint = document.createElement("div");
+ container = mountPoint;
+ document.body.appendChild(mountPoint);
+
+ function Harness() {
+ const ref = useRef<HTMLDivElement>(null);
+ useFlip(ref, { selector: ".item", disabled });
+ return (
+ <div ref={ref}>
+ {keys.map(key => <div key={key} className="item">{key}</div>)}
+ </div>
+ );
+ }
+
+ rerender = async () => {
+ await act(async () => { render(<Harness />, mountPoint); });
+ };
+ await rerender();
+
+ return { items: () => [ ...mountPoint.querySelectorAll<HTMLElement>(".item") ] };
+ }
+
+ async function redraw() {
+ await rerender();
+ }
+
+ /** happy-dom reports no offsets of its own, so each child is told where it stands. */
+ function place(items: HTMLElement[], tops: number[]) {
+ for (const [ index, item ] of items.entries()) {
+ Object.defineProperty(item, "offsetTop", {
+ value: tops[index], configurable: true, writable: true
+ });
+ if (!Object.getOwnPropertyDescriptor(item, "offsetParent")) {
+ Object.defineProperty(item, "offsetParent", {
+ value: item.parentElement, configurable: true, writable: true
+ });
+ }
+ }
+ }
+});
diff --git a/apps/client/src/widgets/react/flip.ts b/apps/client/src/widgets/react/flip.ts
new file mode 100644
index 0000000000..3f5792abc8
--- /dev/null
+++ b/apps/client/src/widgets/react/flip.ts
@@ -0,0 +1,77 @@
+import { RefObject } from "preact";
+import { useLayoutEffect, useRef } from "preact/hooks";
+
+export interface FlipOptions {
+ /** Which children to follow. */
+ selector: string;
+ /** Whether to leave them where the browser puts them. */
+ disabled?: boolean;
+}
+
+/**
+ * Slides a container's children from where they stood to where they now stand.
+ *
+ * The children are drawn in their new places as usual, then carried back to their old ones for a
+ * frame and let go, which the transition they already wear turns into a slide. Nothing about the
+ * layout changes: a child inserted between two others still moves them, and this only says so.
+ *
+ * Positions are read as `offsetTop`, which neither a scroll nor a transform of a slide still
+ * running moves, so neither is taken for a change of place.
+ */
+export function useFlip(ref: RefObject<HTMLElement>, { selector, disabled }: FlipOptions) {
+ // Written after every commit, so what it holds is where the children stood before this one.
+ const seen = useRef(new Map<Element, number>());
+
+ useLayoutEffect(() => {
+ const container = ref.current;
+ if (!container) {
+ seen.current.clear();
+ return;
+ }
+
+ const before = seen.current;
+ const now = new Map<Element, number>();
+ const moved: { child: HTMLElement, by: number }[] = [];
+
+ // Read first and write after: a transform written between two reads costs a fresh layout
+ // for every child that follows it.
+ for (const child of container.querySelectorAll<HTMLElement>(selector)) {
+ // A child out of the flow has no place to be moved from, and is left with none, so
+ // that coming back is an arrival rather than a slide from wherever it last stood.
+ if (!child.offsetParent) {
+ continue;
+ }
+
+ const top = child.offsetTop;
+ now.set(child, top);
+
+ const previous = before.get(child);
+ if (!disabled && previous !== undefined && Math.abs(previous - top) >= 1) {
+ moved.push({ child, by: previous - top });
+ }
+ }
+
+ seen.current = now;
+
+ for (const { child, by } of moved) {
+ slide(child, by);
+ }
+ });
+}
+
+/** Puts a child back where it was, and lets go of it on the next frame. */
+function slide(element: HTMLElement, offset: number) {
+ if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) {
+ return;
+ }
+
+ element.style.transition = "none";
+ element.style.transform = `translateY(${offset}px)`;
+
+ requestAnimationFrame(() => {
+ if (element.isConnected) {
+ element.style.transition = "";
+ element.style.transform = "";
+ }
+ });
+}