|
| 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