Prevent layout shifts in continuous pagers

I was unable to find a fix for this problem, thus, the only solution I see is disabling this functionality for the continuous readers.

Due to preloading leading pages, the layout got shifted once the pages got loaded.
This messed with the scroll position which caused the top of the scrolled into views pages to not be at the top of the viewport anymore.
This commit is contained in:
schroda
2024-12-31 02:42:17 +01:00
parent 1d955257dd
commit f808614e6c
2 changed files with 13 additions and 3 deletions

View File

@@ -43,8 +43,15 @@ const BaseBasePager = ({
}) => {
const previousCurrentPageIndex = useRef(-1);
const pagesIndexesToRender = useMemo(
() => getPageIndexesToLoad(currentPageIndex, pages, previousCurrentPageIndex.current, imagePreLoadAmount),
[currentPageIndex, pages, imagePreLoadAmount],
() =>
getPageIndexesToLoad(
currentPageIndex,
pages,
previousCurrentPageIndex.current,
imagePreLoadAmount,
readingMode,
),
[currentPageIndex, pages, imagePreLoadAmount, readingMode],
);
useEffect(() => {
previousCurrentPageIndex.current = currentPageIndex;

View File

@@ -462,16 +462,19 @@ export const getPageIndexesToLoad = (
pages: ReaderStatePages['pages'],
previousCurrentPageIndex: number,
imagePreLoadAmount: number,
readingMode: ReadingMode,
): number[] => {
const currentPagesIndex = getPage(currentPageIndex, pages).pagesIndex;
const directionInvert = previousCurrentPageIndex <= currentPageIndex ? 1 : -1;
// load at most PREVIOUS_IMAGE_LOAD_AMOUNT of the previous pages to ensure that you do not have to wait too long
// when going back to the previous pages
const startPagesIndex = Math.max(
const startPagesIndexTrailingIncluded = Math.max(
0,
currentPagesIndex - Math.min(PREVIOUS_IMAGE_LOAD_AMOUNT, imagePreLoadAmount) * directionInvert,
);
// do not load previous pages for continuous pagers to prevent layout shifts due to leading pages getting loaded
const startPagesIndex = !isContinuousReadingMode(readingMode) ? startPagesIndexTrailingIncluded : currentPageIndex;
const endPagesIndex = currentPagesIndex + imagePreLoadAmount * directionInvert;
const pagesToRenderLength = Math.abs(endPagesIndex - startPagesIndex) + 1;