|
9 | 9 | */
|
10 | 10 |
|
11 | 11 | import * as React from 'react';
|
| 12 | +import {useMemo} from 'react'; |
12 | 13 |
|
| 14 | +import RefreshControl from '../../Components/RefreshControl/RefreshControl'; |
13 | 15 | 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'; |
14 | 21 | import createAnimatedComponent from '../createAnimatedComponent';
|
| 22 | +import useAnimatedProps from '../useAnimatedProps'; |
15 | 23 |
|
16 | 24 | import type {AnimatedComponentType} from '../createAnimatedComponent';
|
17 | 25 |
|
| 26 | +type Props = React.ElementConfig<typeof ScrollView>; |
| 27 | +type Instance = React.ElementRef<typeof ScrollView>; |
| 28 | + |
18 | 29 | /**
|
19 | 30 | * @see https://github.com/facebook/react-native/commit/b8c8562
|
20 | 31 | */
|
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