File tree Expand file tree Collapse file tree 3 files changed +32
-4
lines changed Expand file tree Collapse file tree 3 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,30 @@ const Demo = () => {
113
113
>
114
114
Scroll To 50 (auto)
115
115
</ button >
116
+ < button
117
+ type = "button"
118
+ onClick = { ( ) => {
119
+ listRef . current . scrollTo ( {
120
+ index : 50 ,
121
+ align : 'top' ,
122
+ offset : 15 ,
123
+ } ) ;
124
+ } }
125
+ >
126
+ Scroll To 50 (top) + 15 offset
127
+ </ button >
128
+ < button
129
+ type = "button"
130
+ onClick = { ( ) => {
131
+ listRef . current . scrollTo ( {
132
+ index : 50 ,
133
+ align : 'bottom' ,
134
+ offset : 15 ,
135
+ } ) ;
136
+ } }
137
+ >
138
+ Scroll To 50 (bottom) + 15 offset
139
+ </ button >
116
140
< button
117
141
type = "button"
118
142
onClick = { ( ) => {
Original file line number Diff line number Diff line change @@ -24,10 +24,12 @@ export type ScrollConfig =
24
24
| {
25
25
index : number ;
26
26
align ?: ScrollAlign ;
27
+ offset ?: number ;
27
28
}
28
29
| {
29
30
key : React . Key ;
30
31
align ?: ScrollAlign ;
32
+ offset ?: number ;
31
33
} ;
32
34
export type ScrollTo = ( arg : number | ScrollConfig ) => void ;
33
35
export type ListRef = {
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ export default function useScrollTo<T>(
16
16
) : ScrollTo {
17
17
const scrollRef = React . useRef < number > ( ) ;
18
18
19
- return arg => {
19
+ return ( arg ) => {
20
20
raf . cancel ( scrollRef . current ) ;
21
21
22
22
if ( typeof arg === 'number' ) {
@@ -28,9 +28,11 @@ export default function useScrollTo<T>(
28
28
if ( 'index' in arg ) {
29
29
( { index } = arg ) ;
30
30
} else {
31
- index = data . findIndex ( item => getKey ( item ) === arg . key ) ;
31
+ index = data . findIndex ( ( item ) => getKey ( item ) === arg . key ) ;
32
32
}
33
33
34
+ const { offset = 0 } = arg ;
35
+
34
36
// We will retry 3 times in case dynamic height shaking
35
37
const syncScroll = ( times : number , targetAlign ?: 'top' | 'bottom' ) => {
36
38
if ( times < 0 || ! containerRef . current ) return ;
@@ -66,10 +68,10 @@ export default function useScrollTo<T>(
66
68
67
69
switch ( mergedAlign ) {
68
70
case 'top' :
69
- targetTop = itemTop ;
71
+ targetTop = itemTop - offset ;
70
72
break ;
71
73
case 'bottom' :
72
- targetTop = itemBottom - height ;
74
+ targetTop = itemBottom - height + offset ;
73
75
break ;
74
76
75
77
default : {
You can’t perform that action at this time.
0 commit comments