import { useLayoutEffect, useRef } from 'react'; export const useAutoFocus = ( autoFocus?: boolean ) => { const ref = useRef(null); useLayoutEffect(() => { if (ref.current && autoFocus) { // to avoid clicking on something focusable(e.g MenuItem), // then the input will not be focused setTimeout(() => { ref.current?.focus(); }, 0); } }, [autoFocus]); return ref; }; export const useAutoSelect = ( autoSelect?: boolean ) => { const ref = useAutoFocus(autoSelect); useLayoutEffect(() => { if (ref.current && autoSelect) { ref.current?.select(); } }, [autoSelect, ref]); return ref; };