@@ -3,6 +3,7 @@ import styled from 'styled-components'
3
3
import { colors } from 'lib/styles'
4
4
import { Padding , ReactSmartScrollerProps } from 'lib/types'
5
5
import { C , isMobile , isMacOs } from 'lib/utils'
6
+ import { constants } from 'lib/common'
6
7
7
8
type ReactSmartScrollerHorizontalState = {
8
9
scrollContainerWidth : number ,
@@ -12,7 +13,8 @@ type ReactSmartScrollerHorizontalState = {
12
13
trackHeight : number ,
13
14
scrollWidth : number ,
14
15
scrollLeft : number ,
15
- padding : Padding
16
+ padding : Padding ,
17
+ ratio : number
16
18
}
17
19
18
20
export class ReactSmartScrollerHorizontal extends React . Component < ReactSmartScrollerProps , ReactSmartScrollerHorizontalState > {
@@ -31,7 +33,8 @@ export class ReactSmartScrollerHorizontal extends React.Component<ReactSmartScro
31
33
trackHeight : 0 ,
32
34
scrollWidth : 0 ,
33
35
scrollLeft : 0 ,
34
- padding : this . trackPadding
36
+ padding : this . trackPadding ,
37
+ ratio : 1
35
38
}
36
39
37
40
private overflowContainerRef : React . RefObject < HTMLDivElement > = React . createRef ( )
@@ -55,14 +58,17 @@ export class ReactSmartScrollerHorizontal extends React.Component<ReactSmartScro
55
58
componentDidMount ( ) {
56
59
window . addEventListener ( 'resize' , this . measureContainers )
57
60
window . addEventListener ( 'mouseup' , this . deleteMouseMoveEvent )
61
+ window . addEventListener ( 'transitionend' , this . measureContainers )
58
62
window . addEventListener ( 'mouseup' , this . deleteOverflowMouseMoveEvent )
59
- this . measureContainers ( )
63
+ window . addEventListener ( 'load' , this . measureContainers )
60
64
}
61
65
62
66
componentWillUnmount ( ) {
63
67
window . removeEventListener ( 'resize' , this . measureContainers )
64
68
window . removeEventListener ( 'mouseup' , this . deleteMouseMoveEvent )
69
+ window . removeEventListener ( 'transitionend' , this . measureContainers )
65
70
window . removeEventListener ( 'mouseup' , this . deleteOverflowMouseMoveEvent )
71
+ window . removeEventListener ( 'load' , this . measureContainers )
66
72
}
67
73
68
74
get shouldRenderScrollbar ( ) {
@@ -111,13 +117,37 @@ export class ReactSmartScrollerHorizontal extends React.Component<ReactSmartScro
111
117
: 0
112
118
}
113
119
120
+ get startElement ( ) {
121
+ if ( this . props . startAt ) {
122
+ return document . getElementById ( `${ constants . reactSmartScrollerId } -${ this . props . startAt . startIndex } ` )
123
+ }
124
+
125
+ return undefined
126
+ }
127
+
114
128
scrollContainerReducedWidth ( scrollContainerWidth : number ) {
115
129
const { padding } = this . state
116
130
117
131
return scrollContainerWidth - ( padding . left + padding . right )
118
132
}
119
133
120
- measureContainers ( ) {
134
+ setStartPosition ( ) {
135
+ const { startAt } = this . props
136
+ const overflowRef = this . overflowContainerRef . current as HTMLDivElement
137
+ const startElement = this . startElement
138
+
139
+ this . measureContainers ( )
140
+
141
+ if ( overflowRef && startElement ) {
142
+ const offset = startAt && startAt . center
143
+ ? ( overflowRef . clientWidth - startElement . clientWidth ) / 2
144
+ : 0
145
+
146
+ overflowRef . scrollLeft = startElement . offsetLeft - offset
147
+ }
148
+ }
149
+
150
+ measureContainers ( event ?: Event ) {
121
151
const overflownRef = this . overflowContainerRef . current as HTMLDivElement
122
152
const thumbRef = this . thumbRef . current as HTMLDivElement
123
153
const trackRef = this . trackRef . current as HTMLDivElement
@@ -128,11 +158,20 @@ export class ReactSmartScrollerHorizontal extends React.Component<ReactSmartScro
128
158
)
129
159
130
160
if ( areRefsCurrent ) {
161
+ const scrollContainerWidth = this . scrollContainerReducedWidth ( overflownRef . clientWidth )
162
+ const maximumOffset = scrollContainerWidth - thumbRef . offsetWidth
163
+ const ratio = maximumOffset / ( overflownRef . scrollWidth - overflownRef . clientWidth )
164
+
131
165
this . setState ( {
132
- scrollContainerWidth : this . scrollContainerReducedWidth ( overflownRef . clientWidth ) ,
166
+ scrollContainerWidth,
133
167
thumbHeight : thumbRef . offsetHeight ,
134
168
trackHeight : trackRef . clientHeight ,
135
- scrollWidth : overflownRef . scrollWidth
169
+ scrollWidth : overflownRef . scrollWidth ,
170
+ ratio
171
+ } , ( ) => {
172
+ if ( event && event . type === 'load' ) {
173
+ this . setStartPosition ( )
174
+ }
136
175
} )
137
176
}
138
177
@@ -193,7 +232,7 @@ export class ReactSmartScrollerHorizontal extends React.Component<ReactSmartScro
193
232
194
233
onMouseDrag ( event : DragEvent | MouseEvent ) {
195
234
const zero = 0
196
- const { deltaX, deltaXOrigin, scrollContainerWidth } = this . state
235
+ const { deltaX, deltaXOrigin, scrollContainerWidth, ratio } = this . state
197
236
const overflowRef = this . overflowContainerRef . current as HTMLDivElement
198
237
const thumbRef = this . thumbRef . current as HTMLDivElement
199
238
const maximumOffset = scrollContainerWidth - thumbRef . offsetWidth
@@ -215,22 +254,17 @@ export class ReactSmartScrollerHorizontal extends React.Component<ReactSmartScro
215
254
}
216
255
217
256
if ( areRefsCurrent && isBetweenClientWidth ) {
218
- const ratio = ( overflowRef . scrollWidth - overflowRef . clientWidth ) / maximumOffset
219
-
220
257
overflowRef . scroll ( ratio * offset , zero )
221
258
thumbRef . style . left = `${ offset } px`
222
259
}
223
260
}
224
261
225
262
onOverflowContentScroll ( ) {
226
- const { scrollContainerWidth } = this . state
263
+ const { ratio } = this . state
227
264
const thumbRef = this . thumbRef . current as HTMLDivElement
228
265
const overflowRef = this . overflowContainerRef . current
229
266
230
267
if ( overflowRef && thumbRef ) {
231
- const maximumOffset = scrollContainerWidth - thumbRef . offsetWidth
232
- const ratio = maximumOffset / ( overflowRef . scrollWidth - overflowRef . clientWidth )
233
-
234
268
thumbRef . style . left = `${ overflowRef . scrollLeft * ratio } px`
235
269
}
236
270
}
@@ -276,6 +310,7 @@ export class ReactSmartScrollerHorizontal extends React.Component<ReactSmartScro
276
310
277
311
return (
278
312
< ChildrenWrapper
313
+ id = { `${ constants . reactSmartScrollerId } -${ index } ` }
279
314
style = { {
280
315
padding : `0 ${ padding } px` ,
281
316
flexBasis,
@@ -348,7 +383,6 @@ export class ReactSmartScrollerHorizontal extends React.Component<ReactSmartScro
348
383
< SecondWrapper
349
384
ref = { this . overflowContainerRef }
350
385
onScroll = { this . onOverflowContentScroll }
351
- onLoad = { this . measureContainers }
352
386
onMouseDown = { draggable ? this . onOverflowContentMouseDown : C . noop }
353
387
style = { { cursor } }
354
388
>
0 commit comments