Skip to content

Commit c0a5bda

Browse files
authored
Added extraScrollHeight prop to handle elements above keyboard (#76)
* Added extraScrollHeight prop to handle elements above keyboard * Updated README
1 parent 8c6423d commit c0a5bda

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ All the `ScrollView`/`ListView` props will be passed.
9595
|----------|----------|-----------------|
9696
| `viewIsInsideTabBar` | `boolean` | Adds an extra offset that represents the `TabBarIOS` height. |
9797
| `resetScrollToCoords` | `Object: {x: number, y: number}` | Coordinates that will be used to reset the scroll when the keyboard hides. |
98-
| `enableAutoAutomaticScroll` | `boolean` | When focus in TextInput will scroll the position, default is enabled. |
99-
| `extraHeight` | `number` | Adds an extra offset |
98+
| `enableAutoAutomaticScroll` | `boolean` | When focus in `TextInput` will scroll the position, default is enabled. |
99+
| `extraHeight` | `number` | Adds an extra offset when focusing the `TextInput`s. |
100+
| `extraScrollHeight` | `number` | Adds an extra offset to the keyboard. Useful if you want to stick elements above the keyboard. |
100101
101102
## License
102103

lib/KeyboardAwareMixin.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ import { PropTypes } from 'react'
44
import ReactNative, { TextInput, Keyboard, UIManager } from 'react-native'
55
import TimerMixin from 'react-timer-mixin'
66

7-
const _KAM_DEFAULT_TAB_BAR_HEIGHT = 49
8-
const _KAM_KEYBOARD_OPENING_TIME = 250
9-
const _KAM_EXTRA_HEIGHT = 75
7+
const _KAM_DEFAULT_TAB_BAR_HEIGHT: number = 49
8+
const _KAM_KEYBOARD_OPENING_TIME: number = 250
9+
const _KAM_EXTRA_HEIGHT: number = 75
1010

1111
const KeyboardAwareMixin = {
1212
mixins: [TimerMixin],
1313
propTypes: {
1414
enableAutoAutomaticScroll: PropTypes.bool,
1515
extraHeight: PropTypes.number,
16+
extraScrollHeight: PropTypes.number,
1617
},
1718

1819
getDefaultProps: function () {
1920
return {
20-
enableAutoAutomaticScroll: true,
21-
extraHeight: _KAM_EXTRA_HEIGHT,
21+
enableAutoAutomaticScroll: true,
22+
extraHeight: _KAM_EXTRA_HEIGHT,
23+
extraScrollHeight: 0,
2224
}
2325
},
2426

@@ -42,43 +44,40 @@ const KeyboardAwareMixin = {
4244

4345
// Keyboard actions
4446
updateKeyboardSpace: function (frames: Object) {
45-
const keyboardSpace = (this.props.viewIsInsideTabBar) ? frames.endCoordinates.height - _KAM_DEFAULT_TAB_BAR_HEIGHT : frames.endCoordinates.height
46-
this.setState({
47-
keyboardSpace: keyboardSpace,
48-
})
47+
let keyboardSpace: number = frames.endCoordinates.height + this.props.extraScrollHeight
48+
if (this.props.viewIsInsideTabBar) {
49+
keyboardSpace -= _KAM_DEFAULT_TAB_BAR_HEIGHT
50+
}
51+
this.setState({keyboardSpace})
4952
// Automatically scroll to focused TextInput
5053
if (this.props.enableAutoAutomaticScroll) {
5154
const currentlyFocusedField = TextInput.State.currentlyFocusedField()
5255
if (!currentlyFocusedField) {
5356
return
5457
}
55-
5658
UIManager.viewIsDescendantOf(
5759
currentlyFocusedField,
5860
this.getScrollResponder().getInnerViewNode(),
5961
(isAncestor) => {
6062
if (isAncestor) {
6163
// Check if the TextInput will be hidden by the keyboard
6264
UIManager.measureInWindow(currentlyFocusedField, (x, y, width, height) => {
63-
if (y + height > frames.endCoordinates.screenY) {
65+
if (y + height > frames.endCoordinates.screenY - this.props.extraScrollHeight - this.props.extraHeight) {
6466
this.scrollToFocusedInputWithNodeHandle(currentlyFocusedField)
6567
}
6668
})
6769
}
6870
}
6971
)
7072
}
71-
7273
if (!this.resetCoords) {
7374
this.defaultResetScrollToCoords = this.position
7475
}
7576
},
7677

7778
resetKeyboardSpace: function () {
78-
const keyboardSpace = (this.props.viewIsInsideTabBar) ? _KAM_DEFAULT_TAB_BAR_HEIGHT : 0
79-
this.setState({
80-
keyboardSpace: keyboardSpace,
81-
})
79+
const keyboardSpace: number = (this.props.viewIsInsideTabBar) ? _KAM_DEFAULT_TAB_BAR_HEIGHT : 0
80+
this.setState({keyboardSpace})
8281
// Reset scroll position after keyboard dismissal
8382
if (this.resetCoords) {
8483
this.scrollToPosition(this.resetCoords.x, this.resetCoords.y, true)
@@ -117,7 +116,7 @@ const KeyboardAwareMixin = {
117116

118117
scrollToFocusedInputWithNodeHandle: function (nodeID: number, extraHeight: number = this.props.extraHeight) {
119118
const reactNode = ReactNative.findNodeHandle(nodeID)
120-
this.scrollToFocusedInput(reactNode, extraHeight)
119+
this.scrollToFocusedInput(reactNode, extraHeight + this.props.extraScrollHeight)
121120
},
122121

123122
position: {x: 0, y: 0},

0 commit comments

Comments
 (0)