|
1 | 1 | import React, { useEffect, useState } from 'react' |
2 | 2 | import { Keyboard, KeyboardEventListener, ScreenRect } from 'react-native' |
3 | 3 |
|
4 | | - |
5 | 4 | export default function useKeyboard() { |
6 | | - const [shown, setShown] = useState(false) |
7 | | - const [coordinates, setCoordinates] = useState<{ start: ScreenRect, end: ScreenRect }>({ |
8 | | - start: { screenX:0, screenY: 0, width: 0, height: 0 }, |
9 | | - end: { screenX:0, screenY: 0, width: 0, height: 0 }, |
10 | | - }) |
| 5 | + const [shown, setShown] = useState(false) |
| 6 | + const [coordinates, setCoordinates] = useState<{ |
| 7 | + start: ScreenRect |
| 8 | + end: ScreenRect |
| 9 | + }>({ |
| 10 | + start: { screenX: 0, screenY: 0, width: 0, height: 0 }, |
| 11 | + end: { screenX: 0, screenY: 0, width: 0, height: 0 }, |
| 12 | + }) |
11 | 13 |
|
12 | 14 | const handleKeyboardWillShow: KeyboardEventListener = (e) => { |
13 | | - setCoordinates({ start: e.startCoordinates, end: e.endCoordinates }) |
| 15 | + setCoordinates({ start: e.startCoordinates, end: e.endCoordinates }) |
14 | 16 | } |
15 | 17 | const handleKeyboardDidShow: KeyboardEventListener = (e) => { |
16 | | - setShown(true) |
17 | | - setCoordinates({ start: e.startCoordinates, end: e.endCoordinates }) |
| 18 | + setShown(true) |
| 19 | + setCoordinates({ start: e.startCoordinates, end: e.endCoordinates }) |
18 | 20 | } |
19 | 21 | const handleKeyboardWillHide: KeyboardEventListener = (e) => { |
20 | | - setCoordinates({ start: e.startCoordinates, end: e.endCoordinates }) |
| 22 | + setCoordinates({ start: e.startCoordinates, end: e.endCoordinates }) |
21 | 23 | } |
22 | 24 | const handleKeyboardDidHide: KeyboardEventListener = (e) => { |
23 | | - setShown(false) |
24 | | - setCoordinates({ start: e.startCoordinates, end: e.endCoordinates }) |
| 25 | + setShown(false) |
| 26 | + setCoordinates({ start: e.startCoordinates, end: e.endCoordinates }) |
25 | 27 | } |
26 | 28 |
|
27 | 29 | useEffect(() => { |
28 | | - const keyboardWillShowListener = Keyboard.addListener('keyboardWillShow', handleKeyboardWillShow) |
29 | | - const keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', handleKeyboardDidShow) |
30 | | - const keyboardWillHideListener = Keyboard.addListener('keyboardWillHide', handleKeyboardWillHide) |
31 | | - const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', handleKeyboardDidHide) |
| 30 | + const keyboardWillShowListener = Keyboard.addListener( |
| 31 | + 'keyboardWillShow', |
| 32 | + handleKeyboardWillShow, |
| 33 | + ) |
| 34 | + const keyboardDidShowListener = Keyboard.addListener( |
| 35 | + 'keyboardDidShow', |
| 36 | + handleKeyboardDidShow, |
| 37 | + ) |
| 38 | + const keyboardWillHideListener = Keyboard.addListener( |
| 39 | + 'keyboardWillHide', |
| 40 | + handleKeyboardWillHide, |
| 41 | + ) |
| 42 | + const keyboardDidHideListener = Keyboard.addListener( |
| 43 | + 'keyboardDidHide', |
| 44 | + handleKeyboardDidHide, |
| 45 | + ) |
32 | 46 |
|
33 | 47 | return () => { |
34 | | - keyboardWillShowListener.remove() |
35 | | - keyboardDidShowListener.remove() |
36 | | - keyboardWillHideListener.remove() |
| 48 | + keyboardWillShowListener.remove() |
| 49 | + keyboardDidShowListener.remove() |
| 50 | + keyboardWillHideListener.remove() |
37 | 51 | keyboardDidHideListener.remove() |
38 | 52 | } |
39 | 53 | }, []) |
40 | 54 |
|
41 | 55 | return { |
42 | | - keyboardShown: shown, |
43 | | - coordinates, |
| 56 | + keyboardShown: shown, |
| 57 | + coordinates, |
44 | 58 | } |
45 | 59 | } |
0 commit comments