Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default class CSSManager implements ICSSManager {
transitionProperties,
pseudoStylesBySelector,
,
,
filteredStyle,
] = filterCSSAndStyleProperties(style);

Expand Down
31 changes: 31 additions & 0 deletions packages/react-native-reanimated/src/css/types/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,37 @@ export type CSSAnimationDirection =
export type CSSAnimationFillMode = 'none' | 'forwards' | 'backwards' | 'both';
export type CSSAnimationPlayState = 'running' | 'paused';

/** Payload for a CSS animation callback. */
export type CSSAnimationEvent = {
// TODO: add a JS-side view ref (e.g. `target`) once the right ref type is
// decided.
/**
* The name of the keyframes that fired the event (matches the `name` of a
* `css.keyframes(...)` rule).
*/
animationName: string;
/**
* The amount of time the animation had been running, in seconds, when the
* event fired.
*/
elapsedTime: number;
};

export type CSSAnimationCallback = (event: CSSAnimationEvent) => void;

export type CSSAnimationCallbacks = {
/** Fired when the animation starts, after any `animationDelay`. */
onAnimationStart?: CSSAnimationCallback;
/** Fired when the animation completes. */
onAnimationEnd?: CSSAnimationCallback;
/** Fired at the end of each iteration except the last. */
onAnimationIteration?: CSSAnimationCallback;
/** Fired when the animation is interrupted before completing. */
onAnimationCancel?: CSSAnimationCallback;
};

export type CSSAnimationCallbackProp = keyof CSSAnimationCallbacks;

