Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/collections/board: use a slide animation for reordering columns
Adorian Doran48a327ed37d6
diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.cssindex 52f538d71c..0636389e4a 100644--- a/apps/client/src/widgets/collections/board/index.css+++ b/apps/client/src/widgets/collections/board/index.css@@ -89,9 +89,9 @@ body.mobile .board-view-container.board-dragging {/* A column with no colour of its own is painted rather than composed: the tinted shades comefrom one lightness, which does not answer for a plain one as well. */background-color: var(--board-column-background);- /* Must stay 0s: the transition that runs is the one carried after the class changes, so this is- what makes opening instant while `.collapsed` below eases the close. */- transition: border-color 0.2s ease, width 0s;+ /* Width must stay 0s: the transition that runs is the one carried after the class changes, so+ this is what makes opening instant while `.collapsed` below eases the close. */+ transition: border-color 0.2s ease, transform 0.2s ease, width 0s;overflow-y: auto;max-height: 100%;display: flex;@@ -666,7 +666,8 @@ body.mobile .board-view-container .board-add-column {width: var(--board-collapsed-column-width);overflow: hidden;/* Width and not a transform: the columns beside it have to move with it. */- transition: border-color 0.2s ease, width var(--board-collapse-duration) ease;+ transition: border-color 0.2s ease, transform 0.2s ease,+ width var(--board-collapse-duration) ease;}@media (prefers-reduced-motion: reduce) {diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex 88b0a6c35f..96456f1fa0 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -27,6 +27,7 @@ import NoteAutocomplete from "../../react/NoteAutocomplete";import ShortcutHintButton from "../../shortcut_hints/shortcut_hint_button";import { onWheelHorizontalScroll } from "../../widget_utils";import { useDragPan } from "../../react/drag_pan";+import { useFlip } from "../../react/flip";import { ViewModeProps } from "../interface";import Api, { getPendingWrites, PendingColumnWrites, settleColumn } from "./api";import { useBoardDrag } from "./board_drag";@@ -450,6 +451,12 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC// is left to whatever it belongs to. Suppressed while a card is carried: the gesture owns the// pointer, and the board must not slide under it.const { isPannable, isPanning } = useDragPan(containerRef, { disabled: isDraggingItem });+ // The gap a carried column opens moves the columns beside it, which they follow rather than+ // jump to. The copy being carried is left out: the gesture writes its transform every frame.+ useFlip(containerRef, {+ selector: ".board-column:not(.board-drag-preview), .board-add-column",+ axis: "horizontal"+ });/*** Brings a column to the middle of the screen, for a board that scrolls one column at a time.diff --git a/apps/client/src/widgets/react/flip.spec.tsx b/apps/client/src/widgets/react/flip.spec.tsxindex 0b50e64ee2..8f6b4f45f6 100644--- a/apps/client/src/widgets/react/flip.spec.tsx+++ b/apps/client/src/widgets/react/flip.spec.tsx@@ -67,6 +67,17 @@ describe("useFlip", () => {expect(items()[1].style.transform).toBe("");});+ it("carries a child sideways where that is the way they move", async () => {+ const { items } = await draw([ "a", "b" ], false, "horizontal");+ place(items(), [ 0, 180 ], "offsetLeft");+ await redraw();++ place(items(), [ 0, 360 ], "offsetLeft");+ await redraw();++ expect(items()[1].style.transform).toBe("translateX(-180px)");+ });+it("says nothing while it is switched off", async () => {const { items } = await draw([ "a", "b" ], true);place(items(), [ 0, 40 ]);@@ -79,14 +90,16 @@ describe("useFlip", () => {let rerender: () => Promise<void>;- async function draw(keys: string[], disabled = false) {+ async function draw(+ keys: string[], disabled = false, axis: "vertical" | "horizontal" = "vertical"+ ) {const mountPoint = document.createElement("div");container = mountPoint;document.body.appendChild(mountPoint);function Harness() {const ref = useRef<HTMLDivElement>(null);- useFlip(ref, { selector: ".item", disabled });+ useFlip(ref, { selector: ".item", axis, disabled });return (<div ref={ref}>{keys.map(key => <div key={key} className="item">{key}</div>)}@@ -107,9 +120,11 @@ describe("useFlip", () => {}/** happy-dom reports no offsets of its own, so each child is told where it stands. */- function place(items: HTMLElement[], tops: number[]) {+ function place(+ items: HTMLElement[], tops: number[], property: "offsetTop" | "offsetLeft" = "offsetTop"+ ) {for (const [ index, item ] of items.entries()) {- Object.defineProperty(item, "offsetTop", {+ Object.defineProperty(item, property, {value: tops[index], configurable: true, writable: true});if (!Object.getOwnPropertyDescriptor(item, "offsetParent")) {diff --git a/apps/client/src/widgets/react/flip.ts b/apps/client/src/widgets/react/flip.tsindex 3f5792abc8..c1eb931231 100644--- a/apps/client/src/widgets/react/flip.ts+++ b/apps/client/src/widgets/react/flip.ts@@ -4,6 +4,8 @@ import { useLayoutEffect, useRef } from "preact/hooks";export interface FlipOptions {/** Which children to follow. */selector: string;+ /** Which way they move. Defaults to down the container. */+ axis?: "vertical" | "horizontal";/** Whether to leave them where the browser puts them. */disabled?: boolean;}@@ -15,10 +17,12 @@ export interface FlipOptions {* 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.+ * Positions are read as an offset from the page, 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) {+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>());@@ -42,31 +46,33 @@ export function useFlip(ref: RefObject<HTMLElement>, { selector, disabled }: Flicontinue;}- const top = child.offsetTop;- now.set(child, top);+ const at = axis === "vertical" ? child.offsetTop : child.offsetLeft;+ now.set(child, at);const previous = before.get(child);- if (!disabled && previous !== undefined && Math.abs(previous - top) >= 1) {- moved.push({ child, by: previous - top });+ if (!disabled && previous !== undefined && Math.abs(previous - at) >= 1) {+ moved.push({ child, by: previous - at });}}seen.current = now;for (const { child, by } of moved) {- slide(child, by);+ slide(child, by, axis);}});}/** Puts a child back where it was, and lets go of it on the next frame. */-function slide(element: HTMLElement, offset: number) {+function slide(element: HTMLElement, offset: number, axis: "vertical" | "horizontal") {if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) {return;}element.style.transition = "none";- element.style.transform = `translateY(${offset}px)`;+ element.style.transform = axis === "vertical"+ ? `translateY(${offset}px)`+ : `translateX(${offset}px)`;requestAnimationFrame(() => {if (element.isConnected) {