Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve horizontal scrolling performance of the headers row in tables #167

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/components/tables/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ function useSelectHandler<TData>(
return [lastIndex, onRowClick];
}

function useTableVirtualizer(count: number): [React.MutableRefObject<null>, number, Virtualizer<Element, Element>] {
const parentRef = useRef(null);
function useTableVirtualizer(count: number): [React.MutableRefObject<HTMLElement | null>, number, Virtualizer<HTMLElement, Element>] {
const parentRef = useRef<HTMLElement | null>(null);
const [rowHeight, setRowHeight] = useState(0);

const { value: fontSize } = useFontSize();
Expand All @@ -233,6 +233,29 @@ function useTableVirtualizer(count: number): [React.MutableRefObject<null>, numb
return [parentRef, rowHeight, rowVirtualizer];
}

function useSynchronizedScroll(fromRef: React.MutableRefObject<HTMLElement | null>, toRef: React.MutableRefObject<HTMLElement | null>): [
(node: HTMLElement | null) => void,
(node: HTMLElement | null) => void,
() => void,
] {
const syncScroll = useCallback(() => {
if (fromRef.current == null || toRef.current == null) return;
toRef.current.style.translate = `${-fromRef.current.scrollLeft}px`;
}, [fromRef, toRef]);

const toRefProxy = useCallback((node: HTMLElement | null) => {
toRef.current = node;
syncScroll();
}, [syncScroll, toRef]);

const fromRefProxy = useCallback((node: HTMLElement | null) => {
fromRef.current = node;
syncScroll();
}, [syncScroll, fromRef]);

return [fromRefProxy, toRefProxy, syncScroll];
}

export type TableSelectReducer = React.Dispatch<{ verb: "add" | "set" | "toggle", ids: string[] }>;

export function useStandardSelect(): [string[], TableSelectReducer] {
Expand Down Expand Up @@ -530,24 +553,20 @@ export function TrguiTable<TData>(props: {

const width = table.getTotalSize();

const [horizScroll, setHorizScroll] = useState(0);
const onTableScroll = useCallback((e: React.UIEvent) => {
setHorizScroll(e.currentTarget.scrollLeft);
}, []);
const headerRef = useRef<HTMLElement | null>(null);
const [parentRefProxy, headerRefProxy, syncHeaderScroll] = useSynchronizedScroll(parentRef, headerRef);

return (
<div className="torrent-table-container">
<Box
ref={headerRefProxy}
sx={(theme) => ({
height: `${rowHeight}px`,
width: `${width}px`,
backgroundColor: theme.colorScheme === "dark" ? theme.colors.dark[5] : theme.colors.gray[2],
flexShrink: 0,
position: "relative",
})}
style={{
translate: `${-horizScroll}px`,
}}>
})}>
{table.getHeaderGroups().map(headerGroup => (
<MemoizedHeaderRow key={headerGroup.id} {...{
headerGroup,
Expand All @@ -562,7 +581,7 @@ export function TrguiTable<TData>(props: {
}} />
))}
</Box>
<div ref={parentRef} className="torrent-table-rows" onScroll={onTableScroll} tabIndex={-1}>
<div ref={parentRefProxy} className="torrent-table-rows" onScroll={syncHeaderScroll} tabIndex={-1}>
<div className="torrent-table"
style={{ height: `${virtualizer.getTotalSize()}px`, width: `${width}px` }}>
{virtualizer.getVirtualItems()
Expand Down
Loading