Skip to content

Commit 8b85d18

Browse files
committed
fix: better scrolling
1 parent 9fb5f66 commit 8b85d18

File tree

7 files changed

+209
-162
lines changed

7 files changed

+209
-162
lines changed

src/components/PaginatedTable/PaginatedTable.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
SortParams,
1717
} from './types';
1818
import {useScrollBasedChunks} from './useScrollBasedChunks';
19+
import {useTableScroll} from './useTableScroll';
1920

2021
import './PaginatedTable.scss';
2122

@@ -122,31 +123,23 @@ export const PaginatedTable = <T, F>({
122123
[onDataFetched, setFoundEntities, setIsInitialLoad, setTotalEntities],
123124
);
124125

125-
// reset table on filters change
126+
// Use the extracted table scroll hook
127+
// The hook handles scrolling internally based on dependencies
128+
useTableScroll({
129+
tableContainerRef,
130+
parentRef,
131+
dependencies: [rawFilters], // Add filters as a dependency to trigger scroll when they change
132+
});
133+
134+
// Reset table on filters change
126135
React.useLayoutEffect(() => {
127136
const defaultTotal = initialEntitiesCount || 0;
128137
const defaultFound = initialEntitiesCount || 1;
129138

130139
setTotalEntities(defaultTotal);
131140
setFoundEntities(defaultFound);
132141
setIsInitialLoad(true);
133-
134-
if (tableContainerRef.current && parentRef.current) {
135-
// Scroll the parent container to the position of the table container
136-
const tableRect = tableContainerRef.current.getBoundingClientRect();
137-
const parentRect = parentRef.current.getBoundingClientRect();
138-
const scrollTop = tableRect.top - parentRect.top + parentRef.current.scrollTop;
139-
parentRef.current.scrollTo(0, scrollTop);
140-
}
141-
}, [
142-
rawFilters,
143-
initialEntitiesCount,
144-
tableContainerRef,
145-
setTotalEntities,
146-
setFoundEntities,
147-
setIsInitialLoad,
148-
parentRef,
149-
]);
142+
}, [initialEntitiesCount, setTotalEntities, setFoundEntities, setIsInitialLoad]);
150143

