mirror of
https://github.com/plausible/analytics.git
synced 2024-12-20 16:11:49 +03:00
ef0efa2d63
Fixes #639
37 lines
847 B
JavaScript
37 lines
847 B
JavaScript
import React from 'react';
|
|
|
|
export const withPinnedHeader = (WrappedComponent, selector) => {
|
|
return class extends React.Component {
|
|
constructor(props) {
|
|
super(props)
|
|
this.state = {
|
|
stuck: false
|
|
}
|
|
}
|
|
|
|
componentDidMount() {
|
|
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]
|
|
});
|
|
|
|
this.el = document.querySelector(selector)
|
|
this.observer.observe(this.el);
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
this.observer.unobserve(this.el);
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<WrappedComponent stuck={this.state.stuck}{...this.props}/>
|
|
);
|
|
}
|
|
}
|
|
}
|