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 1 commit
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
63 changes: 48 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,52 @@ const mockTargetValues: LayoutAnimationsValues = {
currentBorderRadius: 0,
};

function maybeReportOverwrittenProperties(
layoutAnimationStyle: StyleProps,
style: NestedArray<StyleProps>,
displayName: string
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
) {
function getCommonProperties(
Latropos marked this conversation as resolved.
Show resolved Hide resolved
layoutStyle: object,
secondStyle: object | Array<object>
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
) {
const secondStyleFlat = Array.isArray(secondStyle)
? secondStyle.flat()
: [secondStyle];
const commonKeys: Array<string> = [];

secondStyleFlat.forEach((s) => {
if ('initial' in s) {
s = s.initial.value;
}
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved

const commonStyleKeys = Object.keys(s).filter((key) =>
Object.prototype.hasOwnProperty.call(layoutStyle, key)
Latropos marked this conversation as resolved.
Show resolved Hide resolved
);
commonKeys.push(...commonStyleKeys);
Latropos marked this conversation as resolved.
Show resolved Hide resolved
});
return commonKeys;
}

const commonProperties = getCommonProperties(layoutAnimationStyle, style);

if (commonProperties.length > 0) {
console.warn(
`[Reanimated] ${
commonProperties.length === 1 ? 'Property' : 'Properties: '
Latropos marked this conversation as resolved.
Show resolved Hide resolved
} "${commonProperties.join(
', '
)}" of ${displayName} may be overwritten with layout animation. Please create a wrapper with the layout animation you want to apply.`
Latropos marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

export function maybeBuild(
layoutAnimationOrBuilder:
| ILayoutAnimationBuilder
| LayoutAnimationFunction
| Keyframe,
style: StyleProps | undefined,
style: NestedArray<StyleProps> | undefined,
displayName: string
): LayoutAnimationFunction | Keyframe {
const isAnimationBuilder = (
Expand All @@ -42,21 +83,13 @@ 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 layoutAnimationStyle = layoutAnimation.animations;

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__) {
maybeReportOverwrittenProperties(
layoutAnimationStyle,
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
style || {},
displayName
Latropos marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
Loading