-
Notifications
You must be signed in to change notification settings - Fork 33
/
react.fakescroll.js
226 lines (183 loc) · 6.61 KB
/
react.fakescroll.js
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
import React, { useReducer, useRef, useEffect, useLayoutEffect, useCallback, createRef, useMemo } from "react"
const raf = window.requestAnimationFrame || (cb => window.setTimeout(cb, 1000 / 60))
const cx = (...list) => list.filter(Boolean).join(" ")
const CLASSNAMES = {
grab: "fakeScroll--grabbed",
hasBar: "fakeScroll--hasBar"
}
const reducer = (state, action) => {
switch (action.type) {
case "drag":
return {
...state,
isDragging: "scrollTop" in action,
scrollTop: action.scrollTop || state.scrollTop,
lastPageY: action.pageY || state.lastPageY
}
case "drag-release": return {...state, drag:false }
case "trackBounds": return { ...state, trackBounds: action.payload }
case "toggleSmoothScroll": return { ...state, smoothScroll: !state.smoothScroll }
case "ratio": return { ...state, ratio: action.payload }
case "mouseEvent": return { ...state, mouse:{ type:action.event.type, pageY:action.event.pageY }}
case "barProps": {
const { height, top, scrollHeight, ownHeight } = action.payload
return {
...state,
hasBar: scrollHeight > ownHeight,
barStyle: {
height: `${height}%`,
top: `${top}%`
}
}
}
case "scrollTo": return { ...state, scrollTo:action.to}
default:
return state
}
}
const initialState = {
trackBounds: {},
isDragging: false,
scrollTop: null,
scrollTo: null,
ratio: null,
lastPageY: null,
smoothScroll: false,
hasBar: null,
barStyle: undefined,
mouse: {}
}
const FakeScroll = ({ children, className, onChange:onChangeCB, track = false, ...rest }) => {
const [state, dispatch] = useReducer(reducer, initialState)
const listeners = useRef({})
const refs = useMemo(() => ({
wrap: createRef(),
track: createRef(),
content: createRef()
}),[])
const onMouseEvent = useCallback(e =>
raf(() => dispatch({ type:"mouseEvent", event:e }) )
, [dispatch])
const onScrollResize = useCallback(() => {
setBarGeometry()
// debounce - get track bounds
clearTimeout(listeners.current.timeout__resize)
listeners.current.timeout__resize = setTimeout(getTrackBounds, 200)
}, [])
useLayoutEffect(() => {
onScrollResize()
window.addEventListener("resize", onScrollResize)
// cleanup
return () => {
window.removeEventListener("resize", onScrollResize)
toggleDragEvents(false)
document.body.classList.remove(CLASSNAMES.grab)
}
}, [onScrollResize])
useEffect(()=>{
const {type} = state.mouse
if( type == "mousemove" )
onDrag(state.mouse)
else if( type == "mouseup" )
onStopDrag(state.mouse)
}, [state.mouse])
useEffect(()=>{
state.scrollTo >= 0 && scrollTo(state.scrollTo)
}, [state.scrollTo])
// events only binded when Bar element gets a "mousedown" event
const toggleDragEvents = (toggle = true) => {
const action = (toggle ? "add" : "remove") + "EventListener"
document[action]("mousemove", onMouseEvent)
document[action]("mouseup", onMouseEvent)
}
// click-holding the bar and moving it
const onDrag = e => {
const delta = e.pageY - state.lastPageY
const { trackBounds } = state
raf(() => {
const sTop = document.documentElement.scrollTop,
isDragWithinTrackBounds = e.pageY >= (state.trackBounds.top + sTop) &&
e.pageY <= (state.trackBounds.bottom + sTop)
if (isDragWithinTrackBounds)
dispatch({ type:"scrollTo", to:state.scrollTop + delta/state.ratio })
// update variables when cursor position is outside the Track bounds
else
dispatch({ type:"drag", scrollTop:refs.content.current.scrollTop, pageY:e.pageY })
})
}
const scrollTo = to => refs.content.current.scrollTop = to
const onStopDrag = () => {
toggleDragEvents(false)
document.body.classList.remove(CLASSNAMES.grab)
setTimeout(dispatch, 0, { type: "drag" })
}
const onBarGrab = e => {
dispatch({ type:"drag", scrollTop:refs.content.current.scrollTop, pageY:e.pageY })
document.body.classList.add(CLASSNAMES.grab)
toggleDragEvents()
}
const onTrackClick = e => {
if (!track || state.isDragging) return
const { trackBounds: _TB } = state,
perc = (e.clientY - _TB.top) /(_TB.height - _TB.topPad - _TB.bottomPad),
scrollHeight = refs.content.current.scrollHeight,
ownHeight = refs.wrap.current.clientHeight,
newScrollTop = perc * (scrollHeight - ownHeight);
if (track === "smooth") {
dispatch({ type: "toggleSmoothScroll" })
setTimeout(dispatch, 500, { type:"toggleSmoothScroll" })
}
dispatch({ type:"scrollTo", to:newScrollTop })
}
const getTrackBounds = useCallback(() => {
const bounds = refs.track.current.getBoundingClientRect()
const { paddingTop, paddingBottom } = window.getComputedStyle(refs.track.current, null)
bounds.topPad = parseInt(paddingTop, 10)
bounds.bottomPad = parseInt(paddingBottom, 10)
dispatch({ type: "trackBounds", payload: bounds })
return bounds
}, [])
// move th fake track bar element
const setBarGeometry = () => {
const scrollHeight = refs.content.current.scrollHeight,
ownHeight = refs.wrap.current.clientHeight,
scrollRatio = refs.content.current.scrollTop / (refs.content.current.scrollHeight - ownHeight)
// update fake scrollbar location on the Y axis using requestAnimationFrame
raf(() => {
const height = (ownHeight / scrollHeight) * 100,
top = (refs.content.current.scrollTop / scrollHeight) * 100
dispatch({ type: "ratio", payload: refs.track.current.clientHeight / scrollHeight })
dispatch({ type: "barProps", payload: { height, top, scrollHeight, ownHeight } })
onChangeCB && onChangeCB({ scrollRatio })
})
}
return (
<div className={cx("fakeScroll", state.hasBar && CLASSNAMES.hasBar, className)} onMouseEnter={onScrollResize}>
<div className="fakeScroll__wrap" ref={refs.wrap}>
<div
className="fakeScroll__content"
ref={refs.content}
style={state.smoothScroll ? { scrollBehavior: "smooth" } : undefined}
onScroll={onScrollResize}
{...rest}
>
{children}
</div>
</div>
<div
className={cx("fakeScroll__track")}
onClick={onTrackClick}
ref={refs.track}
>
{ state.hasBar &&
<div
style={state.barStyle}
className={cx("fakeScroll__bar", state.isDragging && CLASSNAMES.grab)}
onMouseDown={onBarGrab}
/>
}
</div>
</div>
)
}
export default FakeScroll