2021-05-18 06:56:56 -04:00
|
|
|
import { useRef, useEffect } from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2022-09-26 05:35:54 -04:00
|
|
|
export function useRefWithAutoFocus<T extends HTMLElement = HTMLElement>() {
|
2022-09-15 05:58:35 -04:00
|
|
|
const autoFocusedRef = useRef<T>(null)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (autoFocusedRef.current) {
|
2021-03-18 05:52:36 -04:00
|
|
|
window.requestAnimationFrame(() => {
|
2021-05-18 06:56:56 -04:00
|
|
|
if (autoFocusedRef.current) {
|
|
|
|
autoFocusedRef.current.focus()
|
|
|
|
}
|
2020-12-15 05:23:54 -05:00
|
|
|
})
|
|
|
|
}
|
2022-09-26 05:35:54 -04:00
|
|
|
}, [])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
return { autoFocusedRef }
|
|
|
|
}
|