151144
const renderChunks = () => {
152145
return activeChunks.map((isActive, index) => (
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
3+
interface UseTableScrollProps {
4+
tableContainerRef: React.RefObject<HTMLDivElement>;
5+
parentRef: React.RefObject<HTMLElement>;
6+
dependencies?: any[]; // Optional additional dependencies for the effect
7+
}
8+
9+
export const useTableScroll = ({
10+
tableContainerRef,
11+
parentRef,
12+
dependencies = [],
13+
}: UseTableScrollProps) => {
14+
// Get the CSS variable value for sticky top offset
15+
const getStickyTopOffset = React.useCallback(() => {
16+
// Try to get the variable from parent elements
17+
if (tableContainerRef.current) {
18+
const computedStyle = window.getComputedStyle(tableContainerRef.current);
19+
const stickyTopOffset = computedStyle.getPropertyValue(
20+
'--data-table-sticky-top-offset',
21+
);
22+
23+
return stickyTopOffset ? parseInt(stickyTopOffset, 10) : 0;
24+
}
25+
return 0;
26+
}, [tableContainerRef]);
27+
28+
// Handle table scrolling function
29+
const handleTableScroll = React.useCallback(() => {
30+
if (tableContainerRef.current && parentRef.current) {
31+
// Get the sticky top offset value
32+
const stickyTopOffset = getStickyTopOffset();
33+
34+
// Scroll the parent container to the position of the table container
35+
const tableRect = tableContainerRef.current.getBoundingClientRect();
36+
const parentRect = parentRef.current.getBoundingClientRect();
37+
const scrollTop = tableRect.top - parentRect.top + parentRef.current.scrollTop;
38+
if (tableRect.top < parentRect.top) {
39+
// Adjust scroll position to account for sticky offset
40+
parentRef.current.scrollTo(0, scrollTop - stickyTopOffset);
41+
}
42+
}
43+
}, [parentRef, tableContainerRef, getStickyTopOffset]);
44+
45+
// Trigger scroll adjustment with dependencies
46+
React.useLayoutEffect(() => {
47+
handleTableScroll();
48+
// eslint-disable-next-line react-hooks/exhaustive-deps
49+
}, [handleTableScroll, ...dependencies]);
50+
51+
return {
52+
handleTableScroll,
53+
};
54+
};

src/containers/Cluster/Cluster.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,4 @@
6161
.ydb-table-with-controls-layout__controls-wrapper {
6262
top: 40px;
6363
}
64-
65-
.ydb-table-with-controls-layout {
66-
--data-table-sticky-top-offset: 102px;
67-
}
6864
}

src/containers/Nodes/Nodes.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ interface NodeGroupProps {
250250
groupByParam?: NodesGroupByField;
251251
columns: Column<NodesPreparedEntity>[];
252252
parentRef: React.RefObject<HTMLElement>;
253+
tableContainerRef: React.RefObject<HTMLDivElement>;
253254
onIsExpandedChange: (name: string, isExpanded: boolean) => void;
254255
}
255256

@@ -264,10 +265,9 @@ const NodeGroup = React.memo(function NodeGroup({
264265
groupByParam,
265266
columns,
266267
parentRef,
268+
tableContainerRef,
267269
onIsExpandedChange,
268270
}: NodeGroupProps) {
269-
const tableContainerRef = React.useRef<HTMLDivElement>(null);
270-
271271
return (
272272
<TableGroup
273273
key={name}
@@ -276,7 +276,6 @@ const NodeGroup = React.memo(function NodeGroup({
276276
entityName={i18n('nodes')}
277277
expanded={isExpanded}
278278
onIsExpandedChange={onIsExpandedChange}
279-
ref={tableContainerRef}
280279
>
281280
<NodesTable
282281
path={path}
@@ -371,6 +370,7 @@ function GroupedNodesComponent({
371370
groupByParam={groupByParam}
372371
columns={columnsToShow}
373372
parentRef={parentRef}
373+
tableContainerRef={tableContainerRef}
374374
onIsExpandedChange={setIsGroupExpanded}
375375
/>
376376
);

src/containers/Storage/PaginatedStorageGroups.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ interface StorageGroupGroupProps {
9494
visibleEntities: 'all';
9595
filterGroupBy?: GroupsGroupByField;
9696
columns: StorageGroupsColumn[];
97+
tableContainerRef: React.RefObject<HTMLDivElement>;
9798
parentRef: React.RefObject<HTMLElement>;
9899
onIsExpandedChange: (name: string, isExpanded: boolean) => void;
99100
handleShowAllGroups: VoidFunction;
@@ -111,11 +112,10 @@ const StorageGroupGroup = React.memo(function StorageGroupGroup({
111112
parentRef,
112113
filterGroupBy,
113114
columns,
115+
tableContainerRef,
114116
onIsExpandedChange,
115117
handleShowAllGroups,
116118
}: StorageGroupGroupProps) {
117-
const tableContainerRef = React.useRef<HTMLDivElement>(null);
118-
119119
return (
120120
<TableGroup
121121
key={name}
@@ -124,7 +124,6 @@ const StorageGroupGroup = React.memo(function StorageGroupGroup({
124124
entityName={i18n('groups')}
125125
expanded={isExpanded}
126126
onIsExpandedChange={onIsExpandedChange}
127-
ref={tableContainerRef}
128127
>
129128
<PaginatedStorageGroupsTable
130129
database={database}
@@ -269,6 +268,7 @@ function GroupedStorageGroupsComponent({
269268
onIsExpandedChange={setIsGroupExpanded}
270269
handleShowAllGroups={handleShowAllGroups}
271270
columns={columnsToShow}
271+
tableContainerRef={tableContainerRef}
272272
/>
273273
);
274274
});

0 commit comments

Comments
 (0)