Last active
April 10, 2021 07:29
-
-
Save acro5piano/1f4909988df74047df09f44405041da2 to your computer and use it in GitHub Desktop.
A react hook to handle keyboard show/hide
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
const { inputting } = useInputting() | |
<div style={{ height: inputting ? '50vh' : '100vh' }}> | |
... |
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
export function useInputting() { | |
const [inputting, setInputting] = useState(false) | |
useEffect(() => { | |
const handler = () => { | |
setInputting((st) => !st) | |
} | |
document.addEventListener('focusin', handler) | |
document.addEventListener('focusout', handler) | |
return () => { | |
document.removeEventListener('focusin', handler) | |
document.removeEventListener('focusout', handler) | |
} | |
}, []) | |
return { inputting } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment