Skip to content

Commit 55aa8d2

Browse files
committed
Remove use of Animated.delay and sequence for CSS transition polyfill
We can use the `delay` field in the config for `timing` and `spring` rather than using `Animated.delay` and `Animated.sequence`. This should help with animation sequencing and avoid depending on `onAnimationComplete` events firing.
1 parent f52acd2 commit 55aa8d2

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

apps/examples/src/components/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ const styles = css.create({
731731
},
732732
transitionOpacity: {
733733
backgroundColor: 'red',
734+
transitionDelay: '250ms',
734735
transitionDuration: '0.5s',
735736
transitionProperty: 'opacity',
736737
transitionTimingFunction: 'ease'

packages/react-strict-dom/src/native/modules/useStyleTransition.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ function transitionStyleHasChanged(
195195

196196
function getAnimation(
197197
animatedValue: ReactNative.Animated.Value,
198+
delay: number,
198199
duration: number,
199200
timingFunction: string | null,
200201
shouldUseNativeDriver: boolean
@@ -210,6 +211,7 @@ function getAnimation(
210211
`spring() timing function of "${timingFunction}" is missing closing parenthesis.`
211212
);
212213
return ReactNative.Animated.timing(animatedValue, {
214+
delay,
213215
duration,
214216
easing: getEasingFunction(null),
215217
toValue: 1,
@@ -244,6 +246,7 @@ function getAnimation(
244246
}
245247

246248
return ReactNative.Animated.spring(animatedValue, {
249+
delay,
247250
damping,
248251
mass,
249252
stiffness,
@@ -254,6 +257,7 @@ function getAnimation(
254257
}
255258

256259
return ReactNative.Animated.timing(animatedValue, {
260+
delay,
257261
duration,
258262
easing: getEasingFunction(timingFunction),
259263
toValue: 1,
@@ -324,15 +328,13 @@ export function useStyleTransition(style: ReactNativeStyle): ReactNativeStyle {
324328
const { delay, duration, timingFunction, shouldUseNativeDriver } =
325329
transitionMetadataRef.current;
326330

327-
const animation = ReactNative.Animated.sequence([
328-
ReactNative.Animated.delay(delay),
329-
getAnimation(
330-
animatedValue,
331-
duration,
332-
timingFunction,
333-
shouldUseNativeDriver
334-
)
335-
]);
331+
const animation = getAnimation(
332+
animatedValue,
333+
delay,
334+
duration,
335+
timingFunction,
336+
shouldUseNativeDriver
337+
);
336338
animation.start();
337339

338340
return () => {

packages/react-strict-dom/tests/__mocks__/react-native/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export const Animated = {
1919
}),
2020
timing: jest.fn(() => {
2121
return {
22-
start: jest.fn()
22+
start: jest.fn((callback) => {
23+
callback && callback();
24+
}),
25+
stop: jest.fn()
2326
};
2427
}),
2528
delay: jest.fn(),
@@ -33,7 +36,10 @@ export const Animated = {
3336
}),
3437
spring: jest.fn(() => {
3538
return {
36-
start: jest.fn()
39+
start: jest.fn((callback) => {
40+
callback && callback();
41+
}),
42+
stop: jest.fn()
3743
};
3844
})
3945
};

packages/react-strict-dom/tests/html-test.native.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ describe('<html.*>', () => {
622622
expect(console.warn).not.toHaveBeenCalledWith(
623623
expect.stringContaining('React Strict DOM')
624624
);
625-
expect(Animated.sequence).not.toHaveBeenCalled();
625+
expect(Animated.timing).not.toHaveBeenCalled();
626626
expect(root.toJSON()).toMatchSnapshot('default');
627627
});
628628

@@ -638,14 +638,14 @@ describe('<html.*>', () => {
638638
expect(console.error).not.toHaveBeenCalledWith(
639639
expect.stringContaining('React Strict DOM')
640640
);
641-
expect(Animated.sequence).toHaveBeenCalled();
641+
expect(Animated.timing).toHaveBeenCalled();
642642
expect(root.toJSON()).toMatchSnapshot('red to green');
643-
Animated.sequence.mockClear();
643+
Animated.timing.mockClear();
644644

645645
act(() => {
646646
root.update(<html.div style={styles.backgroundColor('blue')} />);
647647
});
648-
expect(Animated.sequence).toHaveBeenCalled();
648+
expect(Animated.timing).toHaveBeenCalled();
649649
expect(root.toJSON()).toMatchSnapshot('green to blue');
650650
});
651651

@@ -664,6 +664,14 @@ describe('<html.*>', () => {
664664
});
665665
expect(root.toJSON()).toMatchSnapshot('end');
666666
expect(Easing.inOut).toHaveBeenCalled();
667+
expect(Animated.timing).toHaveBeenCalledWith(
668+
expect.anything(),
669+
expect.objectContaining({
670+
delay: 200,
671+
duration: 2000,
672+
useNativeDriver: false
673+
})
674+
);
667675
});
668676

669677
test('opacity transition', () => {
@@ -679,6 +687,14 @@ describe('<html.*>', () => {
679687
});
680688
expect(root.toJSON()).toMatchSnapshot('end');
681689
expect(Easing.in).toHaveBeenCalled();
690+
expect(Animated.timing).toHaveBeenCalledWith(
691+
expect.anything(),
692+
expect.objectContaining({
693+
delay: 50,
694+
duration: 1000,
695+
useNativeDriver: true
696+
})
697+
);
682698
});
683699

684700
test('transform transition', () => {
@@ -698,6 +714,14 @@ describe('<html.*>', () => {
698714
});
699715
expect(root.toJSON()).toMatchSnapshot('end');
700716
expect(Easing.out).toHaveBeenCalled();
717+
expect(Animated.timing).toHaveBeenCalledWith(
718+
expect.anything(),
719+
expect.objectContaining({
720+
delay: 0,
721+
duration: 1000,
722+
useNativeDriver: true
723+
})
724+
);
701725
});
702726

703727
test('width transition', () => {
@@ -713,6 +737,14 @@ describe('<html.*>', () => {
713737
});
714738
expect(root.toJSON()).toMatchSnapshot('end');
715739
expect(Easing.out).toHaveBeenCalled();
740+
expect(Animated.timing).toHaveBeenCalledWith(
741+
expect.anything(),
742+
expect.objectContaining({
743+
delay: 0,
744+
duration: 500,
745+
useNativeDriver: false
746+
})
747+
);
716748
});
717749

718750
test('cubic-bezier() timing function', () => {

0 commit comments

Comments
 (0)