Skip to content

Commit d6153e5

Browse files
committed
fix: add JSDoc
1 parent 997b6b7 commit d6153e5

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

src/components/TableWithControlsLayout/useTableScroll.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,55 @@
11
import React from 'react';
22

33
interface UseTableScrollProps {
4+
/**
5+
* Reference to the table container element. This is the element that contains
6+
* the table and whose position will be adjusted.
7+
*/
48
tableContainerRef?: React.RefObject<HTMLDivElement>;
9+
10+
/**
11+
* Reference to the scrollable container element. This is the parent element
12+
* that has scrolling capabilities.
13+
*/
514
scrollContainerRef?: React.RefObject<HTMLElement>;
6-
dependencies?: unknown[]; // Optional additional dependencies for the effect
15+
16+
/**
17+
* Array of values that, when changed, will trigger the scroll adjustment.
18+
* Include all values that might affect table height or position to ensure
19+
* proper scroll adjustment (e.g., filters, sorting, pagination state).
20+
*/
21+
dependencies?: unknown[];
722
}
823

24+
/**
25+
* A hook that manages scrolling behavior for tables within a scrollable container.
26+
*
27+
* This hook ensures proper positioning of tables when their content changes,
28+
* particularly when using sticky headers, by automatically adjusting the scroll position.
29+
* It reads the `--data-table-sticky-header-offset` CSS variable from the table container
30+
* to determine the sticky header offset for correct positioning.
31+
*
32+
* The scroll adjustment is triggered whenever any of the dependencies change,
33+
* but is skipped on the first render to prevent unwanted initial scrolling.
34+
*
35+
*
36+
* @param props - The hook parameters
37+
* @returns An object containing the handleTableScroll function that can be called manually
38+
*/
939
export const useTableScroll = ({
1040
tableContainerRef,
1141
scrollContainerRef,
1242
dependencies = [],
1343
}: UseTableScrollProps) => {
14-
// Get the CSS variable value for sticky top offset
44+
/**
45+
* Retrieves the sticky header offset from CSS variables.
46+
*
47+
* Reads the `--data-table-sticky-header-offset` CSS variable from the table container
48+
* element and converts it to a number. This value is used to adjust the scroll position
49+
* to account for sticky headers.
50+
*
51+
* @returns The sticky header offset in pixels, or 0 if not defined
52+
*/
1553
const getStickyTopOffset = React.useCallback(() => {
1654
// Try to get the variable from parent elements
1755
if (tableContainerRef?.current) {
@@ -26,7 +64,16 @@ export const useTableScroll = ({
2664
return 0;
2765
}, [tableContainerRef]);
2866

29-
// Handle table scrolling function
67+
/**
68+
* Adjusts the scroll position of the container to properly position the table.
69+
*
70+
* This function calculates the correct scroll position based on:
71+
* - The relative position of the table within the scroll container
72+
* - The sticky header offset (if any)
73+
*
74+
* It only adjusts the scroll position if the table would be partially hidden
75+
* behind the sticky header.
76+
*/
3077
const handleTableScroll = React.useCallback(() => {
3178
// Only proceed if both refs and their current properties are available
3279
if (tableContainerRef?.current && scrollContainerRef?.current) {
@@ -45,7 +92,13 @@ export const useTableScroll = ({
4592
}
4693
}, [scrollContainerRef, tableContainerRef, getStickyTopOffset]);
4794

48-
// Trigger scroll adjustment with dependencies, but skip on first render
95+
/**
96+
* Triggers the scroll adjustment when dependencies change.
97+
*
98+
* Uses useLayoutEffect to adjust the scroll position before browser paint,
99+
* preventing visual jumps. The adjustment is only performed if both refs
100+
* are available.
101+
*/
49102
React.useLayoutEffect(() => {
50103
// Only proceed if both refs are available
51104
if (!tableContainerRef || !scrollContainerRef) {
@@ -58,6 +111,11 @@ export const useTableScroll = ({
58111
}, [handleTableScroll, tableContainerRef, scrollContainerRef, ...dependencies]);
59112

60113
return {
114+
/**
115+
* Function to manually trigger the table scroll adjustment.
116+
* This can be useful in cases where you need to adjust the scroll
117+
* position outside of the dependency changes.
118+
*/
61119
handleTableScroll,
62120
};
63121
};

0 commit comments

Comments
 (0)