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.

197 lines
5.7 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. alwaysPrepend: PropTypes.bool,
  26. emptyMessage: PropTypes.node,
  27. children: PropTypes.node,
  28. };
  29. static defaultProps = {
  30. trackScroll: true,
  31. };
  32. state = {
  33. fullscreen: null,
  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) {
  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. render () {
  120. const { children, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, alwaysPrepend, emptyMessage, onLoadMore } = this.props;
  121. const { fullscreen } = this.state;
  122. const childrenCount = React.Children.count(children);
  123. const loadMore = (hasMore && childrenCount > 0 && onLoadMore) ? <LoadMore visible={!isLoading} onClick={this.handleLoadMore} /> : null;
  124. let scrollableArea = null;
  125. if (isLoading || childrenCount > 0 || !emptyMessage) {
  126. scrollableArea = (
  127. <div className={classNames('scrollable', { fullscreen })} ref={this.setRef}>
  128. <div role='feed' className='item-list'>
  129. {prepend}
  130. {React.Children.map(this.props.children, (child, index) => (
  131. <IntersectionObserverArticleContainer
  132. key={child.key}
  133. id={child.key}
  134. index={index}
  135. listLength={childrenCount}
  136. intersectionObserverWrapper={this.intersectionObserverWrapper}
  137. saveHeightKey={trackScroll ? `${this.context.router.route.location.key}:${scrollKey}` : null}
  138. >
  139. {child}
  140. </IntersectionObserverArticleContainer>
  141. ))}
  142. {loadMore}
  143. </div>
  144. </div>
  145. );
  146. } else {
  147. scrollableArea = (
  148. <div style={{ flex: '1 1 auto', display: 'flex', flexDirection: 'column' }}>
  149. {alwaysPrepend && prepend}
  150. <div className='empty-column-indicator' ref={this.setRef}>
  151. {emptyMessage}
  152. </div>
  153. </div>
  154. );
  155. }
  156. if (trackScroll) {
  157. return (
  158. <ScrollContainer scrollKey={scrollKey} shouldUpdateScroll={shouldUpdateScroll}>
  159. {scrollableArea}
  160. </ScrollContainer>
  161. );
  162. } else {
  163. return scrollableArea;
  164. }
  165. }
  166. }