-
Notifications
You must be signed in to change notification settings - Fork 7
/
ReactStickyfill.js
47 lines (38 loc) · 989 Bytes
/
ReactStickyfill.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
import React from 'react'
import PropTypes from 'prop-types'
import Stickyfill from 'stickyfill'
import supports from 'css-supports'
supports.shim()
const stickyfill = Stickyfill()
class ReactStickyfill extends React.PureComponent {
componentDidMount() {
stickyfill.add(this.stickyElement)
}
componentWillUnmount() {
stickyfill.remove(this.stickyElement)
}
_getPositionStyleValue() {
const isStickySupported = CSS.supports('position', 'sticky')
return isStickySupported? 'sticky': '-webkit-sticky'
}
render() {
return (
<div
{...this.props}
ref={(el) => { this.stickyElement = el }}
style={{
top: 0,
zIndex: 1,
position: this._getPositionStyleValue(),
...this.props.style
}}
/>
)
}
}
ReactStickyfill.displayName = 'ReactStickyfill'
ReactStickyfill.propTypes = {
children: PropTypes.element,
className: PropTypes.string
}
export default ReactStickyfill