You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.8 KiB

  1. import React, { PureComponent } from 'react';
  2. import { ScrollContainer } from 'react-router-scroll-4';
  3. import PropTypes from 'prop-types';
  4. import IntersectionObserverArticleContainer from '../containers/intersection_observer_article_container';
  5. import LoadMore from './load_more';
  6. import IntersectionObserverWrapper from '../features/ui/util/intersection_observer_wrapper';
  7. import { throttle } from 'lodash';
  8. import { List as ImmutableList } from 'immutable';
  9. import classNames from 'classnames';
  10. import { attachFullscreenListener, detachFullscreenListener, isFullscreen } from '../features/ui/util/fullscreen';
  11. export default class ScrollableList extends PureComponent {
  12. static contextTypes = {
  13. router: PropTypes.object,
  14. };
  15. static propTypes = {
  16. scrollKey: PropTypes.string.isRequired,
  17. onLoadMore: PropTypes.func,
  18. onScrollToTop: PropTypes.func,
  19. onScroll: PropTypes.func,
  20. trackScroll: PropTypes.bool,
  21. shouldUpdateScroll: PropTypes.func,
  22. isLoading: PropTypes.bool,
  23. hasMore: PropTypes.bool,
  24. prepend: PropTypes.node,
  25. emptyMessage: PropTypes.node,
  26. children: PropTypes.node,
  27. };
  28. static defaultProps = {
  29. trackScroll: true,
  30. };
  31. state = {
  32. fullscreen: null,
  33. mouseOver: false,
  34. };
  35. intersectionObserverWrapper = new IntersectionObserverWrapper();
  36. handleScroll = throttle(() => {
  37. if (this.node) {
  38. const { scrollTop, scrollHeight, clientHeight } = this.node;
  39. const offset = scrollHeight - scrollTop - clientHeight;
  40. if (400 > offset && this.props.onLoadMore && !this.props.isLoading) {
  41. this.props.onLoadMore();
  42. }
  43. if (scrollTop < 100 && this.props.onScrollToTop) {
  44. this.props.onScrollToTop();
  45. } else if (this.props.onScroll) {
  46. this.props.onScroll();
  47. }
  48. }
  49. }, 150, {
  50. trailing: true,
  51. });
  52. componentDidMount () {
  53. this.attachScrollListener();
  54. this.attachIntersectionObserver();
  55. attachFullscreenListener(this.onFullScreenChange);
  56. // Handle initial scroll posiiton
  57. this.handleScroll();
  58. }
  59. getSnapshotBeforeUpdate (prevProps) {
  60. const someItemInserted = React.Children.count(prevProps.children) > 0 &&
  61. React.Children.count(prevProps.children) < React.Children.count(this.props.children) &&
  62. this.getFirstChildKey(prevProps) !== this.getFirstChildKey(this.props);
  63. if (someItemInserted && this.node.scrollTop > 0 || this.state.mouseOver) {
  64. return this.node.scrollHeight - this.node.scrollTop;
  65. } else {
  66. return null;
  67. }
  68. }
  69. componentDidUpdate (prevProps, prevState, snapshot) {
  70. // Reset the scroll position when a new child comes in in order not to
  71. // jerk the scrollbar around if you're already scrolled down the page.
  72. if (snapshot !== null) {
  73. const newScrollTop = this.node.scrollHeight - snapshot;
  74. if (this.node.scrollTop !== newScrollTop) {
  75. this.node.scrollTop = newScrollTop;
  76. }
  77. }
  78. }
  79. componentWillUnmount () {
  80. this.detachScrollListener();
  81. this.detachIntersectionObserver();
  82. detachFullscreenListener(this.onFullScreenChange);
  83. }
  84. onFullScreenChange = () => {
  85. this.setState({ fullscreen: isFullscreen() });
  86. }
  87. attachIntersectionObserver () {
  88. this.intersectionObserverWrapper.connect({
  89. root: this.node,
  90. rootMargin: '300% 0px',
  91. });
  92. }
  93. detachIntersectionObserver () {
  94. this.intersectionObserverWrapper.disconnect();
  95. }
  96. attachScrollListener () {
  97. this.node.addEventListener('scroll', this.handleScroll);
  98. }
  99. detachScrollListener () {
  100. this.node.removeEventListener('scroll', this.handleScroll);
  101. }
  102. getFirstChildKey (props) {
  103. const { children } = props;
  104. let firstChild = children;
  105. if (children instanceof ImmutableList) {
  106. firstChild = children.get(0);
  107. } else if (Array.isArray(children)) {
  108. firstChild = children[0];
  109. }
  110. return firstChild && firstChild.key;
  111. }
  112. setRef = (c) => {
  113. this.node = c;
  114. }
  115. handleLoadMore = (e) => {
  116. e.preventDefault();
  117. this.props.onLoadMore();
  118. }
  119. handleMouseEnter = () => {
  120. this.setState({ mouseOver: true });
  121. }
  122. handleMouseLeave = () => {
  123. this.setState({ mouseOver: false });
  124. }
  125. render () {
  126. const { children, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, emptyMessage, onLoadMore } = this.props;
  127. const { fullscreen } = this.state;
  128. const childrenCount = React.Children.count(children);
  129. const loadMore = (hasMore && childrenCount > 0 && onLoadMore) ? <LoadMore visible={!isLoading} onClick={this.handleLoadMore} /> : null;
  130. let scrollableArea = null;
  131. if (isLoading || childrenCount > 0 || !emptyMessage) {
  132. scrollableArea = (
  133. <div className={classNames('scrollable', { fullscreen })} ref={this.setRef} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  134. <div role='feed' className='item-list'>
  135. {prepend}
  136. {React.Children.map(this.props.children, (child, index) => (
  137. <IntersectionObserverArticleContainer
  138. key={child.key}
  139. id={child.key}
  140. index={index}
  141. listLength={childrenCount}
  142. intersectionObserverWrapper={this.intersectionObserverWrapper}
  143. saveHeightKey={trackScroll ? `${this.context.router.route.location.key}:${scrollKey}` : null}
  144. >
  145. {child}
  146. </IntersectionObserverArticleContainer>
  147. ))}
  148. {loadMore}
  149. </div>
  150. </div>
  151. );
  152. } else {
  153. scrollableArea = (
  154. <div className='empty-column-indicator' ref={this.setRef}>
  155. {emptyMessage}
  156. </div>
  157. );
  158. }
  159. if (trackScroll) {
  160. return (
  161. <ScrollContainer scrollKey={scrollKey} shouldUpdateScroll={shouldUpdateScroll}>
  162. {scrollableArea}
  163. </ScrollContainer>
  164. );
  165. } else {
  166. return scrollableArea;
  167. }
  168. }
  169. }