File size: 905 Bytes
4e1096a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import { useEffect, useMemo, useRef } from 'react';
import { BookProgress } from '@/types/book';
import { isCfiInLocation } from '@/utils/cfi';
const useScrollToItem = (cfi: string, progress: BookProgress | null) => {
const viewRef = useRef<HTMLLIElement | null>(null);
const isCurrent = useMemo(() => isCfiInLocation(cfi, progress?.location), [cfi, progress]);
useEffect(() => {
if (!viewRef.current || !isCurrent) return;
// Scroll to the item if it's the current one and not visible
const element = viewRef.current;
const rect = element.getBoundingClientRect();
const isVisible = rect.top >= 0 && rect.bottom <= window.innerHeight;
if (!isVisible) {
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
element.setAttribute('aria-current', 'page');
}, [isCurrent]);
return { isCurrent, viewRef };
};
export default useScrollToItem;
|