dataTable/src/scroll.js

49 lines
1.6 KiB
JavaScript
Raw Normal View History

import { VirtualScroll } from '@web/base';
export const createScrollManager = (container, state, onRenderedListChange) => {
const vs = VirtualScroll();
let scrollEl = null;
const refresh = () => {
if (!scrollEl) return;
// Expand the virtual viewport to 1.6x height to create a buffer
const virtualContainer = {
clientHeight: scrollEl.clientHeight * 1.6,
scrollTop: scrollEl.scrollTop
};
const res = vs.calc(virtualContainer, state.list);
if (res) {
Object.assign(state, {
prevHeight: res.prevHeight,
postHeight: res.postHeight,
_listStartIndex: res.listStartIndex,
_renderedList: res.renderedList
});
onRenderedListChange?.(res.renderedList.length);
}
};
return {
init: () => {
scrollEl = container.querySelector('.dt-main');
},
reset: (list) => {
state._listStartIndex = 0;
state._renderedList = vs.reset(list, scrollEl || container) || [];
setTimeout(() => {
if (state.list === list) {
vs.init(list, refresh);
}
});
},
updateRowHeight: (index, node) => {
// Restore this call so VirtualScroll can learn the actual height
// of the rows instead of using its 32px default. This fixes
// layout shifts/jumps when scrolling past the first group.
vs.update(index + (state._listStartIndex || 0), node);
},
refresh,
onScroll: refresh
};
};