2020-12-22 16:39:14 +03:00
|
|
|
import React from 'react';
|
|
|
|
|
2021-03-31 16:08:30 +03:00
|
|
|
export const withPinnedHeader = (WrappedComponent, selector) => {
|
2020-12-22 16:39:14 +03:00
|
|
|
return class extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {
|
|
|
|
stuck: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2022-02-09 00:07:40 +03:00
|
|
|
if ('IntersectionObserver' in window) {
|
|
|
|
this.attachObserver()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
attachObserver() {
|
2020-12-22 16:39:14 +03:00
|
|
|
this.observer = new IntersectionObserver((entries) => {
|
|
|
|
if (entries[0].intersectionRatio === 0)
|
|
|
|
this.setState({ stuck: true });
|
|
|
|
else if (entries[0].intersectionRatio === 1)
|
|
|
|
this.setState({ stuck: false });
|
|
|
|
}, {
|
|
|
|
threshold: [0, 1]
|
|
|
|
});
|
|
|
|
|
2021-03-31 16:08:30 +03:00
|
|
|
this.el = document.querySelector(selector)
|
|
|
|
this.observer.observe(this.el);
|
2020-12-22 16:39:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2022-02-08 22:57:39 +03:00
|
|
|
this.observer && this.observer.unobserve(this.el);
|
2020-12-22 16:39:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<WrappedComponent stuck={this.state.stuck}{...this.props}/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|