export type SingleCSSAnimationSettings = {
animationDuration?: CSSAnimationDuration;
animationTimingFunction?: CSSAnimationTimingFunction;
Expand Down
5 changes: 3 additions & 2 deletions packages/react-native-reanimated/src/css/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
import type { CSSAnimationProp } from './animation';
import type { CSSAnimationCallbackProp, CSSAnimationProp } from './animation';
import type {
CSSTransitionCallbackProp,
CSSTransitionProp,
Expand All @@ -17,4 +17,5 @@ export type * from './transition';
export type CSSConfigProp =
| CSSTransitionProp
| CSSAnimationProp
| CSSTransitionCallbackProp;
| CSSTransitionCallbackProp
| CSSAnimationCallbackProp;
6 changes: 5 additions & 1 deletion packages/react-native-reanimated/src/css/types/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import type { StyleProp } from 'react-native';

import type { PlainStyle } from '../../common';
import type { CSSAnimationProperties } from './animation';
import type {
CSSAnimationCallbacks,
CSSAnimationProperties,
} from './animation';
import type { PseudoValue } from './pseudo';
import type {
CSSTransitionCallbacks,
Expand All @@ -25,6 +28,7 @@ type PickStyleProps<P> = Pick<
export type CSSStyle<S extends object = PlainStyle> = {
[K in keyof S]: S[K] | PseudoValue<S[K]>;
} & Partial<CSSAnimationProperties<S>> &
Partial<CSSAnimationCallbacks> &
Partial<CSSTransitionProperties<S>> &
Partial<CSSTransitionCallbacks>;

Expand Down
144 changes: 134 additions & 10 deletions packages/react-native-reanimated/src/css/utils/__tests__/props.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe(filterCSSAndStyleProperties, () => {
expect.any(Object),
null,
null,
null,
expect.any(Object),
]);
});
Expand All @@ -34,6 +35,7 @@ describe(filterCSSAndStyleProperties, () => {
expect.any(Object),
null,
null,
null,
expect.any(Object),
]);
});
Expand All @@ -48,6 +50,7 @@ describe(filterCSSAndStyleProperties, () => {
expect.any(Object),
null,
null,
null,
expect.any(Object),
]);
});
Expand All @@ -65,6 +68,7 @@ describe(filterCSSAndStyleProperties, () => {
expect.any(Object),
null,
null,
null,
expect.any(Object),
]);
});
Expand All @@ -91,6 +95,7 @@ describe(filterCSSAndStyleProperties, () => {
null,
null,
null,
null,
{},
]);
});
Expand All @@ -105,6 +110,7 @@ describe(filterCSSAndStyleProperties, () => {
null,
null,
null,
null,
expect.any(Object),
]);
});
Expand All @@ -122,13 +128,15 @@ describe(filterCSSAndStyleProperties, () => {
style1,
null,
null,
null,
expect.any(Object),
]);
expect(filterCSSAndStyleProperties(style2)).toEqual([
expect.any(Object),
style2,
null,
null,
null,
expect.any(Object),
]);
});
Expand All @@ -146,6 +154,7 @@ describe(filterCSSAndStyleProperties, () => {
{ transition: 'opacity 2s ease-in' },
null,
null,
null,
expect.any(Object),
]);
});
Expand All @@ -166,6 +175,7 @@ describe(filterCSSAndStyleProperties, () => {
expect.objectContaining({ [key]: value }),
null,
null,
null,
{},
]);
});
Expand Down Expand Up @@ -197,6 +207,7 @@ describe(filterCSSAndStyleProperties, () => {
},
},
null,
null,
{ opacity: 1, backgroundColor: 'blue' },
]);
});
Expand All @@ -207,7 +218,7 @@ describe(filterCSSAndStyleProperties, () => {
width: 100,
};

const [, , , , filteredStyle] = filterCSSAndStyleProperties(style);
const [, , , , , filteredStyle] = filterCSSAndStyleProperties(style);

expect(filteredStyle).toEqual({ opacity: 0.8, width: 100 });
});
Expand All @@ -218,7 +229,7 @@ describe(filterCSSAndStyleProperties, () => {
width: 100,
};

const [, , pseudoStylesBySelector, , filteredStyle] =
const [, , pseudoStylesBySelector, , , filteredStyle] =
filterCSSAndStyleProperties(style);

expect(filteredStyle).toEqual({ width: 100 });
Expand All @@ -236,7 +247,7 @@ describe(filterCSSAndStyleProperties, () => {
width: 100,
};

const [, , pseudoStylesBySelector, , filteredStyle] =
const [, , pseudoStylesBySelector, , , filteredStyle] =
filterCSSAndStyleProperties(style);

expect(filteredStyle).toEqual({ opacity: 0.8, width: 100 });
Expand All @@ -260,6 +271,7 @@ describe(filterCSSAndStyleProperties, () => {
},
},
null,
null,
{ opacity: 1, borderRadius: 8 },
]);
});
Expand All @@ -274,7 +286,7 @@ describe(filterCSSAndStyleProperties, () => {
borderWidth: { default: 0, ':focus': 2 },
};

const [, , pseudoStylesBySelector, , filteredStyle] =
const [, , pseudoStylesBySelector, , , filteredStyle] =
filterCSSAndStyleProperties(style);

expect(filteredStyle).toEqual({
Expand Down Expand Up @@ -306,7 +318,7 @@ describe(filterCSSAndStyleProperties, () => {
height: 100,
};

const [, , pseudoStylesBySelector, , filteredStyle] =
const [, , pseudoStylesBySelector, , , filteredStyle] =
filterCSSAndStyleProperties(style);

expect(filteredStyle).toEqual({
Expand Down Expand Up @@ -339,7 +351,7 @@ describe(filterCSSAndStyleProperties, () => {
},
};

const [, , pseudoStylesBySelector, , filteredStyle] =
const [, , pseudoStylesBySelector, , , filteredStyle] =
filterCSSAndStyleProperties(style);

expect(filteredStyle).toEqual({ opacity: 1 });
Expand All @@ -365,7 +377,7 @@ describe(filterCSSAndStyleProperties, () => {
backgroundColor: { default: 'white', ':active': 'red' },
};

const [, , pseudoStylesBySelector, , filteredStyle] =
const [, , pseudoStylesBySelector, , , filteredStyle] =
filterCSSAndStyleProperties(style);

expect(filteredStyle).toEqual({ opacity: 1, backgroundColor: 'white' });
Expand All @@ -386,7 +398,7 @@ describe(filterCSSAndStyleProperties, () => {
opacity: { ':active': 0.5, ':hover': 0.8 } as never,
};

const [, , pseudoStylesBySelector, , filteredStyle] =
const [, , pseudoStylesBySelector, , , filteredStyle] =
filterCSSAndStyleProperties(style);

expect(filteredStyle).toEqual({});
Expand All @@ -413,7 +425,7 @@ describe(filterCSSAndStyleProperties, () => {
} as never,
};

const [, , pseudoStylesBySelector, , filteredStyle] =
const [, , pseudoStylesBySelector, , , filteredStyle] =
filterCSSAndStyleProperties(style);

expect(filteredStyle).toEqual({ backgroundColor: 'white' });
Expand Down Expand Up @@ -476,6 +488,7 @@ describe(filterCSSAndStyleProperties, () => {
}),
null,
null,
null,
{
width: 100,
height: 100,
Expand All @@ -495,6 +508,7 @@ describe(filterCSSAndStyleProperties, () => {
expect.any(Object),
null,
null,
null,
expect.any(Object),
]);
});
Expand All @@ -510,7 +524,7 @@ describe(filterCSSAndStyleProperties, () => {
onTransitionEnd,
};

const [, transitionConfig, , transitionCallbacks, filteredStyle] =
const [, transitionConfig, , , transitionCallbacks, filteredStyle] =
filterCSSAndStyleProperties(style);

expect(transitionCallbacks).toEqual({ onTransitionRun, onTransitionEnd });
Expand Down Expand Up @@ -569,4 +583,114 @@ describe(filterCSSAndStyleProperties, () => {
});
});
});

describe('animation callbacks', () => {
test('returns null when no callback props are present', () => {
const style: CSSStyle = {
animationName: css.keyframes({
from: { opacity: 0 },
to: { opacity: 1 },
}),
animationDuration: 100,
};
expect(filterCSSAndStyleProperties(style)).toEqual([
expect.any(Object),
null,
null,
null,
null,
expect.any(Object),
]);
});

test('extracts callback props and keeps them out of the style object', () => {
const onAnimationStart = jest.fn();
const onAnimationEnd = jest.fn();
const style: CSSStyle = {
width: 100,
animationName: css.keyframes({
from: { opacity: 0 },
to: { opacity: 1 },
}),
animationDuration: 100,
onAnimationStart,
onAnimationEnd,
};

const [animationConfig, , , animationCallbacks, , filteredStyle] =
filterCSSAndStyleProperties(style);

expect(animationCallbacks).toEqual({ onAnimationStart, onAnimationEnd });
// Callbacks must not leak into the plain style nor the animation config.
expect(filteredStyle).toEqual({ width: 100 });
expect(animationConfig).not.toHaveProperty('onAnimationStart');
expect(animationConfig).not.toHaveProperty('onAnimationEnd');
});
});

describe('animation callbacks validation', () => {
const globalWithDev = globalThis as unknown as { __DEV__: boolean };

beforeEach(() => {
(console.warn as jest.Mock).mockClear();
});

describe('in development (__DEV__)', () => {
beforeEach(() => {
globalWithDev.__DEV__ = true;
});

test('warns when animation callbacks are used without any animation props', () => {
filterCSSAndStyleProperties({ onAnimationEnd: jest.fn() } as CSSStyle);
expect(console.warn).toHaveBeenCalledWith(
expect.stringContaining('onAnimationEnd')
);
});

test('does not warn when an animation is configured alongside callbacks', () => {
filterCSSAndStyleProperties({
animationName: css.keyframes({
from: { opacity: 0 },
to: { opacity: 1 },
}),
animationDuration: 100,
onAnimationEnd: jest.fn(),
} as CSSStyle);
expect(console.warn).not.toHaveBeenCalled();
});

test('does not warn when animationName is a plain keyframes object', () => {
filterCSSAndStyleProperties({
animationName: { from: { opacity: 0 }, to: { opacity: 1 } },
animationDuration: 100,
onAnimationEnd: jest.fn(),
} as CSSStyle);
expect(console.warn).not.toHaveBeenCalled();
});

test('warns when callbacks are used but animationName has no valid keyframes', () => {
// An empty keyframes object means no animation actually runs, so the
// callbacks can never fire and the warning must still be emitted.
filterCSSAndStyleProperties({
animationName: {},
animationDuration: 100,
onAnimationEnd: jest.fn(),
} as CSSStyle);
expect(console.warn).toHaveBeenCalledWith(
expect.stringContaining('onAnimationEnd')
);
});
});

describe('in production (!__DEV__)', () => {
beforeEach(() => {
globalWithDev.__DEV__ = false;
});

test('skips validation entirely - never warns, even without animation props', () => {
filterCSSAndStyleProperties({ onAnimationEnd: jest.fn() } as CSSStyle);
expect(console.warn).not.toHaveBeenCalled();
});
});
});
});
Loading
Loading