Skip to content

Commit a9278f6

Browse files
committed
fix(plugin-grid): fold inline row-action buttons into the row menu while the table overflows with them
Claude-Session: https://claude.ai/code/session_01FEkQ5osPMA12JX9fGcBzR3 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bd439f1 commit a9278f6

3 files changed

Lines changed: 155 additions & 4 deletions

File tree

‎ui/packages/plugin-grid/src/ObjectGrid.tsx‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import { useObjectGridMobile } from './objectGridMobile';
102102
import { useObjectGridGenerateColumns } from './objectGridGenerateColumns';
103103
import { resolveObjectGridPersistedColumns } from './objectGridPersistedColumns';
104104
import { resolveObjectGridActionsColumn } from './objectGridActionsColumn';
105+
import { useInlineRowActionFold } from './objectGridInlineActionFold';
105106
import { BULK_SELECT_ALL_MATCHING_CAP } from './bulkSelectAllMatchingCap';
106107
export type { RetiredListColumnKey, ObjectGridColumnHolds, ObjectGridColumnDraft, ObjectGridColumn } from './objectGridColumns';
107108
export type { ObjectGridColumnState, ObjectGridFooterSummary, ObjectGridExternalPaginationProps, ObjectGridRowGroup } from './objectGridProps';
@@ -757,6 +758,13 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
757758
// hook order (`./objectGridColumnState`).
758759
const { columnState, saveColumnState } = useObjectGridColumnState({ schema, onColumnStateChange });
759760

761+
// objectstack#33230 — the inline primary row actions fold into the row's
762+
// "⋮" menu while the table overflows its scroll box with them, so the
763+
// pinned Actions column never widens over a data column for them
764+
// (`./objectGridInlineActionFold`). Read off the flat table's scroll box;
765+
// read afresh when the rows or the columns change.
766+
const inlineActionFold = useInlineRowActionFold(data, JSON.stringify([schema.columns ?? null, columnState ?? null]));
767+
760768
// Pull to refresh and the card-view breakpoint, in hook order (`./objectGridMobile`).
761769
const { pullRef, isRefreshing, pullDistance } = useObjectGridMobile({
762770
schema, dataSource, setRefreshKey, setUseCardView,
@@ -1716,7 +1724,7 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
17161724
schema, data, objectSchema, persistedColumns, operations, resolvedRowActionDefs,
17171725
wantEditAction, wantDeleteAction, mayInvokeRowAction, rowOperations, canEdit, canDelete,
17181726
recordVerdict, predicateScope, onEdit, onDelete, editPredicates, deletePredicates, executeAction,
1719-
setRefreshKey, t,
1727+
setRefreshKey, t, foldInlineRowActions: inlineActionFold.folded,
17201728
});
17211729

