about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/components
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2018-04-15 21:29:03 +0200
committerThibaut Girka <thib@sitedethib.com>2018-04-19 11:30:42 +0200
commit694337d9bbaa2505f659d9b9980d393bd317679a (patch)
tree346f1041851d60cbd3ec1e0286600892756c63d9 /app/javascript/flavours/glitch/components
parent931a4d1ebf24c3f2b92782f2ee26558ead866f16 (diff)
Fix auto-collapsed toots making the TL jump (fixes #417)
Diffstat (limited to 'app/javascript/flavours/glitch/components')
-rw-r--r--app/javascript/flavours/glitch/components/scrollable_list.js26
-rw-r--r--app/javascript/flavours/glitch/components/status.js29
2 files changed, 46 insertions, 9 deletions
diff --git a/app/javascript/flavours/glitch/components/scrollable_list.js b/app/javascript/flavours/glitch/components/scrollable_list.js
index 928c49737..df3ace4c1 100644
--- a/app/javascript/flavours/glitch/components/scrollable_list.js
+++ b/app/javascript/flavours/glitch/components/scrollable_list.js
@@ -65,6 +65,22 @@ export default class ScrollableList extends PureComponent {
     this.handleScroll();
   }
 
+  getScrollPosition = () => {
+    if (this.node && this.node.scrollTop > 0) {
+      return {height: this.node.scrollHeight, top: this.node.scrollTop};
+    } else {
+      return null;
+    }
+  }
+
+  updateScrollBottom = (snapshot) => {
+    const newScrollTop = this.node.scrollHeight - snapshot;
+
+    if (this.node.scrollTop !== newScrollTop) {
+      this.node.scrollTop = newScrollTop;
+    }
+  }
+
   getSnapshotBeforeUpdate (prevProps, prevState) {
     const someItemInserted = React.Children.count(prevProps.children) > 0 &&
       React.Children.count(prevProps.children) < React.Children.count(this.props.children) &&
@@ -79,13 +95,7 @@ export default class ScrollableList extends PureComponent {
   componentDidUpdate (prevProps, prevState, snapshot) {
     // Reset the scroll position when a new child comes in in order not to
     // jerk the scrollbar around if you're already scrolled down the page.
-    if (snapshot !== null) {
-      const newScrollTop = this.node.scrollHeight - snapshot;
-
-      if (this.node.scrollTop !== newScrollTop) {
-        this.node.scrollTop = newScrollTop;
-      }
-    }
+    if (snapshot !== null) this.updateScrollBottom(snapshot);
   }
 
   componentWillUnmount () {
@@ -160,7 +170,7 @@ export default class ScrollableList extends PureComponent {
                 intersectionObserverWrapper={this.intersectionObserverWrapper}
                 saveHeightKey={trackScroll ? `${this.context.router.route.location.key}:${scrollKey}` : null}
               >
-                {child}
+                {React.cloneElement(child, {getScrollPosition: this.getScrollPosition, updateScrollBottom: this.updateScrollBottom})}
               </IntersectionObserverArticleContainer>
             ))}
 
diff --git a/app/javascript/flavours/glitch/components/status.js b/app/javascript/flavours/glitch/components/status.js
index eb621d5d7..cf82c9ac6 100644
--- a/app/javascript/flavours/glitch/components/status.js
+++ b/app/javascript/flavours/glitch/components/status.js
@@ -45,10 +45,13 @@ export default class Status extends ImmutablePureComponent {
     withDismiss: PropTypes.bool,
     onMoveUp: PropTypes.func,
     onMoveDown: PropTypes.func,
+    getScrollPosition: PropTypes.func,
+    updateScrollBottom: PropTypes.func,
   };
 
   state = {
     isExpanded: null,
+    autoCollapsed: false,
   }
 
   // Avoid checking props that are functions (and whose equality will always
@@ -134,7 +137,31 @@ export default class Status extends ImmutablePureComponent {
       default:
         return false;
       }
-    }()) this.setExpansion(false);
+    }()) {
+      this.setExpansion(false);
+      // Hack to fix timeline jumps on second rendering when auto-collapsing
+      this.setState({ autoCollapsed: true });
+    }
+  }
+
+  getSnapshotBeforeUpdate (prevProps, prevState) {
+    if (this.props.getScrollPosition) {
+      return this.props.getScrollPosition();
+    } else {
+      return null;
+    }
+  }
+
+  //  Hack to fix timeline jumps on second rendering when auto-collapsing
+  componentDidUpdate (prevProps, prevState, snapshot) {
+    if (this.state.autoCollapsed) {
+      this.setState({ autoCollapsed: false });
+      if (snapshot !== null && this.props.updateScrollBottom) {
+        if (this.node.offsetTop < snapshot.top) {
+          this.props.updateScrollBottom(snapshot.height - snapshot.top);
+        }
+      }
+    }
   }
 
   //  `setExpansion()` sets the value of `isExpanded` in our state. It takes