@@ -32,6 +32,7 @@ import { Platform } from 'react-native';
3232
3333import { useKeyboard } from './useKeyboard' ;
3434import { useSafeAreaInsets } from 'react-native-safe-area-context' ;
35+ import { useOrientation } from './useOrientation' ;
3536
3637export type RefType = TextInput | View | Animated . View | null ;
3738
@@ -58,7 +59,7 @@ function Wrapper(props: PropsWithChildren<{}>) {
5859 style = { styles . wrapper }
5960 ref = { wrapperRef }
6061 onLayout = { ( { nativeEvent } ) => {
61- if ( nativeEvent . layout . height < windowDimensions . height ) {
62+ if ( nativeEvent . layout . height !== windowDimensions . height ) {
6263 setWrapperOffset ( windowDimensions . height - nativeEvent . layout . height ) ;
6364 }
6465 } }
@@ -94,6 +95,7 @@ const SmartScrollContext = React.createContext<{
9495 inputs : InputType ;
9596 setInputs : React . Dispatch < React . SetStateAction < InputType > > ;
9697 currentFocus ?: null | Elements [ 0 ] ;
98+ clearFocus : ( ) => void ;
9799} | null > ( null ) ;
98100
99101const SmartScrollProvider = ( { children } : { children : React . ReactNode } ) => {
@@ -113,6 +115,18 @@ const SmartScrollProvider = ({ children }: { children: React.ReactNode }) => {
113115 [ elements ]
114116 ) ;
115117
118+ const clearFocus = useCallback ( ( ) => {
119+ if ( ! currentFocus ) return ;
120+
121+ setElements ( ( els ) => ( {
122+ ...els ,
123+ [ currentFocus . name ] : {
124+ ...currentFocus ,
125+ isFocus : false ,
126+ } ,
127+ } ) ) ;
128+ } , [ elements , currentFocus ] ) ;
129+
116130 // we have a flick on first focus so we make the scrollview wait a bit before animate
117131 useLayoutEffect ( ( ) => {
118132 if ( currentFocus && ! isReady ) {
@@ -136,6 +150,7 @@ const SmartScrollProvider = ({ children }: { children: React.ReactNode }) => {
136150 currentFocus,
137151 inputs,
138152 setInputs,
153+ clearFocus,
139154 } }
140155 >
141156 { children }
@@ -204,6 +219,7 @@ export function useFormSmartScroll({
204219 wrapperOffset,
205220 inputs,
206221 setInputs,
222+ clearFocus,
207223 } = useSmartScrollContext ( ) ;
208224
209225 const _keyboard = useKeyboard ( ) ;
@@ -214,6 +230,17 @@ export function useFormSmartScroll({
214230
215231 const translateY = useSharedValue ( 0 ) ;
216232
233+ const orientation = useOrientation ( ) ;
234+
235+ const keyboardEnd = _keyboard . coordinates . end . screenY ;
236+
237+ useEffect ( ( ) => {
238+ translateY . value = 0 ;
239+ scrollY . value = 0 ;
240+ clearFocus ( ) ;
241+ Keyboard . dismiss ( ) ;
242+ } , [ orientation ] ) ;
243+
217244 useEffect ( ( ) => {
218245 const sub = Keyboard . addListener ( 'keyboardWillHide' , ( ) => {
219246 translateY . value = withTiming ( 0 ) ;
@@ -230,14 +257,11 @@ export function useFormSmartScroll({
230257 if ( ! focus ) return ;
231258
232259 if ( isAndroid ) {
233- if (
234- focus . position + wrapperOffset >
235- _keyboard . coordinates . end . screenY - focus . height * 2
236- ) {
260+ if ( focus . position + wrapperOffset > keyboardEnd - focus . height * 2 ) {
237261 if ( wrapperOffset ) {
238262 const diff =
239263 Math . abs (
240- _keyboard . coordinates . end . screenY -
264+ keyboardEnd -
241265 focus . position -
242266 focus . height -
243267 padding +
@@ -259,15 +283,16 @@ export function useFormSmartScroll({
259283
260284 if (
261285 focus . position + wrapperOffset >
262- _keyboard . coordinates . end . screenY - focus . height + scrollY . value
286+ keyboardEnd - focus . height + scrollY . value
263287 ) {
264288 const diff = Math . abs (
265- _keyboard . coordinates . end . screenY -
289+ keyboardEnd -
266290 focus . position -
267291 focus . height -
268292 padding +
269293 scrollY . value -
270- wrapperOffset
294+ wrapperOffset -
295+ insets . bottom
271296 ) ;
272297 translateY . value = withTiming ( - diff ) ;
273298
0 commit comments