17221730
// Density-driven cell padding/font (applied to every column so it actually reaches <td>).
@@ -3850,7 +3858,11 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
38503858
</div>
38513859
) : (
38523860
<div className="flex flex-col flex-1 min-h-0">
3853-
<div className="flex flex-col flex-1 min-h-0">
3861+
<div
3862+
ref={inlineActionFold.hostRef}
3863+
className="flex flex-col flex-1 min-h-0"
3864+
data-row-actions-inline={inlineActionFold.folded ? 'folded' : undefined}
3865+
>
38543866
<SchemaRenderer schema={dataTableSchema} />
38553867
</div>
38563868
{summaryFooter}

‎ui/packages/plugin-grid/src/objectGridActionsColumn.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function resolveObjectGridActionsColumn({
3030
schema, data, objectSchema, persistedColumns, operations, resolvedRowActionDefs,
3131
wantEditAction, wantDeleteAction, mayInvokeRowAction, rowOperations, canEdit, canDelete,
3232
recordVerdict, predicateScope, onEdit, onDelete, editPredicates, deletePredicates, executeAction,
33-
setRefreshKey, t,
33+
setRefreshKey, t, foldInlineRowActions = false,
3434
}: {
3535
schema: ObjectGridSchema;
3636
data: any[];
@@ -53,6 +53,13 @@ export function resolveObjectGridActionsColumn({
5353
executeAction: ReturnType<typeof useAction>['execute'];
5454
setRefreshKey: React.Dispatch<React.SetStateAction<number>>;
5555
t: ReturnType<typeof useGridTranslation>['t'];
56+
/**
57+
* objectstack#33230 — draw no inline button: every primary action renders at
58+
* the top of its row's "⋮" menu instead. `ObjectGrid` sets it while the
59+
* table overflows its scroll box with the buttons drawn
60+
* (`./objectGridInlineActionFold`).
61+
*/
62+
foldInlineRowActions?: boolean;
5663
}) {
5764
// `operations`, the row-action lists and the row CRUD affordance verdict are
5865
// resolved further up, next to the permission reads they AND with — this
@@ -70,7 +77,7 @@ export function resolveObjectGridActionsColumn({
7077
// rows that offer nothing. One row is enough, so the read stops at the first.
7178
const rowActionDefsGated = (resolvedRowActionDefs as any[]).filter((d) => mayInvokeRowAction(d?.requiredPermissions) && !isActionSwitchedOff(d));
7279
// NON-AUTHOR SURFACE — see the `maxInlineActions` prop below.
73-
const maxInlineRowActions: number = (schema as any).maxInlineRowActions ?? 1;
80+
const maxInlineRowActions: number = foldInlineRowActions ? 0 : ((schema as any).maxInlineRowActions ?? 1);
7481
const rowCrudVerdicts = (row: any) => {
7582
// [objectui#8674] The CALLER's answer for this one row, ANDed in below.
7683
const rowOps = rowOperations?.(row);
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Whether `ObjectGrid` folds its inline primary row actions into the row's
3+
* "⋮" menu (objectstack#33230).
4+
*
5+
* The Actions column is pinned to the scroll box's right edge
6+
* (`./objectGridActionsColumn`), so while the table overflows the box the
7+
* column sits over whichever data column reaches that edge at rest. An inline
8+
* primary button roughly doubles the column (one button beside the "⋮"
9+
* trigger), and in a narrow host — a related list enlarged inside a record
10+
* drawer — that width alone can tip a table whose data columns fit into one
11+
* that overflows, and put the pinned column over the last data column.
12+
*
13+
* The rule: inline buttons are drawn only while the table fits its scroll box
14+
* with them. A table that overflows with them folds them: each primary action
15+
* moves to the top of its row's "⋮" menu (`planRowActionMenu`'s inline budget
16+
* of 0), so no action is dropped, and the pinned column shrinks to the
17+
* trigger. The buttons come back once the box is as wide as the table was with
18+
* them. Where the table still overflows menu-only, the narrower pinned column
19+
* still sits over the data at the box's edge, as on any list that scrolls
20+
* sideways.
21+
*
22+
* Every reading is taken once the table's size has held for two frames: the
23+
* table re-sizes its columns from what their cells drew over several passes,
24+
* and a decision taken mid-pass would read a width the table never settles at.
25+
*/
26+
27+
import * as React from 'react';
28+
29+
/** Sub-pixel rounding is not an overflow. */
30+
const OVERFLOW_SLACK_PX = 1;
31+
/** Frames the table's size must hold before it is read. */
32+
const SETTLED_FRAMES = 2;
33+
34+
/**
35+
* Reads the grid's table scroll box under the element `hostRef` is attached
36+
* to and answers whether the inline row actions fold. Starts unfolded, and
37+
* starts again unfolded whenever `rows` or `columnsKey` change.
38+
*/
39+
export function useInlineRowActionFold(rows: unknown, columnsKey: string): {
40+
hostRef: (element: HTMLElement | null) => void;
41+
folded: boolean;
42+
} {
43+
const [host, setHost] = React.useState<HTMLElement | null>(null);
44+
const [folded, setFolded] = React.useState(false);
45+
// The decision the readings act on, written only where it is made.
46+
const foldedRef = React.useRef(false);
47+
// While folded: the table's width with the buttons, read when it folded.
48+
const unfoldedWidthRef = React.useRef(0);
49+
50+
React.useLayoutEffect(() => {
51+
unfoldedWidthRef.current = 0;
52+
foldedRef.current = false;
53+
setFolded(false);
54+
}, [rows, columnsKey]);
55+
56+
React.useLayoutEffect(() => {
57+
if (!host || typeof window === 'undefined') return undefined;
58+
let resizes: ResizeObserver | null = null;
59+
let mounts: MutationObserver | null = null;
60+
let watched: HTMLElement | null = null;
61+
let frame = 0;
62+
let lastSize = '';
63+
let heldFrames = 0;
64+
65+
const decide = (next: boolean) => {
66+
if (next === foldedRef.current) return;
67+
foldedRef.current = next;
68+
setFolded(next);
69+
};
70+
const read = (box: HTMLElement) => {
71+
const boxWidth = box.clientWidth;
72+
if (boxWidth <= 0) return;
73+
if (foldedRef.current) {
74+
if (boxWidth >= unfoldedWidthRef.current) decide(false);
75+
return;
76+
}
77+
const tableWidth = box.scrollWidth;
78+
if (tableWidth - boxWidth <= OVERFLOW_SLACK_PX) return;
79+
if (!box.querySelector('[data-testid^="row-action-inline-"]')) return;
80+
unfoldedWidthRef.current = tableWidth;
81+
decide(true);
82+
};
83+
const tick = () => {
84+
frame = 0;
85+
const box = watched;
86+
if (!box || !box.isConnected) return;
87+
const size = `${box.clientWidth}:${box.scrollWidth}`;
88+
heldFrames = size === lastSize ? heldFrames + 1 : 0;
89+
lastSize = size;
90+
if (heldFrames >= SETTLED_FRAMES) {
91+
read(box);
92+
return;
93+
}
94+
frame = window.requestAnimationFrame(tick);
95+
};
96+
const schedule = () => {
97+
heldFrames = 0;
98+
if (!frame) frame = window.requestAnimationFrame(tick);
99+
};
100+
const watch = (box: HTMLElement) => {
101+
watched = box;
102+
schedule();
103+
if (typeof ResizeObserver === 'undefined') return;
104+
resizes = new ResizeObserver(schedule);
105+
resizes.observe(box);
106+
const table = box.querySelector('table');
107+
if (table) resizes.observe(table);
108+
};
109+
const findBox = () => host.querySelector<HTMLElement>('[data-table-scroll-box]');
110+
const box = findBox();
111+
if (box) {
112+
watch(box);
113+
} else if (typeof MutationObserver !== 'undefined') {
114+
// The table can mount after the grid does: wait for its scroll box.
115+
mounts = new MutationObserver(() => {
116+
const found = findBox();
117+
if (!found) return;
118+
mounts?.disconnect();
119+
mounts = null;
120+
watch(found);
121+
});
122+
mounts.observe(host, { childList: true, subtree: true });
123+
}
124+
return () => {
125+
if (frame) window.cancelAnimationFrame(frame);
126+
mounts?.disconnect();
127+
resizes?.disconnect();
128+
};
129+
}, [host]);
130+
131+
return { hostRef: setHost, folded };
132+
}

0 commit comments

Comments
 (0)