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: add animation for the expanding columns
Adorian Doran6f37d44e4501
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 14881fff02..23d79581fe 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -26,6 +26,9 @@ import { useScrollFade } from "../../react/scroll_fade";* How long `footerQuietUntil` holds for a card `AddNewItem` is making, if the write never answers.*/const FOOTER_QUIET_MS = 5000;++/** How long an open takes. Matches `--board-expand-duration` in the board's own rules. */+const EXPAND_MS = 200;import NoteLink from "../../react/NoteLink";import { BoardActionsContext, BoardDragStateContext, TitleEditor } from ".";import BoardApi from "./api";@@ -146,6 +149,34 @@ export default function Column({// Read here rather than in the badge: the column body shows an outline as well.const isOverLimit = limit !== undefined && (columnItems?.length ?? 0) > limit;const isCollapsed = !!collapsed && !isActive;+ // A column opened to take a dragged card takes its width at once, and its cards with it.+ const opensAtOnce = !!draggedCard || dropTarget === column;++ /**+ * Whether the column is still widening, during which its cards are left unpainted.+ *+ * They are laid out again on every frame of the widening, their titles rewrapping as the+ * column grows, which is what the reader would otherwise watch. Read during the render that+ * opens the column, so there is no frame where they are painted into a narrow one.+ *+ * Unpainted rather than undrawn: the board focuses the card a keyboard open steps onto, and a+ * card that is not there yet is one it cannot hand focus to.+ */+ const [ isExpanding, setIsExpanding ] = useState(false);+ const [ wasCollapsed, setWasCollapsed ] = useState(isCollapsed);+ if (wasCollapsed !== isCollapsed) {+ setWasCollapsed(isCollapsed);+ setIsExpanding(!isCollapsed && !opensAtOnce);+ }++ useEffect(() => {+ if (!isExpanding) {+ return;+ }++ const timer = window.setTimeout(() => setIsExpanding(false), EXPAND_MS);+ return () => window.clearTimeout(timer);+ }, [ isExpanding ]);// Only while the column is open: the strip's own press opens it, which is what it says// instead. Memoised because `useStaticTooltip` rebuilds the tooltip on a new config.@@ -282,6 +313,10 @@ export default function Column({"over-limit": isOverLimit,collapsed: isCollapsed,"quick-collapse": isCollapsingByHand,+ // Opening is drawn for the reader who asked for it. A column opened to take a+ // dragged card takes its width at once, since the drop is measured as it opens.+ "quick-expand": !isCollapsed && !opensAtOnce,+ expanding: isExpanding,appearing: isNew && !isRevealed})}onAnimationEnd={(e) => {diff --git a/apps/client/src/widgets/collections/board/drag.spec.tsx b/apps/client/src/widgets/collections/board/drag.spec.tsxindex c9659d59a9..994e0a9071 100644--- a/apps/client/src/widgets/collections/board/drag.spec.tsx+++ b/apps/client/src/widgets/collections/board/drag.spec.tsx@@ -160,6 +160,9 @@ describe("Board drag and drop", () => {await drag(columns[0], "dragover", { types: [ TREE_CLIPBOARD_TYPE ] }, 50);expect(columns[0].classList.contains("collapsed")).toBe(false);expect(columns[0].querySelectorAll(".board-note")).toHaveLength(2);+ // At its full width at once, cards and all: the drop is measured as the column opens.+ expect(columns[0].classList.contains("quick-expand")).toBe(false);+ expect(columns[0].classList.contains("expanding")).toBe(false);await drag(columns[0], "dragleave", { types: [ TREE_CLIPBOARD_TYPE ] });expect(columns[0].classList.contains("collapsed")).toBe(false);diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.cssindex 7a40f297d6..15b27e7c66 100644--- a/apps/client/src/widgets/collections/board/index.css+++ b/apps/client/src/widgets/collections/board/index.css@@ -16,8 +16,11 @@/* Matches the height a normal header takes, so a collapsed column reads as one turned onits side. */--board-collapsed-column-width: 2.25em;+ --board-column-width: 250px;+ --board-column-border-width: 2px;--board-collapse-duration: 1s;--board-collapse-duration-quick: 0.3s;+ --board-expand-duration: 0.2s;--board-focus-outline-saturation: 55%;--board-focus-outline-lightness: 45%;@@ -83,15 +86,16 @@ body.mobile .board-view-container.board-dragging {}.board-view-container .board-column {- width: 250px;+ width: var(--board-column-width);flex-shrink: 0;- border: 2px solid transparent;+ border: var(--board-column-border-width) solid transparent;border-radius: 8px;/* 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);- /* 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. */+ /* Width stays 0s here: the transition that runs is the one carried after the class changes, so+ this is what a column opened to take a dragged card takes, `.collapsed` below easing the close+ and `.quick-expand` the opens the reader asks for. */transition: border-color 0.2s ease, transform 0.2s ease, width 0s;overflow-y: auto;max-height: 100%;@@ -100,8 +104,7 @@ body.mobile .board-view-container.board-dragging {}body.mobile .board-view-container .board-column {- width: 75vw;- max-width: 300px;+ --board-column-width: min(75vw, 300px);}body.mobile .board-view-container .board-column,@@ -625,7 +628,9 @@ body.mobile .column-drop-placeholder {border-radius: 5px;color: var(--muted-text-color);cursor: pointer;- transition: border-color 0.2s ease, color 0.2s ease, background-color 0.2s ease;+ transition: border-color 0.2s ease, color 0.2s ease, background-color 0.2s ease,+ /* Matching the cards above, which fade in beside it. */+ opacity 0.15s ease;background-color: transparent;font-size: var(--card-font-size);}@@ -740,11 +745,53 @@ body.mobile .column-drop-placeholder {--board-collapse-duration: var(--board-collapse-duration-quick);}+/* Opening runs the reverse of that collapse. Left out while a card is carried: a column opened to+ take one is measured as it opens, and a width still on its way misplaces the gap. */+.board-view-container .board-column.quick-expand {+ transition: border-color 0.2s ease, transform 0.2s ease,+ width var(--board-expand-duration) ease;+}++/*+ * The column's contents are pinned at the width it is opening to, and clipped until it gets there.+ *+ * Left to stretch, they are laid out again on every frame of the widening: the titles rewrap, which+ * costs a full layout of every card in the column, and a column of a hundred cards cannot keep up.+ * The height goes with it, since cards a strip wide are tall enough to take the column to its+ * limit until they rewrap, and so do the places `useFlip` reads, which turns the settled layout+ * into a slide.+ *+ * They are painted only once the width has arrived. Held in the page rather than left out of it:+ * the board hands focus to the card a keyboard open steps onto.+ */+.board-view-container .board-column.expanding {+ overflow: hidden;+}++.board-view-container .board-column.expanding > * {+ width: calc(var(--board-column-width) - 2 * var(--board-column-border-width));+}++/* The heading stays where it is, and is revealed with the column; only the cards wait. They fade+ in as the class is dropped, through the transition each of them already carries: the scrolling+ body cannot, its own `transition` being the scroll fade's. */+.board-view-container .board-column.expanding .board-note,+.board-view-container .board-column.expanding > .board-new-item {+ opacity: 0;+}+@media (prefers-reduced-motion: reduce) {- /* Matching the selector above, which it has to outweigh rather than merely follow. */- .board-view-container .board-column.collapsed:not(.board-drag-preview) {+ /* Matching the selectors above, which these have to outweigh rather than merely follow. */+ .board-view-container .board-column.collapsed:not(.board-drag-preview),+ .board-view-container .board-column.quick-expand {transition: border-color 0.2s ease;}++ /* The column is at its width already, so there is nothing for the cards to wait for. */+ .board-view-container .board-column.expanding .board-note,+ .board-view-container .board-column.expanding > .board-new-item {+ opacity: 1;+ }}/* On the column, not the header: `overflow: hidden` above would clip a descendant's outline. */diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex 83b432dfca..a2fdbe2a9a 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -340,6 +340,34 @@ describe("Collapsed board columns", () => {expect(isCollapsed(mountPoint, 0)).toBe(false);});+ /**+ * The cards are laid out again on every frame of the widening, so they are held unpainted+ * until it is over. A column opened to take a dragged card shows them at once.+ */+ it("holds the cards unpainted while the column widens", async () => {+ const { mountPoint } = await setup();++ await select(mountPoint, 0);+ const column = columnAt(mountPoint, 0);+ expect(column.classList.contains("expanding")).toBe(true);+ // Drawn all the same, so the board can hand focus to one of them.+ expect(cardCount(mountPoint, 0)).toBe(2);++ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 350));+ });+ expect(columnAt(mountPoint, 0).classList.contains("expanding")).toBe(false);+ });++ /** The reverse of the collapse, for the open the reader asked for. */+ it("marks an open the reader asked for", async () => {+ const { mountPoint } = await setup();+ expect(columnAt(mountPoint, 0).classList.contains("quick-expand")).toBe(false);++ await select(mountPoint, 0);+ expect(columnAt(mountPoint, 0).classList.contains("quick-expand")).toBe(true);+ });+/*** A peek closes behind the pointer, with the columns beside it shifting under it, so it eases* shut; a collapse the reader asked for surprises nobody and runs faster.diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsxindex d41467425f..9c696968fc 100644--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx@@ -902,4 +902,5 @@ describe("Board keyboard", () => {function flush() {return new Promise((resolve) => setTimeout(resolve));}+});