-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
index.tsx
296 lines (251 loc) 路 6.87 KB
/
index.tsx
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import React from 'react'
import clamp from 'clamp'
import classNames from 'classnames'
import keys from 'rc-util/lib/KeyCode'
import { WheelHandler } from '../util/dom/WheelHandler'
import { MouseMoveTracker } from '../util/dom/MouseMoveTracker'
export class Scrollbar extends React.PureComponent<Scrollbar.Props> {
private thumbElem: HTMLDivElement
private containerElem: HTMLDivElement
private wheelHandler: WheelHandler
private mouseMoveTracker: MouseMoveTracker
private scale: number
private thumbSize: number
UNSAFE_componentWillMount() {
this.wheelHandler = new WheelHandler({
onWheel: this.isHorizontal() ? this.onWheelX : this.onWheelY,
shouldHandleScrollX: true,
shouldHandleScrollY: true,
stopPropagation: this.props.stopPropagation,
})
this.mouseMoveTracker = new MouseMoveTracker({
elem: document.documentElement,
onMouseMove: this.onMouseMove,
onMouseMoveEnd: this.onMouseMoveEnd,
})
}
componentWillUnmount() {
this.mouseMoveTracker.release()
delete this.mouseMoveTracker
}
isHorizontal() {
return this.props.orientation === 'horizontal'
}
fixPosition(position: number) {
const max = this.props.contentSize - this.props.containerSize
return clamp(position, 0, max)
}
triggerCallback = (nextPosition: number) => {
const max = this.props.contentSize - this.props.containerSize
const position = clamp(nextPosition, 0, max)
if (position !== this.props.scrollPosition) {
this.props.onScroll(position)
}
}
onWheel = (delta: number) => {
this.triggerCallback(this.props.scrollPosition + delta)
}
onWheelX = (deltaX: number, deltaY: number) => {
if (Math.abs(deltaX) >= Math.abs(deltaY)) {
this.onWheel(deltaX)
}
}
onWheelY = (deltaX: number, deltaY: number) => {
if (Math.abs(deltaX) <= Math.abs(deltaY)) {
this.onWheel(deltaY)
}
}
onKeyDown = (e: React.KeyboardEvent) => {
const keyCode = e.keyCode
// let focus move off the scrollbar
if (keyCode === keys.TAB) {
return
}
const { contentSize, containerSize } = this.props
let distance = this.props.keyboardScrollAmount
let direction = 0
if (this.isHorizontal()) {
switch (keyCode) {
case keys.HOME:
direction = -1
distance = contentSize
break
case keys.LEFT:
direction = -1
break
case keys.RIGHT:
direction = 1
break
default:
return
}
} else {
switch (keyCode) {
case keys.SPACE:
if (e.shiftKey) {
direction = -1
} else {
direction = 1
}
break
case keys.HOME:
direction = -1
distance = contentSize
break
case keys.UP:
direction = -1
break
case keys.DOWN:
direction = 1
break
case keys.PAGE_UP:
direction = -1
distance = containerSize
break
case keys.PAGE_DOWN:
direction = 1
distance = containerSize
break
default:
return
}
}
e.preventDefault()
this.triggerCallback(this.props.scrollPosition + distance! * direction)
}
onMouseDown = (e: React.MouseEvent) => {
if (e.target !== this.thumbElem) {
const nativeEvent = e.nativeEvent as any
const position = this.isHorizontal()
? nativeEvent.offsetX || nativeEvent.layerX
: nativeEvent.offsetY || nativeEvent.layerY
// mousedown on the scroll-track directly, move the
// center of the scroll-face to the mouse position.
this.triggerCallback((position - this.thumbSize * 0.5) / this.scale)
} else {
this.mouseMoveTracker.capture(e)
}
if (this.props.stopPropagation) {
e.stopPropagation()
}
// focus the container so it may receive keyboard events
this.containerElem.focus()
}
onMouseMove = (deltaX: number, deltaY: number) => {
let delta = this.isHorizontal() ? deltaX : deltaY
if (delta !== 0) {
delta /= this.scale
this.triggerCallback(this.props.scrollPosition + delta)
}
}
onMouseMoveEnd = () => {
this.mouseMoveTracker.release()
}
refContainer = (container: HTMLDivElement) => {
this.containerElem = container
}
refThumb = (thumb: HTMLDivElement) => {
this.thumbElem = thumb
}
render() {
const {
prefixCls,
className,
scrollPosition,
containerSize,
contentSize,
miniThumbSize,
zIndex,
scrollbarSize,
} = this.props
// unscrollable
if (containerSize < 1 || contentSize <= containerSize) {
return null
}
let scale = containerSize / contentSize
let thumbSize = containerSize * scale
if (thumbSize < miniThumbSize!) {
scale = (containerSize - miniThumbSize!) / (contentSize - containerSize)
thumbSize = miniThumbSize!
}
// cache
this.scale = scale
this.thumbSize = thumbSize
let trackStyle: React.CSSProperties
let thumbStyle: React.CSSProperties
const horizontal = this.isHorizontal()
if (horizontal) {
trackStyle = {
width: containerSize,
height: scrollbarSize,
}
thumbStyle = {
width: thumbSize,
transform: `translate(${scrollPosition * scale}px, 0)`,
}
} else {
trackStyle = {
width: scrollbarSize,
height: containerSize,
}
thumbStyle = {
height: thumbSize,
transform: `translate(0, ${scrollPosition * scale}px)`,
}
}
if (zIndex) {
trackStyle.zIndex = zIndex
}
const baseCls = `${prefixCls}-scrollbar`
return (
<div
className={classNames(
baseCls,
{
[`${baseCls}-vertical`]: !horizontal,
[`${baseCls}-horizontal`]: horizontal,
},
className,
)}
style={trackStyle}
tabIndex={0}
ref={this.refContainer}
onKeyDown={this.onKeyDown}
onMouseDown={this.onMouseDown}
onWheel={this.wheelHandler.onWheel}
>
<div
ref={this.refThumb}
style={thumbStyle}
className={`${baseCls}-thumb`}
/>
</div>
)
}
}
export namespace Scrollbar {
export interface Props {
prefixCls?: string
className?: string
orientation: 'vertical' | 'horizontal'
contentSize: number
containerSize: number
scrollPosition: number
scrollbarSize?: number
miniThumbSize?: number
keyboardScrollAmount?: number
zIndex?: number
stopPropagation?: boolean | (() => boolean)
onScroll: (delta: number) => void
}
export const defaultProps = {
prefixCls: 'x6',
orientation: 'vertical',
contentSize: 0,
containerSize: 0,
defaultPosition: 0,
scrollbarSize: 4,
miniThumbSize: 16,
keyboardScrollAmount: 40,
}
}