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.

200 lines
5.9 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 'flavours/glitch/containers/intersection_observer_article_container';
  5. import LoadMore from './load_more';
  6. import IntersectionObserverWrapper from 'flavours/glitch/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 'flavours/glitch/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. onScrollToBottom: 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. };
  34. intersectionObserverWrapper = new IntersectionObserverWrapper();
  35. handleScroll = throttle(() => {
  36. if (this.node) {
  37. const { scrollTop, scrollHeight, clientHeight } = this.node;
  38. const offset = scrollHeight - scrollTop - clientHeight;
  39. if (400 > offset && this.props.onScrollToBottom && !this.props.isLoading) {
  40. this.props.onScrollToBottom();
  41. } else if (scrollTop < 100 && this.props.onScrollToTop) {
  42. this.props.onScrollToTop();
  43. } else if (this.props.onScroll) {
  44. this.props.onScroll();
  45. }
  46. }
  47. }, 150, {
  48. trailing: true,
  49. });
  50. componentDidMount () {
  51. this.attachScrollListener();
  52. this.attachIntersectionObserver();
  53. attachFullscreenListener(this.onFullScreenChange);
  54. // Handle initial scroll posiiton
  55. this.handleScroll();
  56. }
  57. getScrollPosition = () => {
  58. if (this.node && this.node.scrollTop > 0) {
  59. return {height: this.node.scrollHeight, top: this.node.scrollTop};
  60. } else {
  61. return null;
  62. }
  63. }
  64. updateScrollBottom = (snapshot) => {
  65. const newScrollTop = this.node.scrollHeight - snapshot;
  66. if (this.node.scrollTop !== newScrollTop) {
  67. this.node.scrollTop = newScrollTop;
  68. }
  69. }
  70. getSnapshotBeforeUpdate (prevProps, prevState) {
  71. const someItemInserted = React.Children.count(prevProps.children) > 0 &&
  72. React.Children.count(prevProps.children) < React.Children.count(this.props.children) &&
  73. this.getFirstChildKey(prevProps) !== this.getFirstChildKey(this.props);
  74. if (someItemInserted && this.node.scrollTop > 0) {
  75. return this.node.scrollHeight - this.node.scrollTop;
  76. } else {
  77. return null;
  78. }
  79. }
  80. componentDidUpdate (prevProps, prevState, snapshot) {
  81. // Reset the scroll position when a new child comes in in order not to
  82. // jerk the scrollbar around if you're already scrolled down the page.
  83. if (snapshot !== null) this.updateScrollBottom(snapshot);
  84. }
  85. componentWillUnmount () {
  86. this.detachScrollListener();
  87. this.detachIntersectionObserver();
  88. detachFullscreenListener(this.onFullScreenChange);
  89. }
  90. onFullScreenChange = () => {
  91. this.setState({ fullscreen: isFullscreen() });
  92. }
  93. attachIntersectionObserver () {
  94. this.intersectionObserverWrapper.connect({
  95. root: this.node,
  96. rootMargin: '300% 0px',
  97. });
  98. }
  99. detachIntersectionObserver () {
  100. this.intersectionObserverWrapper.disconnect();
  101. }
  102. attachScrollListener () {
  103. this.node.addEventListener('scroll', this.handleScroll);
  104. }
  105. detachScrollListener () {
  106. this.node.removeEventListener('scroll', this.handleScroll);
  107. }
  108. getFirstChildKey (props) {
  109. const { children } = props;
  110. let firstChild = children;
  111. if (children instanceof ImmutableList) {
  112. firstChild = children.get(0);
  113. } else if (Array.isArray(children)) {
  114. firstChild = children[0];
  115. }
  116. return firstChild && firstChild.key;
  117. }
  118. setRef = (c) => {
  119. this.node = c;
  120. }
  121. handleLoadMore = (e) => {
  122. e.preventDefault();
  123. this.props.onScrollToBottom();
  124. }
  125. render () {
  126. const { children, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, emptyMessage } = this.props;
  127. const { fullscreen } = this.state;
  128. const childrenCount = React.Children.count(children);
  129. const loadMore = (hasMore && childrenCount > 0) ? <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}>
  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. {React.cloneElement(child, {getScrollPosition: this.getScrollPosition, updateScrollBottom: this.updateScrollBottom})}
  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. }