Skip to content
Open
Changes from 1 commit
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
19 changes: 14 additions & 5 deletions src/components/KeyboardAwareScrollView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export type KeyboardAwareScrollViewProps = {
enabled?: boolean;
/** Adjusting the bottom spacing of KeyboardAwareScrollView. Default is `0` */
extraKeyboardSpace?: number;
/** Used if inverted Flatlist/Flashlist is being used */
inverted?: boolean;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this property can be derived from style property?

style?.transform?.some(({scaleY}) => scaleY === -1);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I am missing how to call style, but when I call it as is in the component, I get

"Cannot find name 'style'."

and in rest.style I have only
"style": {"backgroundColor": "#FFFFFF", "flex": 1}} so ni scaleY.

But this inverted prop should be ok because it is a Flatlist/Flashlist prop. Right?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lpatrun can you show me how do you use FlatList and KeyboardAwareScrollView?

I assumed it would be something like that:

<FlatList
  data={[]}
  renderItem={() => <></>}
  inverted
  renderScrollComponent={(props) => <KeyboardAwareScrollView {...props} />}
/>

And here (if I understand correctly) props.style should contain transform property (it should be injected by FlatList). Or no? 🙈

Copy link
Author

@lpatrun lpatrun Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the code from the app

 <FlatList
   contentContainerStyle={styles.dataStyles}
   data={[...messages].reverse()}
   inverted
   renderItem={({ item }) => renderChatElement(item)}
   ItemSeparatorComponent={() => <SpacingComponent val={4} />}
   renderScrollComponent={props => <KeyboardAwareScrollView {...props} />}
   ListHeaderComponent={() => (isLoading ? <Loading /> : <></>)}
/>

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lpatrun

I'll check it today! ❤️

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lpatrun

I added this code to KeyboardAwareScrollView (it has TS errors):

    const inverted = Array.isArray(rest.style)
      ? rest.style?.[0]?.transform[0]?.scaleY === -1
      : false;

    console.log("rest", rest.style, inverted);

And this code I used for testing:

    <FlatList
      // contentContainerStyle={{flex: 1}}
      // style={{flexGrow: 1}}
      data={[1]}
      inverted
      renderItem={({ item }) => <></>}
      renderScrollComponent={props => <KeyboardAwareScrollView {...props} />}
  />

And it seems to work fine:

image

Can you check on your side again?

/** Ajdusting the offset for the content when keyboard is shown on inverted Flatlist/Flashlist on screen with BottomTab navigation */
tabBarHeight?: number;
/** Custom component for `ScrollView`. Default is `ScrollView` */
ScrollViewComponent?: React.ComponentType<ScrollViewProps>;
} & ScrollViewProps;
Expand Down Expand Up @@ -94,6 +98,8 @@ const KeyboardAwareScrollView = forwardRef<
extraKeyboardSpace = 0,
ScrollViewComponent = Reanimated.ScrollView,
snapToOffsets,
inverted = false,
tabBarHeight = 0,
...rest
},
ref,
Expand Down Expand Up @@ -349,18 +355,20 @@ const KeyboardAwareScrollView = forwardRef<
);

const view = useAnimatedStyle(
() =>
enabled
() => {
const invertedOffset = inverted ? -tabBarHeight : 0;
return enabled
? {
// animations become choppy when scrolling to the end of the `ScrollView` (when the last input is focused)
// this happens because the layout recalculates on every frame. To avoid this we slightly increase padding
// by `+1`. In this way we assure, that `scrollTo` will never scroll to the end, because it uses interpolation
// from 0 to `keyboardHeight`, and here our padding is `keyboardHeight + 1`. It allows us not to re-run layout
// re-calculation on every animation frame and it helps to achieve smooth animation.
// see: https://github.com/kirillzyusko/react-native-keyboard-controller/pull/342
paddingBottom: currentKeyboardFrameHeight.value + 1,
paddingBottom: currentKeyboardFrameHeight.value + invertedOffset + 1,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why in case of inverted we do need to remove extraKeyboardSpace?

}
: {},
: {}
},
[enabled],
);

Expand All @@ -371,8 +379,9 @@ const KeyboardAwareScrollView = forwardRef<
scrollEventThrottle={16}
onLayout={onScrollViewLayout}
>
{inverted ? <Reanimated.View style={view} /> : null}
{children}
<Reanimated.View style={view} />
{!inverted ? <Reanimated.View style={view} /> : null}
</ScrollViewComponent>
);
},
Expand Down