Created
April 5, 2019 13:13
-
-
Save zhammer/47dc55d9d9e22b8253f157baf2116865 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from 'react'; | |
type FocusProps = { | |
autoFocus: boolean; | |
onBlur: () => void; | |
onFocus: () => void; | |
}; | |
/** | |
* Hook that provides props to focus an element on mount. | |
* | |
* ``` | |
* const focusProps = useFocusOnMount(); | |
* return <input {...focusProps} /> | |
* ``` | |
*/ | |
export default function useFocusOnMount(): FocusProps { | |
const [focused, setFocused] = useState(true); | |
function onBlur() { | |
setFocused(false); | |
} | |
function onFocus() { | |
setFocused(true); | |
} | |
return { | |
autoFocus: focused, | |
onBlur, | |
onFocus | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment