overleaf/services/web/frontend/js/shared/hooks/use-ref-with-auto-focus.js
Alf Eaton 8c3578e74b Merge pull request #4041 from overleaf/ae-use-ref-with-autofocus
Rename hooks files and move to shared folder

GitOrigin-RevId: 9659247b5e767197c3e11acc9a3922ecaab49162
2021-05-19 02:05:50 +00:00

17 lines
373 B
JavaScript

import { useRef, useEffect } from 'react'
export function useRefWithAutoFocus() {
const autoFocusedRef = useRef()
useEffect(() => {
if (autoFocusedRef.current) {
window.requestAnimationFrame(() => {
if (autoFocusedRef.current) {
autoFocusedRef.current.focus()
}
})
}
}, [autoFocusedRef])
return { autoFocusedRef }
}