Skip to content

Commit

Permalink
Scroll to start of page on page change
Browse files Browse the repository at this point in the history
Resets the scroll position for single/double page mode to the start of the opened page on a page change.
  • Loading branch information
schroda committed Dec 25, 2024
1 parent d67d6f7 commit ae89a42
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/modules/reader/components/viewer/ReaderViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
createPagesData,
getDoublePageModePages,
getScrollIntoViewInlineOption,
getScrollToXForReadingDirection,
isSpreadPage,
} from '@/modules/reader/utils/ReaderPager.utils.tsx';
import { useReaderScrollbarContext } from '@/modules/reader/contexts/ReaderScrollbarContext.tsx';
Expand Down Expand Up @@ -227,6 +228,13 @@ const BaseReaderViewer = forwardRef(
});
}

if (!isContinuousReadingModeActive) {
scrollElementRef.current?.scrollTo(
getScrollToXForReadingDirection(scrollElementRef.current, themeDirection, readingDirection),
0,
);
}

const newPageIndex = getNextIndexFromPage(pageToScrollTo);
const isLastPage = newPageIndex === totalPages - 1;

Expand Down
14 changes: 14 additions & 0 deletions src/modules/reader/utils/ReaderPager.utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,17 @@ export const getScrollIntoViewInlineOption = (

return getOptionForDirection('end', 'start', themeDirectionForReadingDirection);
};

export const getScrollToXForReadingDirection = (
element: HTMLElement,
themeDirection: Direction,
readingDirection: ReadingDirection,
): number => {
const themeDirectionForReadingDirection = READING_DIRECTION_TO_THEME_DIRECTION[readingDirection];

if (themeDirection === 'ltr') {
return getOptionForDirection(0, element.scrollWidth, themeDirectionForReadingDirection);
}

return getOptionForDirection(-element.scrollWidth, 0, themeDirectionForReadingDirection);
};

0 comments on commit ae89a42

Please sign in to comment.