overleaf/services/web/frontend/js/shared/hooks/use-ref-with-auto-focus.ts
Davinder Singh e65ede1d6a Merge pull request #9572 from overleaf/ab-new-project-autofocus
[web] Autofocus on new project name input field

GitOrigin-RevId: 4e26b2b5b6d39df942dc8ed293d3e4212028e4b0
2022-09-16 08:04:34 +00:00

17 lines
409 B
TypeScript

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