forked from react-native-community/hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseKeyboard.js
52 lines (44 loc) · 1.15 KB
/
useKeyboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React, { useEffect, useState } from 'react'
import { Keyboard } from 'react-native'
export default function useKeyboard() {
const [keyboard, setKeyboard] = useState({ Keyboard })
function keyboardShown(e) {
setKeyboard({
isKeyboardShow: true,
Keyboard,
endCoordinates: e ? e.endCoordinates : null,
startCoordinates: e ? e.startCoordinates : null
})
}
function keyboardHidden(e) {
setKeyboard({
isKeyboardShow: false,
Keyboard,
endCoordinates: e ? e.endCoordinates : null,
startCoordinates: e ? e.startCoordinates : null
})
}
useEffect(() => {
keyboardDidShowListener = Keyboard.addListener(
'keyboardWillShow',
keyboardHidden
)
keyboardDidShowListener = Keyboard.addListener(
'keyboardDidShow',
keyboardShown
)
keyboardDidShowListener = Keyboard.addListener(
'keyboardWillHide',
keyboardShown
)
keyboardDidHideListener = Keyboard.addListener(
'keyboardDidHide',
keyboardHidden
)
return () => {
keyboardDidShowListener.remove()
keyboardDidHideListener.remove()
}
}, [])
return keyboard
}