overleaf/services/web/frontend/js/shared/hooks/use-ref-with-auto-focus.ts

18 lines
403 B
TypeScript
Raw Normal View History

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