-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathScrollToCell.tsx
More file actions
42 lines (37 loc) · 1.12 KB
/
ScrollToCell.tsx
File metadata and controls
42 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { useLayoutEffect, useRef } from 'react';
import { scrollIntoView } from './utils';
export interface PartialPosition {
readonly idx?: number | undefined;
readonly rowIdx?: number | undefined;
}
export default function ScrollToCell({
scrollToPosition: { idx, rowIdx },
gridRef,
setScrollToCellPosition
}: {
scrollToPosition: PartialPosition;
gridRef: React.RefObject<HTMLDivElement | null>;
setScrollToCellPosition: (cell: null) => void;
}) {
const ref = useRef<HTMLDivElement>(null);
useLayoutEffect(() => {
const grid = gridRef.current!;
const { scrollLeft, scrollTop } = grid;
// scroll until the cell is completely visible
// this is needed if the grid has auto-sized columns
// setting the behavior to auto so it can be overridden
scrollIntoView(ref.current, 'auto');
if (grid.scrollLeft === scrollLeft && grid.scrollTop === scrollTop) {
setScrollToCellPosition(null);
}
});
return (
<div
ref={ref}
style={{
gridColumn: idx === undefined ? '1/-1' : idx + 1,
gridRow: rowIdx === undefined ? '1/-1' : rowIdx + 1
}}
/>
);
}