-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
allow custom StickyHeader in ScrollView-based components #25428
allow custom StickyHeader in ScrollView-based components #25428
Conversation
@@ -18,7 +18,7 @@ import type {LayoutEvent} from '../../Types/CoreEventTypes'; | |||
|
|||
const AnimatedView = AnimatedImplementation.createAnimatedComponent(View); | |||
|
|||
type Props = { | |||
export type ScrolViewStickyHeaderProps = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: ScrolView
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would prefer calling this Props
and then using import type {Props as ScrollViewStickyHeaderProps}
where it is being imported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cpojer done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you fix the typo in the type? :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
This pull request was successfully merged by @vonovak in e6c7846. When will my fix make it into a release? | Upcoming Releases |
) Summary: This PR adds support for custom `StickyHeaderComponent` to be used in ScrollView (and by extension in FlatList, SectionList..). Motivation: I've been working on a FlatList with hidable header that has a search field in it. Something like https://medium.com/appandflow/react-native-collapsible-navbar-e51a049b560a but using a FlatList w/ pull-to-refresh. The implementation can be found at https://snack.expo.io/vonovak/hidable-header-flatlist . I used the `ListHeaderComponent` prop to render the custom header - as opposed to absolute positioning which is used in the linked article because I also need the loading indicator (I added `refreshing` and `onRefresh` for that) to show up above the header. I proceeded by adding `stickyHeaderIndices={[0]}` to keep the header at the top, which seems to be the idiomatic way to do so. Then I added `Animated.View` with custom translation logic to the rendered header. All appears to be working fine at the first sight - when you tap any item, you'll see it react to touch (red underlay). You'll also see the header becomes hidden if I scroll far enough and appears again after scrolling up. BUT - when you scroll down so that the header becomes hidden and tap the first visible item in the list, it will not react to touches! The reason is that `ScrollViewStickyHeader` https://github.com/facebook/react-native/blob/9a84970c35d22b68fb3d8eac019c7f415a14c888/Libraries/Components/ScrollView/ScrollView.js#L984 has its own translation logic and when I tap onto the item at the top of the list, it seems like I'm tapping the item but I'm in fact tapping that `ScrollViewStickyHeader`. I tried working around this by not specifying `stickyHeaderIndices={[0]}` and using `ListHeaderComponentStyle` prop (this needed some additional changes in https://github.com/facebook/react-native/blob/9a84970c35d22b68fb3d8eac019c7f415a14c888/Libraries/Lists/VirtualizedList.js#L786, and the animation is junky for some reason - as if the header always needed to "catch up" with the scroll offset, causing jitter) and `CellRendererComponent` (junky animations too), but concluded that allowing to specify custom `StickyHeaderComponent` is the cleanest way to make something like this work. I'm slightly surprised I needed to do all this to make such a usual pattern work - am I missing something? ## Changelog [GENERAL] [ADDED] - allow custom StickyHeader in ScrollView-based components Pull Request resolved: facebook#25428 Test Plan: This is a minor change that should not break anything; tested locally. Differential Revision: D16073016 Pulled By: cpojer fbshipit-source-id: cdb878d12a426068dbaa9a54367c1190a6c55328
Summary
This PR adds support for custom
StickyHeaderComponent
to be used in ScrollView (and by extension in FlatList, SectionList..).Motivation: I've been working on a FlatList with hidable header that has a search field in it. Something like https://medium.com/appandflow/react-native-collapsible-navbar-e51a049b560a but using a FlatList w/ pull-to-refresh. The implementation can be found at https://snack.expo.io/@vonovak/hidable-header-flatlist .
I used the
ListHeaderComponent
prop to render the custom header - as opposed to absolute positioning which is used in the linked article because I also need the loading indicator (I addedrefreshing
andonRefresh
for that) to show up above the header.I proceeded by adding
stickyHeaderIndices={[0]}
to keep the header at the top, which seems to be the idiomatic way to do so. Then I addedAnimated.View
with custom translation logic to the rendered header.All appears to be working fine at the first sight - when you tap any item, you'll see it react to touch (red underlay). You'll also see the header becomes hidden if I scroll far enough and appears again after scrolling up. BUT - when you scroll down so that the header becomes hidden and tap the first visible item in the list, it will not react to touches! The reason is that
ScrollViewStickyHeader
react-native/Libraries/Components/ScrollView/ScrollView.js
Line 984 in 9a84970
has its own translation logic and when I tap onto the item at the top of the list, it seems like I'm tapping the item but I'm in fact tapping that
ScrollViewStickyHeader
.I tried working around this by not specifying
stickyHeaderIndices={[0]}
and usingListHeaderComponentStyle
prop (this needed some additional changes inreact-native/Libraries/Lists/VirtualizedList.js
Line 786 in 9a84970
CellRendererComponent
(junky animations too), but concluded that allowing to specify customStickyHeaderComponent
is the cleanest way to make something like this work. I'm slightly surprised I needed to do all this to make such a usual pattern work - am I missing something?Changelog
[GENERAL] [ADDED] - allow custom StickyHeader in ScrollView-based components
Test Plan
This is a minor change that should not break anything; tested locally.