Skip to content

Commit

Permalink
fix: prevent running onKeyboardWillShow when picker is closed (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakex7 committed May 2, 2023
1 parent 4619b01 commit faea36d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/EmojiPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const EmojiPicker = ({
const offsetY = React.useRef(new Animated.Value(0)).current
const height = React.useRef(new Animated.Value(getHeight(defaultHeight, screenHeight))).current
const additionalHeight = React.useRef(new Animated.Value(0)).current
const { keyboardVisible, keyboardHeight } = useKeyboard()
const { keyboardVisible, keyboardHeight } = useKeyboard(open)
const [isExpanded, setIsExpanded] = React.useState(false)

React.useEffect(() => {
Expand Down
26 changes: 15 additions & 11 deletions src/hooks/useKeyboard.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { useEffect, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'
import { Keyboard, KeyboardEvent } from 'react-native'

export const useKeyboard = () => {
export const useKeyboard = (isOpen: boolean) => {
const [keyboardVisible, setKeyboardVisible] = useState(false)
const [keyboardHeight, setKeyboardHeight] = useState(0)

function onKeyboardDidShow(e: KeyboardEvent) {
setKeyboardHeight(e.endCoordinates.height)
setKeyboardVisible(true)
}
const onKeyboardWillShow = useCallback(
(e: KeyboardEvent) => {
if (!isOpen) return
setKeyboardHeight(e.endCoordinates.height)
setKeyboardVisible(true)
},
[isOpen]
)

function onKeyboardDidHide() {
const onKeyboardWillHide = useCallback(() => {
setKeyboardHeight(0)
setKeyboardVisible(false)
}
}, [])

useEffect(() => {
const showSubscription = Keyboard.addListener('keyboardWillShow', onKeyboardDidShow)
const hideSubscription = Keyboard.addListener('keyboardWillHide', onKeyboardDidHide)
const showSubscription = Keyboard.addListener('keyboardWillShow', onKeyboardWillShow)
const hideSubscription = Keyboard.addListener('keyboardWillHide', onKeyboardWillHide)
return () => {
showSubscription.remove()
hideSubscription.remove()
}
}, [])
}, [onKeyboardWillHide, onKeyboardWillShow])

return { keyboardVisible, keyboardHeight }
}

0 comments on commit faea36d

Please sign in to comment.