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