Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add animatedKeyboardHeight in useKeyboard hook #140

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ yarn add @react-native-community/hooks
- [useInteractionManager](https://github.com/react-native-community/hooks#useinteractionmanager)
- [useDeviceOrientation](https://github.com/react-native-community/hooks#usedeviceorientation)
- [useLayout](https://github.com/react-native-community/hooks#uselayout)
- [useAnimatedValue](https://github.com/react-native-community/hooks#useAnimatedValue)

### `useAccessibilityInfo`

Expand Down Expand Up @@ -113,6 +114,9 @@ const keyboard = useKeyboard()

console.log('keyboard isKeyboardShow: ', keyboard.keyboardShown)
console.log('keyboard keyboardHeight: ', keyboard.keyboardHeight)
console.log('keyboard animatedKeyboardHeight: ', keyboard.animatedKeyboardHeight)

<Animated.View style={{height: keyboard.animatedKeyboardHeight}} />
```

### `useInteractionManager`
Expand Down Expand Up @@ -148,6 +152,25 @@ console.log('layout: ', layout)
<View onLayout={onLayout} style={{width: 200, height: 200, marginTop: 30}} />
```

### `useAnimatedValue`

```js
import { useAnimatedValue } from '@react-native-community/hooks'

const animatedValue = useAnimatedValue(0)

useEffect(() => {
Animated.timing(animatedKeyboardHeight, {
magrinj marked this conversation as resolved.
Show resolved Hide resolved
duration: 250,
toValue: 200,
}).start()
}, [])

console.log('value: ', animatedValue)

<Animated.View onLayout={onLayout} style={{width: 200, height: animatedValue, marginTop: 30}} />
```

[version-badge]: https://img.shields.io/npm/v/@react-native-community/hooks.svg?style=flat-square
[package]: https://www.npmjs.com/package/@react-native-community/hooks

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {useKeyboard} from './useKeyboard'
import {useInteractionManager} from './useInteractionManager'
import {useDeviceOrientation} from './useDeviceOrientation'
import {useLayout} from './useLayout'
import {useAnimatedValue} from './useAnimatedValue'

export {
useDimensions,
Expand All @@ -20,4 +21,5 @@ export {
useInteractionManager,
useDeviceOrientation,
useLayout,
useAnimatedValue,
}
7 changes: 7 additions & 0 deletions src/useAnimatedValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {useRef} from 'react'
import {Animated} from 'react-native'

export const useAnimatedValue = (initialValue: number): Animated.Value => {
const ref = useRef(new Animated.Value(initialValue))
return ref.current
}
25 changes: 24 additions & 1 deletion src/useKeyboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import {useEffect, useState} from 'react'
import {Keyboard, KeyboardEventListener, ScreenRect} from 'react-native'
import {
Keyboard,
KeyboardEventListener,
ScreenRect,
Animated,
} from 'react-native'

import {useAnimatedValue} from './useAnimatedValue'

export function useKeyboard() {
const [shown, setShown] = useState(false)
Expand All @@ -11,9 +18,16 @@ export function useKeyboard() {
end: {screenX: 0, screenY: 0, width: 0, height: 0},
})
const [keyboardHeight, setKeyboardHeight] = useState<number>(0)
const animatedKeyboardHeight = useAnimatedValue(0)

const handleKeyboardWillShow: KeyboardEventListener = (e) => {
setCoordinates({start: e.startCoordinates, end: e.endCoordinates})

// Start raise keyboard animated value
Animated.timing(animatedKeyboardHeight, {
duration: e.duration,
toValue: e.endCoordinates.height,
}).start()
}
const handleKeyboardDidShow: KeyboardEventListener = (e) => {
setShown(true)
Expand All @@ -22,6 +36,12 @@ export function useKeyboard() {
}
const handleKeyboardWillHide: KeyboardEventListener = (e) => {
setCoordinates({start: e.startCoordinates, end: e.endCoordinates})

// Start close keyboard animated value
Animated.timing(animatedKeyboardHeight, {
duration: e.duration,
toValue: 0,
}).start()
}
const handleKeyboardDidHide: KeyboardEventListener = (e) => {
setShown(false)
Expand All @@ -42,12 +62,15 @@ export function useKeyboard() {
Keyboard.removeListener('keyboardDidShow', handleKeyboardDidShow)
Keyboard.removeListener('keyboardWillHide', handleKeyboardWillHide)
Keyboard.removeListener('keyboardDidHide', handleKeyboardDidHide)
animatedKeyboardHeight.stopAnimation()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return {
keyboardShown: shown,
coordinates,
keyboardHeight,
animatedKeyboardHeight,
}
}