Gathering detailed insights and metrics for @codler/react-native-keyboard-aware-scroll-view
Gathering detailed insights and metrics for @codler/react-native-keyboard-aware-scroll-view
Gathering detailed insights and metrics for @codler/react-native-keyboard-aware-scroll-view
Gathering detailed insights and metrics for @codler/react-native-keyboard-aware-scroll-view
A ScrollView component that handles keyboard appearance and automatically scrolls to focused TextInput.
npm install @codler/react-native-keyboard-aware-scroll-view
Typescript
Module System
Node Version
NPM Version
57.8
Supply Chain
30.1
Quality
65.4
Maintenance
50
Vulnerability
94.1
License
JavaScript (100%)
Total Downloads
4,921,677
Last Day
203
Last Week
10,726
Last Month
41,054
Last Year
554,240
38 Stars
177 Commits
17 Forks
2 Watchers
3 Branches
1 Contributors
Updated on Sep 11, 2024
Latest Version
2.0.1
Package Id
@codler/react-native-keyboard-aware-scroll-view@2.0.1
Unpacked Size
48.97 kB
Size
12.87 kB
File Count
13
NPM Version
8.1.0
Node Version
16.13.0
Published on
Dec 10, 2021
Cumulative downloads
Total Downloads
Last Day
-11%
203
Compared to previous day
Last Week
3.2%
10,726
Compared to previous week
Last Month
3.5%
41,054
Compared to previous month
Last Year
-20.8%
554,240
Compared to previous year
1
1
A ScrollView component that handles keyboard appearance and automatically scrolls to focused TextInput
.
v2.0.0
requires RN>=0.65.1
v1.0.0
requires RN>=0.63.0
Installation can be done through npm
:
1npm i @codler/react-native-keyboard-aware-scroll-view --save
You can use the KeyboardAwareScrollView
, KeyboardAwareSectionList
or the KeyboardAwareFlatList
components. They accept ScrollView
, SectionList
and FlatList
default props respectively and
implement a custom high order component called KeyboardAwareHOC
to handle keyboard appearance.
The high order component is also available if you want to use it in any other component.
Import react-native-keyboard-aware-scroll-view
and wrap your content inside
it:
1import { KeyboardAwareScrollView } from '@codler/react-native-keyboard-aware-scroll-view'
1<KeyboardAwareScrollView> 2 <View> 3 <TextInput /> 4 </View> 5</KeyboardAwareScrollView>
TextInput
fieldsAs of v0.1.0
, the component auto scrolls to the focused TextInput
????. For versions v0.0.7
and older you can do the following.
TextInput
In order to scroll to any TextInput
field, you can use the built-in method scrollToFocusedInput
. Example:
1_scrollToInput (reactNode: any) { 2 // Add a 'scroll' ref to your ScrollView 3 this.scroll.props.scrollToFocusedInput(reactNode) 4}
1<KeyboardAwareScrollView 2 innerRef={ref => { 3 this.scroll = ref 4 }}> 5 <View> 6 <TextInput 7 onFocus={(event: Event) => { 8 // `bind` the function if you're using ES6 classes 9 this._scrollToInput(ReactNative.findNodeHandle(event.target)) 10 }} 11 /> 12 </View> 13</KeyboardAwareScrollView>
There's another built-in function that lets you programatically scroll to any position of the scroll view:
1this.scroll.props.scrollToPosition(0, 0)
You can register to ScrollViewResponder
events onKeyboardWillShow
and onKeyboardWillHide
:
1<KeyboardAwareScrollView 2 onKeyboardWillShow={(frames: Object) => { 3 console.log('Keyboard event', frames) 4 }}> 5 <View> 6 <TextInput /> 7 </View> 8</KeyboardAwareScrollView>
First, Android natively has this feature, you can easily enable it by setting windowSoftInputMode
in AndroidManifest.xml
. Check here.
But if you want to use feature like extraHeight
, you need to enable Android Support with the following steps:
0.46
or above.windowSoftInputMode
to adjustPan
in AndroidManifest.xml
.enableOnAndroid
property to true
.Android Support is not perfect, here is the supported list:
Prop | Android Support |
---|---|
viewIsInsideTabBar | Yes |
resetScrollToCoords | Yes |
enableAutomaticScroll | Yes |
extraHeight | Yes |
extraScrollHeight | Yes |
enableResetScrollToCoords | Yes |
keyboardOpeningTime | No |
All the ScrollView
/FlatList
props will be passed.
Prop | Type | Description |
---|---|---|
innerRef | Function | Catch the reference of the component. |
viewIsInsideTabBar | boolean | Adds an extra offset that represents the TabBarIOS height. |
resetScrollToCoords | Object: {x: number, y: number} | Coordinates that will be used to reset the scroll when the keyboard hides. |
enableAutomaticScroll | boolean | When focus in TextInput will scroll the position, default is enabled. |
extraHeight | number | Adds an extra offset when focusing the TextInput s. |
extraScrollHeight | number | Adds an extra offset to the keyboard. Useful if you want to stick elements above the keyboard. |
enableResetScrollToCoords | boolean | Lets the user enable or disable automatic resetScrollToCoords. |
keyboardOpeningTime | number | Sets the delay time before scrolling to new position, default is 250 |
enableOnAndroid | boolean | Enable Android Support |
Use innerRef
to get the component reference and use this.scrollRef.props
to access these methods.
Method | Parameter | Description |
---|---|---|
getScrollResponder | void | Get ScrollResponder |
scrollToPosition | x: number, y: number, animated: bool = true | Scroll to specific position with or without animation. |
scrollToEnd | animated?: bool = true | Scroll to end with or without animation. |
scrollIntoView | element: React.Element<*>, options: { getScrollPosition: ?(parentLayout, childLayout, contentOffset) => { x: number, y: number, animated: boolean } } | Scrolls an element inside a KeyboardAwareScrollView into view. |
Enabling any component to be keyboard-aware is very easy. Take a look at the code of KeyboardAwareFlatList
:
1/* @flow */ 2 3import { FlatList } from 'react-native' 4import listenToKeyboardEvents from './KeyboardAwareHOC' 5 6export default listenToKeyboardEvents(FlatList)
The HOC can also be configured. Sometimes it's more convenient to provide a static config than configuring the behavior with props. This HOC config can be overriden with props.
1/* @flow */ 2 3import { FlatList } from 'react-native' 4import listenToKeyboardEvents from './KeyboardAwareHOC' 5 6const config = { 7 enableOnAndroid: true, 8 enableAutomaticScroll: true 9} 10 11export default listenToKeyboardEvents(config)(FlatList)
The available config options are:
1{ 2 enableOnAndroid: boolean, 3 contentContainerStyle: ?Object, 4 enableAutomaticScroll: boolean, 5 extraHeight: number, 6 extraScrollHeight: number, 7 enableResetScrollToCoords: boolean, 8 keyboardOpeningTime: number, 9 viewIsInsideTabBar: boolean, 10 refPropName: string, 11 extractNativeRef: Function 12}
No vulnerabilities found.