Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix an issue

Adorian Dorancb588e77564c
diff --git a/apps/client/src/widgets/react/flip.spec.tsx b/apps/client/src/widgets/react/flip.spec.tsx
index 8f6b4f45f6..68e5b4b250 100644
--- a/apps/client/src/widgets/react/flip.spec.tsx
+++ b/apps/client/src/widgets/react/flip.spec.tsx
@@ -78,6 +78,35 @@ describe("useFlip", () => {
expect(items()[1].style.transform).toBe("translateX(-180px)");
});
+ /**
+ * An offset is measured from whatever is positioned above the child, so a change of that is a
+ * different number for the very same place: a column's cards read one way against the column
+ * and another against the box holding them, and the difference is a header's height.
+ */
+ it("leaves alone a child whose offsets are measured from somewhere else", async () => {
+ const { items } = await draw([ "a", "b" ]);
+ place(items(), [ 0, 40 ]);
+ await redraw();
+
+ const [ , second ] = items();
+ Object.defineProperty(second, "offsetParent", {
+ value: document.createElement("div"), configurable: true, writable: true
+ });
+ Object.defineProperty(second, "offsetTop", {
+ value: 115, configurable: true, writable: true
+ });
+ await redraw();
+
+ expect(second.style.transform).toBe("");
+
+ // Read against the same thing twice over, it moves as any other child does.
+ Object.defineProperty(second, "offsetTop", {
+ value: 160, configurable: true, writable: true
+ });
+ await redraw();
+ expect(second.style.transform).toBe("translateY(-45px)");
+ });
+
it("says nothing while it is switched off", async () => {
const { items } = await draw([ "a", "b" ], true);
place(items(), [ 0, 40 ]);
diff --git a/apps/client/src/widgets/react/flip.ts b/apps/client/src/widgets/react/flip.ts
index c1eb931231..ef9d2e937b 100644
--- a/apps/client/src/widgets/react/flip.ts
+++ b/apps/client/src/widgets/react/flip.ts
@@ -24,7 +24,7 @@ export function useFlip(
ref: RefObject<HTMLElement>, { selector, axis = "vertical", 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>());
+ const seen = useRef(new Map<Element, Place>());
useLayoutEffect(() => {
const container = ref.current;
@@ -34,7 +34,7 @@ export function useFlip(
}
const before = seen.current;
- const now = new Map<Element, number>();
+ const now = new Map<Element, Place>();
const moved: { child: HTMLElement, by: number }[] = [];
// Read first and write after: a transform written between two reads costs a fresh layout
@@ -46,12 +46,15 @@ export function useFlip(
continue;
}
+ const from = child.offsetParent;
const at = axis === "vertical" ? child.offsetTop : child.offsetLeft;
- now.set(child, at);
+ now.set(child, { at, from });
+ // Only against the same origin: an offset is measured from whatever is positioned above
+ // it, and a change of that is a different number for the very same place.
const previous = before.get(child);
- if (!disabled && previous !== undefined && Math.abs(previous - at) >= 1) {
- moved.push({ child, by: previous - at });
+ if (!disabled && previous?.from === from && Math.abs(previous.at - at) >= 1) {
+ moved.push({ child, by: previous.at - at });
}
}
@@ -63,6 +66,12 @@ export function useFlip(
});
}
+/** Where a child stood, and what that was measured from. */
+interface Place {
+ at: number;
+ from: Element | null;
+}
+
/** Puts a child back where it was, and lets go of it on the next frame. */
function slide(element: HTMLElement, offset: number, axis: "vertical" | "horizontal") {
if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) {