From 8ccb1d482a1cc58a18301ffe5b2a128f56a83a8f Mon Sep 17 00:00:00 2001 From: wmwragg Date: Fri, 26 Aug 2016 15:52:57 +0100 Subject: [PATCH] Better comments, and adjusted the top sticky logic, so that the very top header isn't allways stuck --- src/components/views/rooms/RoomList.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/RoomList.js b/src/components/views/rooms/RoomList.js index 1c507e6629..48f4b4084f 100644 --- a/src/components/views/rooms/RoomList.js +++ b/src/components/views/rooms/RoomList.js @@ -323,10 +323,13 @@ module.exports = React.createClass({ var scrollAreaHeight = scrollArea.getBoundingClientRect().height; if (initialise) { + // Get a collection of sticky header containers var stickies = document.getElementsByClassName("mx_RoomSubList_labelContainer"); + // Make sure there is sufficient space to do sticky headers this.scrollAreaSufficient = (120 + (stickies[0].getBoundingClientRect().height * stickies.length)) < scrollAreaHeight; + // Initialise the sticky headers if (this.scrollAreaSufficient) { if (typeof stickies === "object" && stickies.length > 0) { // Initialise the sticky headers @@ -344,9 +347,11 @@ module.exports = React.createClass({ var self = this; var scrollStuckOffset = 0; + // Scroll to the passed in position if (scrollToPosition !== undefined) { scrollArea.scrollTop = scrollToPosition; } + // Stick headers to top and bottom, or free them Array.prototype.forEach.call(this.stickyWrappers, function(sticky, i, stickyWrappers) { var stickyPosition = sticky.dataset.originalPosition; var stickyHeight = sticky.dataset.originalHeight; @@ -354,11 +359,12 @@ module.exports = React.createClass({ var topStuckHeight = stickyHeight * i; var bottomStuckHeight = stickyHeight * (stickyWrappers.length - i) - if (self.scrollAreaSufficient && stickyPosition <= (scrollArea.scrollTop + topStuckHeight)) { + if (self.scrollAreaSufficient && stickyPosition < (scrollArea.scrollTop + topStuckHeight)) { // Top stickies sticky.dataset.stuck = "top"; stickyHeader.classList.add("mx_RoomSubList_fixed"); stickyHeader.style.top = scrollArea.offsetTop + topStuckHeight + "px"; + // If stuck at top adjust the scroll back down to take account of all the stuck headers if (scrollToPosition !== undefined && stickyPosition === scrollToPosition) { scrollStuckOffset = topStuckHeight; } @@ -374,7 +380,7 @@ module.exports = React.createClass({ stickyHeader.style.top = null; } }); - // Adjust the scroll to take account of stuck headers + // Adjust the scroll to take account of top stuck headers if (scrollToPosition !== undefined) { scrollArea.scrollTop -= scrollStuckOffset; }