1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/pdf-viewer/hooks/useVisibleOnSelect.ts

28 lines
739 B
TypeScript
Raw Normal View History

import { useRef, useEffect, MutableRefObject } from 'react';
export interface VisibleOnSelect {
container: MutableRefObject<HTMLElement>;
wrapperRef: MutableRefObject<HTMLElement>;
isVisible: boolean;
isSelected: boolean;
}
// Used in thumbnail view, to scroll to the newly selected page.
const useVisibleOnSelect = ({ container, wrapperRef, isVisible, isSelected }: VisibleOnSelect) => {
const isVisibleRef = useRef(isVisible);
useEffect(() => {
if (isSelected && !isVisibleRef.current) {
container.current.scrollTop = wrapperRef.current.offsetTop;
}
}, [isSelected, isVisibleRef, container, wrapperRef]);
useEffect(() => {
isVisibleRef.current = isVisible;
}, [isVisible]);
};
export default useVisibleOnSelect;