logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

scrollable_list.js (5693B)


  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. };
  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.onLoadMore && !this.props.isLoading) {
  40. this.props.onLoadMore();
  41. }
  42. if (scrollTop < 100 && this.props.onScrollToTop) {
  43. this.props.onScrollToTop();
  44. } else if (this.props.onScroll) {
  45. this.props.onScroll();
  46. }
  47. }
  48. }, 150, {
  49. trailing: true,
  50. });
  51. componentDidMount () {
  52. this.attachScrollListener();
  53. this.attachIntersectionObserver();
  54. attachFullscreenListener(this.onFullScreenChange);
  55. // Handle initial scroll posiiton
  56. this.handleScroll();
  57. }
  58. getSnapshotBeforeUpdate (prevProps) {
  59. const someItemInserted = React.Children.count(prevProps.children) > 0 &&
  60. React.Children.count(prevProps.children) < React.Children.count(this.props.children) &&
  61. this.getFirstChildKey(prevProps) !== this.getFirstChildKey(this.props);
  62. if (someItemInserted && this.node.scrollTop > 0) {
  63. return this.node.scrollHeight - this.node.scrollTop;
  64. } else {
  65. return null;
  66. }
  67. }
  68. componentDidUpdate (prevProps, prevState, snapshot) {
  69. // Reset the scroll position when a new child comes in in order not to
  70. // jerk the scrollbar around if you're already scrolled down the page.
  71. if (snapshot !== null) {
  72. const newScrollTop = this.node.scrollHeight - snapshot;
  73. if (this.node.scrollTop !== newScrollTop) {
  74. this.node.scrollTop = newScrollTop;
  75. }
  76. }
  77. }
  78. componentWillUnmount () {
  79. this.detachScrollListener();
  80. this.detachIntersectionObserver();
  81. detachFullscreenListener(this.onFullScreenChange);
  82. }
  83. onFullScreenChange = () => {
  84. this.setState({ fullscreen: isFullscreen() });
  85. }
  86. attachIntersectionObserver () {
  87. this.intersectionObserverWrapper.connect({
  88. root: this.node,
  89. rootMargin: '300% 0px',
  90. });
  91. }
  92. detachIntersectionObserver () {
  93. this.intersectionObserverWrapper.disconnect();
  94. }
  95. attachScrollListener () {
  96. this.node.addEventListener('scroll', this.handleScroll);
  97. }
  98. detachScrollListener () {
  99. this.node.removeEventListener('scroll', this.handleScroll);
  100. }
  101. getFirstChildKey (props) {
  102. const { children } = props;
  103. let firstChild = children;
  104. if (children instanceof ImmutableList) {
  105. firstChild = children.get(0);
  106. } else if (Array.isArray(children)) {
  107. firstChild = children[0];
  108. }
  109. return firstChild && firstChild.key;
  110. }
  111. setRef = (c) => {
  112. this.node = c;
  113. }
  114. handleLoadMore = (e) => {
  115. e.preventDefault();
  116. this.props.onLoadMore();
  117. }
  118. render () {
  119. const { children, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, emptyMessage, onLoadMore } = this.props;
  120. const { fullscreen } = this.state;
  121. const childrenCount = React.Children.count(children);
  122. const loadMore = (hasMore && childrenCount > 0 && onLoadMore) ? <LoadMore visible={!isLoading} onClick={this.handleLoadMore} /> : null;
  123. let scrollableArea = null;
  124. if (isLoading || childrenCount > 0 || !emptyMessage) {
  125. scrollableArea = (
  126. <div className={classNames('scrollable', { fullscreen })} ref={this.setRef}>
  127. <div role='feed' className='item-list'>
  128. {prepend}
  129. {React.Children.map(this.props.children, (child, index) => (
  130. <IntersectionObserverArticleContainer
  131. key={child.key}
  132. id={child.key}
  133. index={index}
  134. listLength={childrenCount}
  135. intersectionObserverWrapper={this.intersectionObserverWrapper}
  136. saveHeightKey={trackScroll ? `${this.context.router.route.location.key}:${scrollKey}` : null}
  137. >
  138. {child}
  139. </IntersectionObserverArticleContainer>
  140. ))}
  141. {loadMore}
  142. </div>
  143. </div>
  144. );
  145. } else {
  146. scrollableArea = (
  147. <div className='empty-column-indicator' ref={this.setRef}>
  148. {emptyMessage}
  149. </div>
  150. );
  151. }
  152. if (trackScroll) {
  153. return (
  154. <ScrollContainer scrollKey={scrollKey} shouldUpdateScroll={shouldUpdateScroll}>
  155. {scrollableArea}
  156. </ScrollContainer>
  157. );
  158. } else {
  159. return scrollableArea;
  160. }
  161. }
  162. }