import { CSSProperties, Key, RefObject } from 'react'; /** * Upper bound on remembered row measurements. Heights are cached per row key (not per * index) so filtering a list down and back keeps them usable — which means a list fed by * remote search accumulates an entry for every row it has ever shown. Past this many, the * cache is pruned to the rows currently in the list. */ export declare const DEFAULT_MAX_MEASURED_ROWS = 1000; /** * The slice of `react-window`'s `VariableSizeList` this hook drives. Typed structurally so * `@synerise/ds-utils` does not have to depend on `react-window`. */ export type VariableSizeListLike = { resetAfterIndex: (index: number, shouldForceUpdate?: boolean) => void; }; export type UseMeasuredRowHeightsOptions = { /** * Row identity per index. Measurements are keyed by this, so they survive filtering. A * key repeated within the list (one item shown both as a recent and in its section, say) * is told apart by its occurrence, so each row still keeps a height of its own. */ keys: readonly RowKey[]; /** * An existing list ref to drive instead of the hook's own. Pass one when something above * this call already needs the ref — `keys` usually depends on state declared late in a * component, and a `const { listRef } = useMeasuredRowHeights(...)` below that point is * out of reach for everything above it. */ listRef?: RefObject; /** Height to use for a row that has not reported one yet. Memoise it. */ estimate: (index: number) => number; /** * A value that changes when the *meaning* of `estimate` changes (e.g. a `rowHeight` * prop). Every cached measurement was taken against the old estimate, so a change here * clears them all. Leave it out when only the row set changes — pruning on every * keystroke would throw away exactly the measurements a narrowing search is about to * need again. */ estimateVersion?: unknown; maxCachedRows?: number; }; export type UseMeasuredRowHeightsResult = { /** Attach to the `VariableSizeList` so measurements can invalidate its offset memo. */ listRef: RefObject; /** Pass as the list's `itemSize`. */ getItemSize: (index: number) => number; /** Report a row's real height. Pair with `useMeasuredRow` on the row itself. */ measureRow: (index: number, height: number) => void; /** * Bumped once per settled batch of measurements. Include it in the deps of anything * derived from `getItemSize` outside the list — a total content height, say — which * would otherwise keep showing the estimate: `resetAfterIndex` re-renders the list, not * its parent. */ measurementVersion: number; }; /** * The style for a measured row's outer element. react-window's offset stays on it, the * height follows the content, and `minHeight` keeps the row at least as tall as it was * estimated. * * Pass the row's *estimate* as `minHeight`, never react-window's `style.height`: that one * comes from the cache, so a row pinned to it can never measure shorter than it once did. * A row whose key once stood for a taller one (an index key after the list changed, a * label that wrapped in a narrower container) would then keep the stale height for good. */ export declare const getMeasuredRowStyle: (style: CSSProperties, minHeight: number) => CSSProperties; /** * Lets a `VariableSizeList` honour content-driven row heights instead of a constant map: * rows report what they actually measure, and the list re-lays out around them. * * Two traps this encapsulates, both of them expensive to rediscover: * - react-window memoises row offsets, so a measured height only takes effect once the * list is told to drop that memo via `resetAfterIndex`. * - a row's own layout effect cannot reach `listRef` on the mount commit — rows are * descendants of the list, so their effects run before its ref is attached, and that * first `resetAfterIndex` is silently lost. Nothing guarantees another render will come * along to pick the cache up, so the flush effect below does it explicitly. */ export declare const useMeasuredRowHeights: ({ keys, estimate, estimateVersion, listRef: externalListRef, maxCachedRows, }: UseMeasuredRowHeightsOptions) => UseMeasuredRowHeightsResult; /** * The row half of {@link useMeasuredRowHeights}: returns a ref to put on the row's outer * element, and reports its height on every commit and whenever it later changes. Style * that element with {@link getMeasuredRowStyle}. * * The `ResizeObserver` is what covers content that settles after the first paint — an * image, a font swap, a narrower container wrapping a label. Without it such a row never * re-renders, so it would keep its stale height and overlap its neighbour. */ export declare const useMeasuredRow: (index: number, measureRow: (index: number, height: number) => void) => RefObject;