mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-18 09:35:20 +02:00
28 lines
746 B
TypeScript
28 lines
746 B
TypeScript
|
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;
|