File tree Expand file tree Collapse file tree 4 files changed +66
-3
lines changed
apps/common-app/src/examples
packages/react-native-reanimated/Common/cpp/reanimated/Fabric Expand file tree Collapse file tree 4 files changed +66
-3
lines changed Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+ import { StyleSheet , Text } from 'react-native' ;
3
+ import Animated , {
4
+ useAnimatedScrollHandler ,
5
+ useAnimatedStyle ,
6
+ useSharedValue ,
7
+ } from 'react-native-reanimated' ;
8
+
9
+ const styles = StyleSheet . create ( {
10
+ stickyHeader : {
11
+ height : 80 ,
12
+ backgroundColor : 'navy' ,
13
+ } ,
14
+ listItem : {
15
+ padding : 16 ,
16
+ } ,
17
+ } ) ;
18
+
19
+ export default function StickyHeaderExample ( ) {
20
+ const offset = useSharedValue ( 0 ) ;
21
+
22
+ const scrollHandler = useAnimatedScrollHandler ( ( event ) => {
23
+ offset . value = event . contentOffset . y ;
24
+ } ) ;
25
+
26
+ const animatedStyle = useAnimatedStyle ( ( ) => {
27
+ return { transform : [ { translateY : offset . value } ] } ;
28
+ } ) ;
29
+
30
+ return (
31
+ < Animated . ScrollView onScroll = { scrollHandler } >
32
+ < Animated . View style = { [ styles . stickyHeader , animatedStyle ] } />
33
+ { Array . from ( { length : 100 } ) . map ( ( _ , i ) => (
34
+ < Text key = { i } style = { styles . listItem } >
35
+ Item { i + 1 }
36
+ </ Text >
37
+ ) ) }
38
+ </ Animated . ScrollView >
39
+ ) ;
40
+ }
Original file line number Diff line number Diff line change @@ -124,6 +124,7 @@ import RestoreStateExample from './SharedElementTransitions/RestoreState';
124
124
import TabNavigatorExample from './SharedElementTransitions/TabNavigatorExample' ;
125
125
import TransitionRestartExample from './SharedElementTransitions/TransitionRestart' ;
126
126
import SharedStyleExample from './SharedStyleExample' ;
127
+ import StickyHeaderExample from './StickyHeaderExample' ;
127
128
import StrictDOMExample from './StrictDOMExample' ;
128
129
import SvgExample from './SvgExample' ;
129
130
import SwipeableListExample from './SwipeableListExample' ;
@@ -380,6 +381,11 @@ export const EXAMPLES: Record<string, Example> = {
380
381
title : 'useScrollViewOffset' ,
381
382
screen : ScrollViewOffsetExample ,
382
383
} ,
384
+ StickyHeaderExample : {
385
+ icon : '🔝' ,
386
+ title : 'Sticky header' ,
387
+ screen : StickyHeaderExample ,
388
+ } ,
383
389
DispatchCommandExample : {
384
390
icon : '🫡' ,
385
391
title : 'Dispatch command' ,
Original file line number Diff line number Diff line change @@ -52,7 +52,12 @@ void ReanimatedCommitHook::maybeInitializeLayoutAnimations(
52
52
RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit (
53
53
ShadowTree const &,
54
54
RootShadowNode::Shared const &,
55
- RootShadowNode::Unshared const &newRootShadowNode) noexcept {
55
+ RootShadowNode::Unshared const &newRootShadowNode
56
+ #if REACT_NATIVE_MINOR_VERSION >= 80
57
+ ,
58
+ const ShadowTreeCommitOptions &commitOptions
59
+ #endif
60
+ ) noexcept {
56
61
maybeInitializeLayoutAnimations (newRootShadowNode->getSurfaceId ());
57
62
58
63
auto reaShadowNode =
@@ -89,8 +94,15 @@ RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit(
89
94
// PropsRegistry.
90
95
// This is very important, since if we didn't pause Reanimated commits,
91
96
// it could lead to RN commits being delayed until the animation is finished
92
- // (very bad).
97
+ // (very bad). We don't pause Reanimated commits for state updates coming
98
+ // from React Native as this would break sticky header animations.
99
+ #if REACT_NATIVE_MINOR_VERSION >= 80
100
+ if (commitOptions.source == ShadowTreeCommitSource::React) {
101
+ propsRegistry_->pauseReanimatedCommits ();
102
+ }
103
+ #else
93
104
propsRegistry_->pauseReanimatedCommits ();
105
+ #endif
94
106
}
95
107
96
108
return rootNode;
Original file line number Diff line number Diff line change @@ -32,7 +32,12 @@ class ReanimatedCommitHook
32
32
RootShadowNode::Unshared shadowTreeWillCommit (
33
33
ShadowTree const &shadowTree,
34
34
RootShadowNode::Shared const &oldRootShadowNode,
35
- RootShadowNode::Unshared const &newRootShadowNode) noexcept override ;
35
+ RootShadowNode::Unshared const &newRootShadowNode
36
+ #if REACT_NATIVE_MINOR_VERSION >= 80
37
+ ,
38
+ const ShadowTreeCommitOptions &commitOptions
39
+ #endif
40
+ ) noexcept override ;
36
41
37
42
private:
38
43
std::shared_ptr<PropsRegistry> propsRegistry_;
You can’t perform that action at this time.
0 commit comments