Skip to content

Commit 77e79d6

Browse files
skinssharkfacebook-github-bot
authored andcommitted
Animated.ScrollView with RefreshControl applying Animated transform twice
Summary: There was a bug on Android when an Animated.ScrollView had a RefreshControl while an Animated style was applied, ie `transform`: ``` <Animated.ScrollView refreshControl={<RefreshControl />} style={{ transform: [{ translateY: new Animated.Value(200, {useNativeDriver: true}) }] }} /> ``` The transform value was being incorrectly applied twice. Since the styles were applied once on RefreshControl and once on NativeScrollView, the transform style is effectively applied twice: **1. ScrollView.js** - RefreshControl gets the transform through Fabric commit - [The RefreshControl gets wrapped around ScrollView](https://fburl.com/code/k60krxbj) while on iOS there is no change in the parent/child relationship. [Outer/inner styles are split and applied to RefreshControl/ScrollView](https://fburl.com/code/b2to75er), and transform styles are applied on the parent (RefreshControl) **2. createAnimatedComponent.js** - NativeScrollView gets the transform through Animated - [ScrollView forwards its ref to NativeScrollView](https://fburl.com/code/w1whtl5f), which means AnimatedComponent is setting the transform styles on NativeScrollView and not RefreshControl as ScrollView.js did This diff fixes this bug by using the `useAnimatedProps` hook which makes both RefreshControl and ScrollView components into animated components. Otherwise, the components don't know what to do with Animated values. --- Changelog: [Internal][Fixed] - Animated transform style properties were being applied twice when used on an Animated.ScrollView with RefreshControl on Android Reviewed By: javache Differential Revision: D38815633 fbshipit-source-id: 2b76639d2237176b6aae4fb1e22cf1a1ec70a69a
1 parent 8edf4e9 commit 77e79d6

File tree

1 file changed

+105
-10
lines changed

1 file changed

+105
-10
lines changed

Libraries/Animated/components/AnimatedScrollView.js

Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,117 @@
99
*/
1010

1111
import * as React from 'react';
12+
import {useMemo} from 'react';
1213

14+
import RefreshControl from '../../Components/RefreshControl/RefreshControl';
1315
import ScrollView from '../../Components/ScrollView/ScrollView';
16+
import StyleSheet from '../../StyleSheet/StyleSheet';
17+
import flattenStyle from '../../StyleSheet/flattenStyle';
18+
import splitLayoutProps from '../../StyleSheet/splitLayoutProps';
19+
import Platform from '../../Utilities/Platform';
20+
import useMergeRefs from '../../Utilities/useMergeRefs';
1421
import createAnimatedComponent from '../createAnimatedComponent';
22+
import useAnimatedProps from '../useAnimatedProps';
1523

1624
import type {AnimatedComponentType} from '../createAnimatedComponent';
1725

26+
type Props = React.ElementConfig<typeof ScrollView>;
27+
type Instance = React.ElementRef<typeof ScrollView>;
28+
1829
/**
1930
* @see https://github.com/facebook/react-native/commit/b8c8562
2031
*/
21-
const ScrollViewWithEventThrottle = React.forwardRef((props, ref) => (
22-
<ScrollView scrollEventThrottle={0.0001} {...props} ref={ref} />
23-
));
24-
25-
export default (createAnimatedComponent(
26-
ScrollViewWithEventThrottle,
27-
): AnimatedComponentType<
28-
React.ElementConfig<typeof ScrollView>,
29-
React.ElementRef<typeof ScrollView>,
30-
>);
32+
const AnimatedScrollView: AnimatedComponentType<Props, Instance> =
33+
React.forwardRef((props, forwardedRef) => {
34+
// (Android only) When a ScrollView has a RefreshControl and
35+
// any `style` property set with an Animated.Value, the CSS
36+
// gets incorrectly applied twice. This is because ScrollView
37+
// swaps the parent/child relationship of itself and the
38+
// RefreshControl component (see ScrollView.js for more details).
39+
if (
40+
Platform.OS === 'android' &&
41+
props.refreshControl != null &&
42+
props.style != null
43+
) {
44+
return (
45+
<AnimatedScrollViewWithInvertedRefreshControl
46+
scrollEventThrottle={0.0001}
47+
{...props}
48+
ref={forwardedRef}
49+
refreshControl={props.refreshControl}
50+
/>
51+
);
52+
} else {
53+
return (
54+
<AnimatedScrollViewWithoutInvertedRefreshControl
55+
scrollEventThrottle={0.0001}
56+
{...props}
57+
ref={forwardedRef}
58+
/>
59+
);
60+
}
61+
});
62+
63+
const AnimatedScrollViewWithInvertedRefreshControl = React.forwardRef(
64+
(
65+
props: {
66+
...React.ElementConfig<typeof ScrollView>,
67+
// $FlowFixMe[unclear-type] Same Flow type as `refreshControl` in ScrollView
68+
refreshControl: React.Element<any>,
69+
},
70+
forwardedRef,
71+
) => {
72+
// Split `props` into the animate-able props for the parent (RefreshControl)
73+
// and child (ScrollView).
74+
const {intermediatePropsForRefreshControl, intermediatePropsForScrollView} =
75+
useMemo(() => {
76+
const {outer, inner} = splitLayoutProps(flattenStyle(props.style));
77+
return {
78+
intermediatePropsForRefreshControl: {style: outer},
79+
intermediatePropsForScrollView: {...props, style: inner},
80+
};
81+
}, [props]);
82+
83+
// Handle animated props on `refreshControl`.
84+
const [refreshControlAnimatedProps, refreshControlRef] = useAnimatedProps(
85+
intermediatePropsForRefreshControl,
86+
);
87+
// NOTE: Assumes that refreshControl.ref` and `refreshControl.style` can be
88+
// safely clobbered.
89+
const refreshControl: React.Element<typeof RefreshControl> =
90+
React.cloneElement(props.refreshControl, {
91+
...refreshControlAnimatedProps,
92+
ref: refreshControlRef,
93+
});
94+
95+
// Handle animated props on `NativeDirectionalScrollView`.
96+
const [scrollViewAnimatedProps, scrollViewRef] = useAnimatedProps<
97+
Props,
98+
Instance,
99+
>(intermediatePropsForScrollView);
100+
const ref = useMergeRefs<Instance | null>(scrollViewRef, forwardedRef);
101+
102+
return (
103+
// $FlowFixMe[incompatible-use] Investigate useAnimatedProps return value
104+
<ScrollView
105+
{...scrollViewAnimatedProps}
106+
ref={ref}
107+
refreshControl={refreshControl}
108+
// Because `refreshControl` is a clone of `props.refreshControl` with
109+
// `refreshControlAnimatedProps` added, we need to pass ScrollView.js
110+
// the combined styles since it also splits the outer/inner styles for
111+
// its parent/child, respectively. Without this, the refreshControl
112+
// styles would be ignored.
113+
style={StyleSheet.compose(
114+
scrollViewAnimatedProps.style,
115+
refreshControlAnimatedProps.style,
116+
)}
117+
/>
118+
);
119+
},
120+
);
121+
122+
const AnimatedScrollViewWithoutInvertedRefreshControl =
123+
createAnimatedComponent(ScrollView);
124+
125+
export default AnimatedScrollView;

0 commit comments

Comments
 (0)