File size: 947 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 30 31 32 33 34 35 36 | import { useEffect, useRef } from 'react';
interface UseAutoFocusOptions {
ref?: React.RefObject<HTMLElement | null>;
delay?: number;
condition?: boolean;
preventScroll?: boolean;
}
export const useAutoFocus = <T extends HTMLElement = HTMLDivElement>(
options: UseAutoFocusOptions = {},
) => {
const { delay = 0, condition = true, preventScroll = false, ref } = options;
const localRef = useRef<T>(null);
const focusRef = ref || localRef;
useEffect(() => {
if (condition && focusRef.current) {
const focusElement = () => {
focusRef.current?.focus({ preventScroll });
};
if (delay > 0) {
const timer = setTimeout(focusElement, delay);
return () => clearTimeout(timer);
} else {
focusElement();
}
}
return;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [condition, delay, preventScroll]);
return focusRef as React.RefObject<T>;
};
|