Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(frontend): fit a table to the floor of its scroller's exact width

zchpeterc98339a922fb
diff --git a/frontend/src/components/HumanizeTs.test.tsx b/frontend/src/components/HumanizeTs.test.tsx
index 6e803a3fea..1dd4d7a86d 100644
--- a/frontend/src/components/HumanizeTs.test.tsx
+++ b/frontend/src/components/HumanizeTs.test.tsx
@@ -165,17 +165,22 @@ describe("HumanizeTs", () => {
});
// Each piece of a fitted label, and whether it keeps its width or gives way.
- // A kept space must also be preformatted: a flex item drops the space at its
- // edge, and the cut time then runs into the date.
+ // A piece with a space at its edge must also render it: a flex item drops
+ // that space unless it is preformatted, and the cut time runs into the date.
const pieces = (box: Element | null) =>
Array.from(box?.children ?? []).map((piece) => {
+ const text = piece.textContent ?? "";
const fit = piece.classList.contains("truncate")
? "cut"
: piece.classList.contains("shrink-0")
? "kept"
: "loose";
- const pre = piece.classList.contains("whitespace-pre") ? " pre" : "";
- return `${piece.textContent}:${fit}${pre}`;
+ const edgeSpace = /^\s|\s$/.test(text)
+ ? piece.closest(".whitespace-pre")
+ ? " pre"
+ : " dropped"
+ : "";
+ return `${text}:${fit}${edgeSpace}`;
});
test.each([
diff --git a/frontend/src/components/HumanizeTs.tsx b/frontend/src/components/HumanizeTs.tsx
index a74124b797..dd30c8fd7b 100644
--- a/frontend/src/components/HumanizeTs.tsx
+++ b/frontend/src/components/HumanizeTs.tsx
@@ -36,8 +36,8 @@ interface HumanizeTsProps {
className?: string;
/**
* Keep the label on one line inside its box. Where the box is too narrow, a
- * date-time keeps its date whole and ellipsizes its time and zone; any
- * other label ellipsizes from its end.
+ * date-time that begins or ends with its date keeps the date whole and
+ * ellipsizes its time and zone; any other label ellipsizes from its end.
*/
truncate?: boolean;
/**
diff --git a/frontend/src/hooks/useColumnWidths.test.tsx b/frontend/src/hooks/useColumnWidths.test.tsx
index 3896b1101e..0968ab5b10 100644
--- a/frontend/src/hooks/useColumnWidths.test.tsx
+++ b/frontend/src/hooks/useColumnWidths.test.tsx
@@ -1,7 +1,11 @@
import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, describe, expect, test, vi } from "vitest";
-import { distributeColumnWidths, useColumnWidths } from "./useColumnWidths";
+import {
+ distributeColumnWidths,
+ fillableWidth,
+ useColumnWidths,
+} from "./useColumnWidths";
(
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
@@ -476,3 +480,23 @@ describe("distributeColumnWidths with a yield order", () => {
expect(distributeColumnWidths(narrowDefault, 350)).toEqual([120, 230]);
});
});
+
+describe("fillableWidth", () => {
+ // jsdom lays nothing out, so each case states what a browser reports.
+ const scroller = (rect: number, offset: number, client: number) => {
+ const node = document.createElement("div");
+ Object.defineProperty(node, "offsetWidth", { value: offset });
+ Object.defineProperty(node, "clientWidth", { value: client });
+ node.getBoundingClientRect = () => ({ width: rect }) as DOMRect;
+ return node;
+ };
+
+ test.each([
+ ["a whole width, inside a 1px border", scroller(800, 800, 798), 798],
+ // 795.5 inside reads as 796; a table that wide scrolls by half a pixel.
+ ["a fractional width clientWidth rounds up", scroller(797.5, 798, 796), 795],
+ ["a width beside a vertical scrollbar", scroller(800, 800, 783), 783],
+ ])("fills %s without scrolling", (_, node, width) => {
+ expect(fillableWidth(node)).toBe(width);
+ });
+});
diff --git a/frontend/src/hooks/useColumnWidths.ts b/frontend/src/hooks/useColumnWidths.ts
index 1d77f5beea..41192e466f 100644
--- a/frontend/src/hooks/useColumnWidths.ts
+++ b/frontend/src/hooks/useColumnWidths.ts
@@ -92,6 +92,18 @@ export function distributeColumnWidths<
return widths;
}
+/**
+ * The width a table can take inside `scroller` without scrolling it: inside
+ * its border and any scrollbar, and floored, because `clientWidth` rounds and
+ * a table half a pixel wider than its scroller still scrolls.
+ */
+export function fillableWidth(scroller: HTMLElement): number {
+ const bordersAndScrollbar = scroller.offsetWidth - scroller.clientWidth;
+ return Math.floor(
+ scroller.getBoundingClientRect().width - bordersAndScrollbar
+ );
+}
+
// Starts every column at its default, or its floor if that is wider, and
// takes back what overruns the container a yield group at a time: within a
// group in proportion to how far each column can shrink.
diff --git a/frontend/src/modules/schema-editor/Panels/TableColumnEditor/TableColumnEditor.tsx b/frontend/src/modules/schema-editor/Panels/TableColumnEditor/TableColumnEditor.tsx
index a66f2f887e..5bdeb8385c 100644
--- a/frontend/src/modules/schema-editor/Panels/TableColumnEditor/TableColumnEditor.tsx
+++ b/frontend/src/modules/schema-editor/Panels/TableColumnEditor/TableColumnEditor.tsx
@@ -14,6 +14,7 @@ import {
} from "@/components/ui/table";
import {
distributeColumnWidths,
+ fillableWidth,
useColumnWidths,
} from "@/hooks/useColumnWidths";
import { cn } from "@/lib/utils";
@@ -292,7 +293,9 @@ export function TableColumnEditor({
const didFitRef = useRef(false);
useLayoutEffect(() => {
if (didFitRef.current) return;
- const width = containerRef.current?.clientWidth ?? 0;
+ const width = containerRef.current
+ ? fillableWidth(containerRef.current)
+ : 0;
if (width <= 0) return;
didFitRef.current = true;
setWidths(distributeColumnWidths(columnDefs, width));
diff --git a/frontend/src/modules/schema-editor/Panels/TableList/TableList.tsx b/frontend/src/modules/schema-editor/Panels/TableList/TableList.tsx
index fe0553d782..dfaa97337e 100644
--- a/frontend/src/modules/schema-editor/Panels/TableList/TableList.tsx
+++ b/frontend/src/modules/schema-editor/Panels/TableList/TableList.tsx
@@ -13,6 +13,7 @@ import {
} from "@/components/ui/table";
import {
distributeColumnWidths,
+ fillableWidth,
useColumnWidths,
} from "@/hooks/useColumnWidths";
import { cn } from "@/lib/utils";
@@ -150,7 +151,9 @@ export function TableList({
const didFitRef = useRef(false);
useLayoutEffect(() => {
if (didFitRef.current) return;
- const width = containerRef.current?.clientWidth ?? 0;
+ const width = containerRef.current
+ ? fillableWidth(containerRef.current)
+ : 0;
if (width <= 0) return;
didFitRef.current = true;
setWidths(distributeColumnWidths(columnDefs, width));
diff --git a/frontend/src/routes/project/ProjectAccessGrantsPage.tsx b/frontend/src/routes/project/ProjectAccessGrantsPage.tsx
index 0c8bcdd8dc..4008700406 100644
--- a/frontend/src/routes/project/ProjectAccessGrantsPage.tsx
+++ b/frontend/src/routes/project/ProjectAccessGrantsPage.tsx
@@ -47,6 +47,7 @@ import { useCurrentUser } from "@/hooks/useAppState";
import {
type ColumnWithWidth,
distributeColumnWidths,
+ fillableWidth,
useColumnWidths,
} from "@/hooks/useColumnWidths";
import { PagedTableFooter, usePagedData } from "@/hooks/usePagedData";
@@ -561,7 +562,7 @@ export function ProjectAccessGrantsPage({ projectId }: { projectId: string }) {
const fitTableContainer = useCallback(
(node: HTMLDivElement | null) => {
if (!node || didFitColumnsRef.current) return;
- const width = node.clientWidth;
+ const width = fillableWidth(node);
if (width <= 0) return;
didFitColumnsRef.current = true;
setWidths(distributeColumnWidths(columns, width));