Skip to content

Commit

Permalink
RN: Disable collapsable for Animated Components
Browse files Browse the repository at this point in the history
Summary:
When we were iterating on the Fabric renderer, animated components went through some iteration to ensure that animated shadow nodes were not flattened away. At the time, `collapsable` was not supported on iOS, even in Fabric, because the legacy renderer would not publish the `collapsable` prop on the view config.

This has since been fixed and `collapsable` is supported on both Android and iOS. We no longer need the `nativeID` workaround to prevent view flattening.

For use cases of the JavaScript driver and legacy renderers, this change will cause views which used to be flattened to no longer be flattened. This seems like an appropriate change considering the direction that we are moving (in which everything is eventually transitioned to using the Fabric renderer).

Changelog:
[Android][Changed] - Native views backing Animated.View (w/ JavaScript-driven animations) will no longer be flattened; this should be a transparent change.

Reviewed By: lunaleaps, mdvacca

Differential Revision: D31223031

fbshipit-source-id: 48dc63471eef406f4c215bfea0b3ef82a05d4b24
  • Loading branch information
yungsters authored and facebook-github-bot committed Sep 30, 2021
1 parent c9c14ef commit 4fdbc44
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 56 deletions.
6 changes: 3 additions & 3 deletions Libraries/Animated/components/AnimatedImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const createAnimatedComponent = require('../createAnimatedComponent');

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

module.exports = (createAnimatedComponent((Image: $FlowFixMe), {
collapsable: false,
}): AnimatedComponentType<
module.exports = (createAnimatedComponent(
(Image: $FlowFixMe),
): AnimatedComponentType<
React.ElementConfig<typeof Image>,
React.ElementRef<typeof Image>,
>);
6 changes: 3 additions & 3 deletions Libraries/Animated/components/AnimatedScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const ScrollViewWithEventThrottle = React.forwardRef((props, ref) => (
<ScrollView scrollEventThrottle={0.0001} {...props} ref={ref} />
));

module.exports = (createAnimatedComponent(ScrollViewWithEventThrottle, {
collapsable: false,
}): AnimatedComponentType<
module.exports = (createAnimatedComponent(
ScrollViewWithEventThrottle,
): AnimatedComponentType<
React.ElementConfig<typeof ScrollView>,
React.ElementRef<typeof ScrollView>,
>);
6 changes: 3 additions & 3 deletions Libraries/Animated/components/AnimatedText.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const createAnimatedComponent = require('../createAnimatedComponent');

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

module.exports = (createAnimatedComponent((Text: $FlowFixMe), {
collapsable: false,
}): AnimatedComponentType<
module.exports = (createAnimatedComponent(
(Text: $FlowFixMe),
): AnimatedComponentType<
React.ElementConfig<typeof Text>,
React.ElementRef<typeof Text>,
>);
4 changes: 1 addition & 3 deletions Libraries/Animated/components/AnimatedView.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ const createAnimatedComponent = require('../createAnimatedComponent');

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

module.exports = (createAnimatedComponent(View, {
collapsable: true,
}): AnimatedComponentType<
module.exports = (createAnimatedComponent(View): AnimatedComponentType<
React.ElementConfig<typeof View>,
React.ElementRef<typeof View>,
>);
37 changes: 3 additions & 34 deletions Libraries/Animated/createAnimatedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ export type AnimatedComponentType<
Instance,
>;

type AnimatedComponentOptions = {
collapsable?: boolean,
};

function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
Component: React.AbstractComponent<Props, Instance>,
options?: AnimatedComponentOptions,
): AnimatedComponentType<Props, Instance> {
invariant(
typeof Component !== 'function' ||
Expand Down Expand Up @@ -210,39 +205,13 @@ function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
this.props.passthroughAnimatedPropExplicitValues || {};
const mergedStyle = {...style, ...passthruStyle};

// On Fabric, we always want to ensure the container Animated View is *not*
// flattened.
// Because we do not get a host component ref immediately and thus cannot
// do a proper Fabric vs non-Fabric detection immediately, we default to assuming
// that Fabric *is* enabled until we know otherwise.
// Thus, in Fabric, this view will never be flattened. In non-Fabric, the view will
// not be flattened during the initial render but may be flattened in the second render
// and onwards.
const forceNativeIdFabric =
(this._component == null &&
(options?.collapsable === false || props.collapsable !== true)) ||
this._isFabric();

const forceNativeId =
props.collapsable ??
(this._propsAnimated.__isNative ||
forceNativeIdFabric ||
options?.collapsable === false);
// The native driver updates views directly through the UI thread so we
// have to make sure the view doesn't get optimized away because it cannot
// go through the NativeViewHierarchyManager since it operates on the shadow
// thread. TODO: T68258846
const collapsableProps = forceNativeId
? {
nativeID: props.nativeID ?? 'animatedComponent',
collapsable: false,
}
: {};
// Force `collapsable` to be false so that native view is not flattened.
// Flattened views cannot be accurately referenced by a native driver.
return (
<Component
{...props}
{...passthruProps}
{...collapsableProps}
collapsable={false}
style={mergedStyle}
ref={this._setComponentRef}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ exports[`TouchableOpacity renders correctly 1`] = `
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down Expand Up @@ -35,7 +34,6 @@ exports[`TouchableOpacity renders in disabled state when a disabled prop is pass
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down Expand Up @@ -65,7 +63,6 @@ exports[`TouchableOpacity renders in disabled state when a key disabled in acces
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ exports[`<Button /> should be disabled and it should set accessibilityState to d
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down Expand Up @@ -66,7 +65,6 @@ exports[`<Button /> should be disabled when disabled is empty and accessibilityS
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down Expand Up @@ -121,7 +119,6 @@ exports[`<Button /> should be disabled when disabled={true} and accessibilitySta
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down Expand Up @@ -176,7 +173,6 @@ exports[`<Button /> should not be disabled when disabled={false} and accessibili
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down Expand Up @@ -227,7 +223,6 @@ exports[`<Button /> should not be disabled when disabled={false} and accessibili
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down Expand Up @@ -278,7 +273,6 @@ exports[`<Button /> should overwrite accessibilityState with value of disabled p
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down Expand Up @@ -328,7 +322,6 @@ exports[`<Button /> should render as expected 1`] = `
accessible={true}
collapsable={false}
focusable={false}
nativeID="animatedComponent"
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
Expand Down

0 comments on commit 4fdbc44

Please sign in to comment.