feat(collections): virtual row layout with variable heights, headings, loader and gap/padding
ListBox virtualized mode grows a full row stream (padding spacers, data-driven heading rows, item rows, #loader row) with px semantics mapped from the pinned ListLayout contract: rowHeight may be a per-index function falling back to estimatedRowHeight, gap sits between content rows only, padding is end spacers, and every input is re-read per layout pass. Scroll ownership resolves to the list root or the nearest scrolling ancestor so popovers scroll as a whole with a sticky search field (data-virtualized hook; the historical 320px self-scroll rule stays for non-virtualized lists). Table gains the public useVirtualRows/bindVirtualRows pure geometry helpers (prefix sums + binary search, no state kernel): variable heights, estimated fallback, gap/padding and a loader reservation that stays consistent with the rendered spacers. headingHeight and dropIndicatorThickness remain unavailable (no sectioned header model, no drag-and-drop) and are recorded as such. Pinned demo values stay explicit (table 42px, list-box/autocomplete 50px); the table demo scrolls an outer viewport div because a native table cannot be a scroll container (verified: 300px height blown out to 45036px).
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
/**
|
||||
* Row geometry for table-shaped virtual windows. Pure measurement helpers —
|
||||
* no state kernel: selection, identity and focus stay with the existing
|
||||
* collection/table models. Heights are px; a row height function returning
|
||||
* `undefined` falls back to `estimatedRowHeight`, and every input is read on
|
||||
* each layout pass (prefix sums, binary search) so reactive values update the
|
||||
* window without a remount.
|
||||
*
|
||||
* Geometry semantics match the pinned ListLayout/TableLayout contracts: `gap`
|
||||
* is the spacing between rows (body rows and the trailing loader row), while
|
||||
* `padding` is list padding rendered as spacer rows at both ends and carries
|
||||
* no gap. The loader area is a reservation at the end of the row stream (after
|
||||
* the last row's gap and before the trailing padding): it is included in
|
||||
* `totalHeight` and in `window.padBottom`, so a caller rendering a physical
|
||||
* loader row should place it inside that reserved span.
|
||||
*/
|
||||
import {
|
||||
computed,
|
||||
shallowRef,
|
||||
toValue,
|
||||
type ComputedRef,
|
||||
type MaybeRefOrGetter,
|
||||
} from "vue";
|
||||
|
||||
export type VirtualRowHeight = number | ((index: number) => number | undefined);
|
||||
|
||||
export interface UseVirtualRowsOptions {
|
||||
/** Number of virtualized body rows. */
|
||||
count: MaybeRefOrGetter<number>;
|
||||
/** Row height in px; a function maps the row index to a variable height. */
|
||||
rowHeight: VirtualRowHeight;
|
||||
/** Fallback height in px for rows whose height function returns undefined. */
|
||||
estimatedRowHeight?: number;
|
||||
/** Loader row reservation in px at the end of the row stream. */
|
||||
loaderHeight?: number;
|
||||
/** Gap in px between rows (body rows and the trailing loader reservation). */
|
||||
gap?: number;
|
||||
/** List padding in px as spacer rows at both ends. */
|
||||
padding?: number;
|
||||
/** Extra rows rendered beyond the viewport. */
|
||||
overscan?: number;
|
||||
}
|
||||
|
||||
export interface VirtualRowWindow {
|
||||
/** First rendered body row index. */
|
||||
start: number;
|
||||
/** Body row index after the last rendered row. */
|
||||
end: number;
|
||||
/** Spacer height in px before the first rendered row (incl. padding). */
|
||||
padTop: number;
|
||||
/**
|
||||
* Spacer height in px after the last rendered row: the unrendered rows, the
|
||||
* loader reservation and the trailing padding.
|
||||
*/
|
||||
padBottom: number;
|
||||
}
|
||||
|
||||
export interface UseVirtualRows {
|
||||
window: ComputedRef<VirtualRowWindow>;
|
||||
/** Top offset in px of a body row within the row stream. */
|
||||
rowOffsetOf: (index: number) => number;
|
||||
/** Rendered extent in px of a body row (its height plus the gap after it). */
|
||||
rowExtentOf: (index: number) => number;
|
||||
/** Total row-stream height in px (padding, rows, gaps, loader reservation). */
|
||||
totalHeight: ComputedRef<number>;
|
||||
/** Recompute the window from a scroll position. */
|
||||
update: (scrollTop: number, viewportHeight: number) => void;
|
||||
/** Scroll a row into a viewport element, clamped to the row stream. */
|
||||
scrollToIndex: (element: HTMLElement, index: number) => void;
|
||||
}
|
||||
|
||||
export function useVirtualRows(options: UseVirtualRowsOptions): UseVirtualRows {
|
||||
const overscan = options.overscan ?? 8;
|
||||
const state = shallowRef({ scrollTop: 0, viewport: 0 });
|
||||
/** Prefix sums: offsets[i] = row stream top of body row i (incl. padding). */
|
||||
const geometry = computed(() => {
|
||||
const count = Math.max(0, toValue(options.count) ?? 0);
|
||||
const gap = (options.gap ?? 0) > 0 ? (options.gap ?? 0) : 0;
|
||||
const pad = (options.padding ?? 0) > 0 ? (options.padding ?? 0) : 0;
|
||||
const loader = (options.loaderHeight ?? 0) > 0 ? (options.loaderHeight ?? 0) : 0;
|
||||
// Rows and the loader reservation are content rows: gaps sit between
|
||||
// them, never between padding and content.
|
||||
const loaderSpan = loader > 0 ? (count > 0 ? gap : 0) + loader : 0;
|
||||
const offsets = new Float64Array(count + 1);
|
||||
let top = pad;
|
||||
for (let i = 0; i < count; i++) {
|
||||
offsets[i] = top;
|
||||
const raw =
|
||||
typeof options.rowHeight === "function"
|
||||
? options.rowHeight(i)
|
||||
: options.rowHeight;
|
||||
top += raw ?? options.estimatedRowHeight ?? 50;
|
||||
if (i < count - 1) top += gap;
|
||||
}
|
||||
offsets[count] = top;
|
||||
return { count, pad, loaderSpan, offsets };
|
||||
});
|
||||
const rowOffsetOf = (index: number) =>
|
||||
geometry.value.offsets[Math.min(Math.max(index, 0), geometry.value.count)];
|
||||
const rowExtentOf = (index: number) =>
|
||||
rowOffsetOf(index + 1) - rowOffsetOf(index);
|
||||
const totalHeight = computed(
|
||||
() =>
|
||||
geometry.value.offsets[geometry.value.count]! +
|
||||
geometry.value.loaderSpan +
|
||||
geometry.value.pad,
|
||||
);
|
||||
const window = computed<VirtualRowWindow>(() => {
|
||||
const { count, offsets } = geometry.value;
|
||||
const { scrollTop, viewport } = state.value;
|
||||
// First row whose extent reaches the viewport top.
|
||||
let low = 0;
|
||||
let high = count;
|
||||
while (low < high) {
|
||||
const mid = (low + high) >> 1;
|
||||
if (offsets[mid + 1]! <= scrollTop) low = mid + 1;
|
||||
else high = mid;
|
||||
}
|
||||
const start = Math.max(0, low - overscan);
|
||||
// First row starting beyond the viewport bottom.
|
||||
let endLow = start;
|
||||
let endHigh = count;
|
||||
const limit = scrollTop + viewport;
|
||||
while (endLow < endHigh) {
|
||||
const mid = (endLow + endHigh) >> 1;
|
||||
if (offsets[mid]! < limit) endLow = mid + 1;
|
||||
else endHigh = mid;
|
||||
}
|
||||
const end = Math.min(count, endLow + overscan);
|
||||
return {
|
||||
start,
|
||||
end,
|
||||
padTop: rowOffsetOf(start),
|
||||
padBottom: Math.max(0, totalHeight.value - rowOffsetOf(end)),
|
||||
};
|
||||
});
|
||||
function update(scrollTop: number, viewportHeight: number) {
|
||||
state.value = { scrollTop, viewport: viewportHeight };
|
||||
}
|
||||
function scrollToIndex(element: HTMLElement, index: number) {
|
||||
const top = rowOffsetOf(index);
|
||||
const extent = rowExtentOf(index);
|
||||
if (top < element.scrollTop) element.scrollTop = top;
|
||||
else if (top + extent > element.scrollTop + element.clientHeight)
|
||||
element.scrollTop = top + extent - element.clientHeight;
|
||||
update(element.scrollTop, element.clientHeight);
|
||||
}
|
||||
return { window, rowOffsetOf, rowExtentOf, totalHeight, update, scrollToIndex };
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach the window updater to a scroll container: recomputes on scroll and on
|
||||
* container resize. Returns a cleanup callable.
|
||||
*/
|
||||
export function bindVirtualRows(
|
||||
rows: UseVirtualRows,
|
||||
element: HTMLElement,
|
||||
): () => void {
|
||||
const measure = () => rows.update(element.scrollTop, element.clientHeight);
|
||||
const observer =
|
||||
typeof ResizeObserver !== "undefined" ? new ResizeObserver(measure) : null;
|
||||
observer?.observe(element);
|
||||
element.addEventListener("scroll", measure, { passive: true });
|
||||
measure();
|
||||
return () => {
|
||||
element.removeEventListener("scroll", measure);
|
||||
observer?.disconnect();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user