Fix intersection root margin for horizontal pager

This commit is contained in:
schroda
2025-02-17 14:31:18 +01:00
parent eba3cadb8b
commit 811b17361c

View File

@@ -26,7 +26,43 @@ interface ElementIntersectionInfo {
[ReadingMode.CONTINUOUS_HORIZONTAL]: ElementIntersection; [ReadingMode.CONTINUOUS_HORIZONTAL]: ElementIntersection;
} }
type PageType = 'first' | 'last';
const OPEN_CHAPTER_INTERSECTION_RATIO = 0; const OPEN_CHAPTER_INTERSECTION_RATIO = 0;
const INTERSECTION_THRESHOLD = '-10px';
const getRootMargin = (
pageType: PageType,
readingMode: ReadingMode,
readingDirection: ReadingDirection,
): string | undefined => {
if (!isContinuousReadingMode(readingMode)) {
return undefined;
}
const themeDirectionOfReadingDirection = READING_DIRECTION_TO_THEME_DIRECTION[readingDirection];
const firstPageMarginHorizontal = `0px ${INTERSECTION_THRESHOLD} 0px 0px`;
const lastPageMarginHorizontal = `0px 0px 0px ${INTERSECTION_THRESHOLD}`;
if (isContinuousVerticalReadingMode(readingMode)) {
if (pageType === 'first') {
return `0px 0px ${INTERSECTION_THRESHOLD} 0px`;
}
return `${INTERSECTION_THRESHOLD} 0px 0px 0px`;
}
if (pageType === 'first') {
return getOptionForDirection(
firstPageMarginHorizontal,
lastPageMarginHorizontal,
themeDirectionOfReadingDirection,
);
}
return getOptionForDirection(lastPageMarginHorizontal, firstPageMarginHorizontal, themeDirectionOfReadingDirection);
};
/** /**
* Returns info about if the start or end of an element is intersecting. * Returns info about if the start or end of an element is intersecting.
@@ -184,7 +220,7 @@ const getElementIntersection = (
* *
*/ */
export const useReaderInfiniteScrollUpdateChapter = ( export const useReaderInfiniteScrollUpdateChapter = (
pageType: 'first' | 'last', pageType: PageType,
chapterId: number, chapterId: number,
chapterToOpenId: number | undefined, chapterToOpenId: number | undefined,
isCurrentChapter: boolean, isCurrentChapter: boolean,
@@ -255,11 +291,11 @@ export const useReaderInfiniteScrollUpdateChapter = (
useMemo( useMemo(
() => ({ () => ({
threshold: [OPEN_CHAPTER_INTERSECTION_RATIO], threshold: [OPEN_CHAPTER_INTERSECTION_RATIO],
rootMargin: pageType === 'first' ? '0px 0px -10px 0px' : '-10px 0px 0px 0px', rootMargin: getRootMargin(pageType, readingMode, readingDirection),
// gets immediately observed once on initial render // gets immediately observed once on initial render
ignoreInitialObserve: true, ignoreInitialObserve: true,
}), }),
[pageType], [pageType, readingMode, readingDirection],
), ),
); );
}; };