Fix intersection root margin for horizontal pager
This commit is contained in:
@@ -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],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user