Skip to content

Commit

Permalink
Implement Scroll to Bottom on Timeline V2 (#1493)
Browse files Browse the repository at this point in the history
Co-authored-by: April Sylph <28949509+AprilSylph@users.noreply.github.com>
  • Loading branch information
marcustyphoon and AprilSylph authored Jul 13, 2024
1 parent 76411a6 commit 2d1c989
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/features/scroll_to_bottom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ $(`[id="${scrollToBottomButtonId}"]`).remove();
const activeClass = 'xkit-scroll-to-bottom-active';

const loaderSelector = `
${keyToCss('timeline', 'blogRows')} > :last-child,
${keyToCss('notifications')} + ${keyToCss('loader')}
${keyToCss('timeline', 'blogRows')} > :is(${keyToCss('scrollContainer')}, .sortableContainer) + div,
${keyToCss('notifications')} + div
`;
const knightRiderLoaderSelector = `:is(${loaderSelector}) > ${keyToCss('knightRiderLoader')}`;

Expand All @@ -22,13 +22,23 @@ const styleElement = buildStyle(`
}
`);

let timeoutID;

const onLoadersAdded = loaders => {
if (active) {
clearTimeout(timeoutID);
}
};

const scrollToBottom = () => {
clearTimeout(timeoutID);
window.scrollTo({ top: document.documentElement.scrollHeight });
const loaders = [...document.querySelectorAll(knightRiderLoaderSelector)];

if (loaders.length === 0) {
stopScrolling();
}
timeoutID = setTimeout(() => {
if (!document.querySelector(knightRiderLoaderSelector)) {
stopScrolling();
}
}, 500);
};
const observer = new ResizeObserver(scrollToBottom);

Expand All @@ -40,6 +50,7 @@ const startScrolling = () => {
};

const stopScrolling = () => {
clearTimeout(timeoutID);
observer.disconnect();
active = false;
scrollToBottomButton?.classList.remove(activeClass);
Expand Down Expand Up @@ -79,12 +90,14 @@ const addButtonToPage = async function ([scrollToTopButton]) {

export const main = async function () {
pageModifications.register(`button[aria-label="${translate('Scroll to top')}"]`, addButtonToPage);
pageModifications.register(knightRiderLoaderSelector, onLoadersAdded);
document.documentElement.append(styleElement);
};

export const clean = async function () {
pageModifications.unregister(addButtonToPage);
pageModifications.unregister(checkForButtonRemoved);
pageModifications.unregister(onLoadersAdded);
stopScrolling();
scrollToBottomButton?.remove();
styleElement.remove();
Expand Down

0 comments on commit 2d1c989

Please sign in to comment.