Guest User

Untitled

a guest
Jan 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. interface IState {
  2. sticky: boolean;
  3. }
  4.  
  5. export class HeaderBar extends React.Component<{}, IState> {
  6. sentinelRef: React.RefObject<HTMLDivElement>;
  7. observer: IntersectionObserver;
  8.  
  9. state = { sticky: false };
  10.  
  11. constructor(props: IProps) {
  12. super(props);
  13.  
  14. this.sentinelRef = React.createRef();
  15. }
  16.  
  17. componentDidMount() {
  18. this.observer = new IntersectionObserver(
  19. entries =>
  20. entries.forEach(entry =>
  21. this.setState({ sticky: entry.intersectionRatio !== 1 })
  22. ),
  23. { threshold: 1 }
  24. );
  25. this.observer.observe(this.sentinelRef.current);
  26. }
  27.  
  28. componentWillUnmount() {
  29. if (typeof window === 'undefined') return;
  30.  
  31. this.observer.unobserve(this.sentinelRef.current);
  32. }
  33.  
  34. render() {
  35. const { sticky } = this.state;
  36.  
  37. return (
  38. <Container
  39. sticky={sticky}
  40. >
  41. <Sentinel innerRef={this.sentinelRef} />
  42. <InnerContainerLeft fullHeight={fullHeightLeft}>
  43. {left}
  44. </InnerContainerLeft>
  45. <InnerContainerRight>{right}</InnerContainerRight>
  46. </Container>
  47. );
  48. }
  49. }
Add Comment
Please, Sign In to add comment