/
Sticky.js
331 lines (263 loc) · 9.11 KB
/
Sticky.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { isRefObject } from '@stardust-ui/react-component-ref'
import cx from 'classnames'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React, { Component, createRef } from 'react'
import {
customPropTypes,
eventStack,
getElementType,
getUnhandledProps,
isBrowser,
} from '../../lib'
/**
* Sticky content stays fixed to the browser viewport while another column of content is visible on the page.
*/
export default class Sticky extends Component {
static propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/** A Sticky can be active. */
active: PropTypes.bool,
/** Offset in pixels from the bottom of the screen when fixing element to viewport. */
bottomOffset: PropTypes.number,
/** Primary content. */
children: PropTypes.node,
/** Additional classes. */
className: PropTypes.string,
/** Context which sticky element should stick to. */
context: PropTypes.oneOfType([customPropTypes.domNode, customPropTypes.refObject]),
/** Offset in pixels from the top of the screen when fixing element to viewport. */
offset: PropTypes.number,
/**
* Callback when element is bound to bottom of parent container.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onBottom: PropTypes.func,
/**
* Callback when element is fixed to page.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onStick: PropTypes.func,
/**
* Callback when element is bound to top of parent container.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onTop: PropTypes.func,
/**
* Callback when element is unfixed from page.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onUnstick: PropTypes.func,
/** Whether element should be "pushed" by the viewport, attaching to the bottom of the screen when scrolling up. */
pushing: PropTypes.bool,
/** Context which sticky should attach onscroll events. */
scrollContext: PropTypes.oneOfType([customPropTypes.domNode, customPropTypes.refObject]),
/** Custom style for sticky element. */
styleElement: PropTypes.object,
}
static defaultProps = {
active: true,
bottomOffset: 0,
offset: 0,
scrollContext: isBrowser() ? window : null,
}
state = {
sticky: false,
}
stickyRef = createRef()
triggerRef = createRef()
componentDidMount() {
if (!isBrowser()) return
const { active } = this.props
if (active) {
this.handleUpdate()
this.addListeners(this.props)
}
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
const { active: current, scrollContext: currentScrollContext } = this.props
const { active: next, scrollContext: nextScrollContext } = nextProps
if (current === next) {
if (currentScrollContext !== nextScrollContext) {
this.removeListeners()
this.addListeners(nextProps)
}
return
}
if (next) {
this.handleUpdate()
this.addListeners(nextProps)
return
}
this.removeListeners()
this.setState({ sticky: false })
}
componentWillUnmount() {
if (!isBrowser()) return
const { active } = this.props
if (active) {
this.removeListeners()
cancelAnimationFrame(this.frameId)
}
}
// ----------------------------------------
// Events
// ----------------------------------------
addListeners = (props) => {
const { scrollContext } = props
const scrollContextNode = isRefObject(scrollContext) ? scrollContext.current : scrollContext
if (scrollContextNode) {
eventStack.sub('resize', this.handleUpdate, { target: scrollContextNode })
eventStack.sub('scroll', this.handleUpdate, { target: scrollContextNode })
}
}
removeListeners = () => {
const { scrollContext } = this.props
const scrollContextNode = isRefObject(scrollContext) ? scrollContext.current : scrollContext
if (scrollContextNode) {
eventStack.unsub('resize', this.handleUpdate, { target: scrollContextNode })
eventStack.unsub('scroll', this.handleUpdate, { target: scrollContextNode })
}
}
// ----------------------------------------
// Handlers
// ----------------------------------------
update = (e) => {
const { pushing } = this.state
this.ticking = false
this.assignRects()
if (pushing) {
if (this.didReachStartingPoint()) return this.stickToContextTop(e)
if (this.didTouchScreenBottom()) return this.stickToScreenBottom(e)
return this.stickToContextBottom(e)
}
if (this.isOversized()) {
if (this.contextRect.top > 0) return this.stickToContextTop(e)
if (this.contextRect.bottom < window.innerHeight) return this.stickToContextBottom(e)
}
if (this.didTouchScreenTop()) {
if (this.didReachContextBottom()) return this.stickToContextBottom(e)
return this.stickToScreenTop(e)
}
return this.stickToContextTop(e)
}
handleUpdate = (e) => {
if (!this.ticking) {
this.ticking = true
this.frameId = requestAnimationFrame(() => this.update(e))
}
}
// ----------------------------------------
// Helpers
// ----------------------------------------
assignRects = () => {
const { context } = this.props
const contextNode = isRefObject(context) ? context.current : context || document.body
this.triggerRect = this.triggerRef.current.getBoundingClientRect()
this.contextRect = contextNode.getBoundingClientRect()
this.stickyRect = this.stickyRef.current.getBoundingClientRect()
}
computeStyle() {
const { styleElement } = this.props
const { bottom, bound, sticky, top } = this.state
if (!sticky) return styleElement
return {
bottom: bound ? 0 : bottom,
top: bound ? undefined : top,
width: this.triggerRect.width,
...styleElement,
}
}
// Return true when the component reached the bottom of the context
didReachContextBottom = () => {
const { offset } = this.props
return this.stickyRect.height + offset >= this.contextRect.bottom
}
// Return true when the component reached the starting point
didReachStartingPoint = () => this.stickyRect.top <= this.triggerRect.top
// Return true when the top of the screen overpasses the Sticky component
didTouchScreenTop = () => this.triggerRect.top < this.props.offset
// Return true when the bottom of the screen overpasses the Sticky component
didTouchScreenBottom = () => {
const { bottomOffset } = this.props
return this.contextRect.bottom + bottomOffset > window.innerHeight
}
// Return true if the height of the component is higher than the window
isOversized = () => this.stickyRect.height > window.innerHeight
// ----------------------------------------
// Stick helpers
// ----------------------------------------
// If true, the component will stick to the bottom of the screen instead of the top
pushing = (pushing) => {
const { pushing: possible } = this.props
if (possible) this.setState({ pushing })
}
stick = (e, bound) => {
this.setState({ bound, sticky: true })
_.invoke(this.props, 'onStick', e, this.props)
}
unstick = (e, bound) => {
this.setState({ bound, sticky: false })
_.invoke(this.props, 'onUnstick', e, this.props)
}
stickToContextBottom = (e) => {
_.invoke(this.props, 'onBottom', e, this.props)
this.stick(e, true)
this.pushing(true)
}
stickToContextTop = (e) => {
_.invoke(this.props, 'onTop', e, this.props)
this.unstick(e, false)
this.pushing(false)
}
stickToScreenBottom = (e) => {
const { bottomOffset: bottom } = this.props
this.stick(e, false)
this.setState({ bottom, top: null })
}
stickToScreenTop = (e) => {
const { offset: top } = this.props
this.stick(e, false)
this.setState({ top, bottom: null })
}
// ----------------------------------------
// Render
// ----------------------------------------
render() {
const { children, className } = this.props
const { bottom, bound, sticky } = this.state
const rest = getUnhandledProps(Sticky, this.props)
const ElementType = getElementType(Sticky, this.props)
const containerClasses = cx(
sticky && 'ui',
sticky && 'stuck-container',
sticky && (bound ? 'bound-container' : 'fixed-container'),
className,
)
const elementClasses = cx(
'ui',
sticky && (bound ? 'bound bottom' : 'fixed'),
sticky && !bound && (bottom === null ? 'top' : 'bottom'),
'sticky',
)
const triggerStyles = sticky && this.stickyRect ? { height: this.stickyRect.height } : {}
return (
<ElementType {...rest} className={containerClasses}>
<div ref={this.triggerRef} style={triggerStyles} />
<div className={elementClasses} ref={this.stickyRef} style={this.computeStyle()}>
{children}
</div>
</ElementType>
)
}
}