Skip to content
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

Fix warning about overwriting style with layout animation #5644

Merged
merged 7 commits into from
Feb 20, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions src/animationBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
LayoutAnimationsValues,
} from './reanimated2/layoutReanimation';
import type { StyleProps } from './reanimated2/commonTypes';
import type { NestedArray } from './createAnimatedComponent/commonTypes';

const mockTargetValues: LayoutAnimationsValues = {
targetOriginX: 0,
Expand All @@ -25,12 +26,50 @@ const mockTargetValues: LayoutAnimationsValues = {
currentBorderRadius: 0,
};

function getCommonProperties(
layoutStyle: StyleProps,
secondStyle: StyleProps | Array<StyleProps>
tjzel marked this conversation as resolved.
Show resolved Hide resolved
) {
const secondStyleFlat = Array.isArray(secondStyle)
Latropos marked this conversation as resolved.
Show resolved Hide resolved
? secondStyle.flat()
: [secondStyle];

let commonKeys: Array<string> = [];
secondStyleFlat.forEach((s) => {
Latropos marked this conversation as resolved.
Show resolved Hide resolved
if ('initial' in s) {
s = s.initial.value; // Include properties of animated style
}

const commonStyleKeys = Object.keys(s).filter((key) => key in layoutStyle);
commonKeys = commonKeys.concat(commonStyleKeys);
});
return commonKeys;
Latropos marked this conversation as resolved.
Show resolved Hide resolved
}

function maybeReportOverwrittenProperties(
layoutAnimationStyle: StyleProps,
style: NestedArray<StyleProps>,
displayName: string
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
) {
const commonProperties = getCommonProperties(layoutAnimationStyle, style);

if (commonProperties.length > 0) {
console.warn(
`[Reanimated] ${
commonProperties.length === 1 ? 'Property' : 'Properties'
} "${commonProperties.join(
', '
)}" of ${displayName} may be overwritten by a layout animation. Please wrap your component with an animated view and apply the layout animation on the wrapper.`
);
}
}

export function maybeBuild(
layoutAnimationOrBuilder:
| ILayoutAnimationBuilder
| LayoutAnimationFunction
| Keyframe,
style: StyleProps | undefined,
style: NestedArray<StyleProps> | undefined,
displayName: string
): LayoutAnimationFunction | Keyframe {
const isAnimationBuilder = (
Expand All @@ -42,21 +81,12 @@ export function maybeBuild(
if (isAnimationBuilder(layoutAnimationOrBuilder)) {
const animationFactory = layoutAnimationOrBuilder.build();
const layoutAnimation = animationFactory(mockTargetValues);
const animatedStyle = layoutAnimation.animations;

const getCommonProperties = (obj1: object, obj2: object) =>
Object.keys(obj1).filter((key) =>
Object.prototype.hasOwnProperty.call(obj2, key)
);

const commonProperties = getCommonProperties(animatedStyle, style || {});
if (commonProperties.length > 0) {
console.warn(
`[Reanimated] ${
commonProperties.length === 1 ? 'Property' : 'Properties: '
} "${commonProperties.join(
', '
)}" of ${displayName} may be overwritten with layout animation. Please create a wrapper with the layout animation you want to apply.`
if (__DEV__ && style) {
maybeReportOverwrittenProperties(
layoutAnimation.animations,
style,
displayName
);
}

Expand Down