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
fix(table): grow a select editor upwards where the room below has run out
Elian Doran74c041b3b0ca
diff --git a/apps/client/src/widgets/collections/table/columns.css b/apps/client/src/widgets/collections/table/columns.cssindex 209a391d83..7c461b13c0 100644--- a/apps/client/src/widgets/collections/table/columns.css+++ b/apps/client/src/widgets/collections/table/columns.css@@ -64,3 +64,10 @@background-color: var(--input-background-color);}}++/* Anchored by its foot instead, so that what it gains rises over the rows above rather than pressing+ past the foot of the pane. Applied from the editor, which is what can tell the room is short. */+.table-select-editor.grows-upwards {+ inset-block-start: auto;+ inset-block-end: 0;+}diff --git a/apps/client/src/widgets/collections/table/columns.tsx b/apps/client/src/widgets/collections/table/columns.tsxindex 0ba163cc0f..77e3d430d5 100644--- a/apps/client/src/widgets/collections/table/columns.tsx+++ b/apps/client/src/widgets/collections/table/columns.tsx@@ -1,6 +1,7 @@import "./columns.css";import { LabelType } from "@triliumnext/commons";+import clsx from "clsx";import { JSX } from "preact";import { useEffect, useRef, useState } from "preact/hooks";import type { CellComponent, ColumnDefinition, EmptyCallback, FormatterParams, RowComponent, ValueBooleanCallback, ValueVoidCallback } from "tabulator-tables";@@ -13,6 +14,7 @@ import { SelectValuesInput } from "../../attribute_widgets/select_input.jsx";import Icon from "../../react/Icon.jsx";import NoteAutocomplete from "../../react/NoteAutocomplete.jsx";import { renderReactWidget } from "../../react/react_utils.jsx";+import { useGrowsUpwards } from "./grows_upwards.js";type ColumnType = LabelType | "relation";@@ -369,8 +371,12 @@ function SelectEditor({ cell, success, editorParams }: EditorOpts) {return () => editor.removeEventListener("focusout", onFocusOut);}, [ isMulti, success ]);+ // The editor grows downwards as chips are taken, which the pane the table scrolls in cuts off at+ // its foot — on the last row there is nothing below the cell to grow into.+ const growsUpwards = useGrowsUpwards(containerRef);+return (- <div ref={containerRef} className="table-select-editor">+ <div ref={containerRef} className={clsx("table-select-editor", growsUpwards && "grows-upwards")}>{isMulti ? (<SelectValuesInputoptions={options}diff --git a/apps/client/src/widgets/collections/table/grows_upwards.spec.tsx b/apps/client/src/widgets/collections/table/grows_upwards.spec.tsxnew file mode 100644index 0000000000..a26debc60a--- /dev/null+++ b/apps/client/src/widgets/collections/table/grows_upwards.spec.tsx@@ -0,0 +1,123 @@+import { render } from "preact";+import { useRef } from "preact/hooks";+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import { useGrowsUpwards } from "./grows_upwards";++describe("useGrowsUpwards", () => {+ /** A pane that scrolls, the thing being stood on, and the overlay measured against them. */+ let pane: HTMLElement;+ let anchor: HTMLElement;+ let overlay: HTMLElement | null;+ /** The observer's callback, this being how the element says it has changed size. */+ let sizeChanged: (() => void) | undefined;+ let growsUpwards: boolean | undefined;++ function Probe() {+ const ref = useRef<HTMLDivElement>(null);+ growsUpwards = useGrowsUpwards(ref);+ return <div ref={ref} />;+ }++ beforeEach(() => {+ vi.stubGlobal("ResizeObserver", class {+ constructor(callback: () => void) {+ sizeChanged = callback;+ }+ observe() {}+ disconnect() {}+ });++ pane = document.createElement("div");+ // What makes it the pane: the walk upwards stops at the first thing that cuts off overflow.+ pane.style.overflowY = "auto";+ anchor = document.createElement("div");+ pane.appendChild(anchor);+ document.body.appendChild(pane);++ growsUpwards = undefined;+ sizeChanged = undefined;+ overlay = null;+ });++ afterEach(() => {+ render(null, anchor);+ pane.remove();+ vi.unstubAllGlobals();+ });++ // Nothing is laid out under happy-dom, so each test states the geometry it is describing: where+ // the pane and the thing being stood on are, and how tall the overlay has grown.+ function spans(element: HTMLElement, top: number, bottom: number) {+ element.getBoundingClientRect = () => ({ top, bottom, height: bottom - top }) as DOMRect;+ }++ function standsAt({ paneTop = 0, paneBottom = 500, anchorTop = 100, anchorBottom = 130, height = 60 }) {+ spans(pane, paneTop, paneBottom);+ spans(anchor, anchorTop, anchorBottom);+ if (overlay) {+ Object.defineProperty(overlay, "offsetHeight", { value: height, configurable: true });+ }+ }++ async function mount(geometry: Parameters<typeof standsAt>[0]) {+ standsAt(geometry);+ await act(async () => render(<Probe />, anchor));+ overlay = anchor.querySelector("div");+ // The overlay only exists once rendered, so its height is given now and measured again.+ standsAt(geometry);+ await act(async () => sizeChanged?.());+ }++ it("stands as it is while there is room below it, and turns round once there is not", async () => {+ // 60 tall from a cell at 100 reaches 160, well inside a pane ending at 500.+ await mount({ paneBottom: 500 });+ expect(growsUpwards).toBe(false);++ // Scrolled until the pane's foot is above where the overlay would reach: anchored by its own+ // foot instead, it runs from 70 to 130 — over the rows above, inside the pane.+ spans(pane, 0, 140);+ await act(async () => {+ pane.dispatchEvent(new Event("scroll"));+ });+ expect(growsUpwards).toBe(true);+ });++ it("stays as it is where it would fit neither way", async () => {+ // A pane spanning 100..140 is shorter than the overlay, so turning round would only trade+ // one clipped end for the other — and the head is the end worth showing.+ await mount({ paneTop: 100, paneBottom: 140, anchorTop: 100, anchorBottom: 130, height: 60 });+ expect(growsUpwards).toBe(false);+ });++ it("measures again as the overlay grows, a line at a time", async () => {+ await mount({ paneBottom: 200, height: 60 });+ expect(growsUpwards).toBe(false);++ // A chip taken wraps onto another line, and now it reaches past the pane's foot.+ Object.defineProperty(overlay, "offsetHeight", { value: 120, configurable: true });+ await act(async () => sizeChanged?.());+ expect(growsUpwards).toBe(true);+ });++ it("waits for the overlay to be put in the document before deciding anything", async () => {+ // Tabulator builds an editor and hands it over, putting it in a cell only afterwards — so+ // there is a moment where nothing above it says what room there is.+ const detached = document.createElement("div");+ spans(detached, 100, 130);+ await act(async () => render(<Probe />, detached));+ overlay = detached.querySelector("div");+ standsAt({ paneBottom: 140 });+ Object.defineProperty(overlay, "offsetHeight", { value: 60, configurable: true });+ await act(async () => sizeChanged?.());+ expect(growsUpwards).toBe(false);++ // Put in the pane at last, which is a change of size in itself.+ pane.appendChild(detached);+ await act(async () => sizeChanged?.());+ expect(growsUpwards).toBe(true);++ render(null, detached);+ });+});diff --git a/apps/client/src/widgets/collections/table/grows_upwards.ts b/apps/client/src/widgets/collections/table/grows_upwards.tsnew file mode 100644index 0000000000..9780928957--- /dev/null+++ b/apps/client/src/widgets/collections/table/grows_upwards.ts@@ -0,0 +1,67 @@+import type { RefObject } from "preact";+import { useLayoutEffect, useState } from "preact/hooks";++/**+ * Whether an element standing over its parent should be anchored by its foot rather than its head —+ * that is, whether it should grow upwards.+ *+ * Such an element is as tall as what it holds while the thing it stands on is a fixed height, so it+ * can outgrow the room beneath and be cut off by the pane it scrolls in — as a cell editor does on+ * the last row of a table. Where that happens and there is room the other way, it is better turned+ * round: what it gains then rises over what has been read already rather than pressing past the foot+ * of the pane. Where it fits neither way it is left alone, its head being the part worth showing.+ *+ * The caller does the anchoring, this being a question of layout rather than of markup: with the+ * answer it swaps which edge the element is pinned by.+ *+ * Measured afresh whenever the element changes size or anything scrolls, and read from the document+ * each time rather than held — an element may be built and handed to its host before being put in+ * the document at all, which is how Tabulator hands out its editors.+ */+export function useGrowsUpwards(ref: RefObject<HTMLElement>) {+ const [ growsUpwards, setGrowsUpwards ] = useState(false);++ useLayoutEffect(() => {+ const element = ref.current;+ if (!element) return;++ const measure = () => {+ const anchor = element.parentElement;+ const pane = clippingAncestor(element);+ if (!anchor || !pane) return;++ const anchorRect = anchor.getBoundingClientRect();+ const paneRect = pane.getBoundingClientRect();+ const height = element.offsetHeight;+ setGrowsUpwards(+ anchorRect.top + height > paneRect.bottom && anchorRect.bottom - height >= paneRect.top+ );+ };++ measure();+ // Which way it goes does not change its height, so measuring cannot chase its own answer.+ const resizes = new ResizeObserver(measure);+ resizes.observe(element);+ // Captured, since what scrolls is a pane within the page rather than the page itself.+ window.addEventListener("scroll", measure, true);+ return () => {+ resizes.disconnect();+ window.removeEventListener("scroll", measure, true);+ };+ }, [ ref ]);++ return growsUpwards;+}++/** The ways of saying that what does not fit is cut off rather than left to spill out. */+const CLIPPING_OVERFLOWS = new Set([ "auto", "scroll", "hidden", "clip", "overlay" ]);++/** The nearest ancestor that cuts off what overflows it, whose edges an overlay has to stay within. */+function clippingAncestor(element: HTMLElement) {+ for (let node = element.parentElement; node; node = node.parentElement) {+ if (CLIPPING_OVERFLOWS.has(getComputedStyle(node).overflowY)) {+ return node;+ }+ }+ return null;+}