overleaf/services/web/frontend/js/shared/hooks/use-ref-with-auto-focus.ts
Alexandre Bourdin 171cd4f21c Merge pull request #9724 from overleaf/ab-tag-modals-autofocus
[web] Add autofocus to React dashboard tag modals

GitOrigin-RevId: 804fee108ab8e1c5bf97ade8032c4ea363baf7bb
2022-09-27 08:04:16 +00:00

17 lines
403 B
TypeScript

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 }
}