Skip to content

Commit cec2483

Browse files
committed
feat: use Animated API if reanimated2 not available
1 parent 5c35ad5 commit cec2483

File tree

20 files changed

+150
-73
lines changed

20 files changed

+150
-73
lines changed

example/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"expo-status-bar": "~1.4.0",
1515
"expo-updates": "~0.14.6",
1616
"react": "18.0.0",
17-
"react-native": "0.69.6",
18-
"react-native-reanimated": "~2.9.1"
17+
"react-native": "0.69.6"
1918
},
2019
"devDependencies": {
2120
"@babel/core": "^7.12.9",

example/yarn.lock

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@
344344
"@babel/helper-plugin-utils" "^7.18.9"
345345
"@babel/plugin-syntax-export-default-from" "^7.18.6"
346346

347-
"@babel/plugin-proposal-export-namespace-from@^7.17.12", "@babel/plugin-proposal-export-namespace-from@^7.18.9":
347+
"@babel/plugin-proposal-export-namespace-from@^7.18.9":
348348
version "7.18.9"
349349
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-export-namespace-from/-/plugin-proposal-export-namespace-from-7.18.9.tgz#5f7313ab348cdb19d590145f9247540e94761203"
350350
integrity sha512-k1NtHyOMvlDDFeb9G5PhUXuGj8m/wiwojgQVEhJ/fsVsMCpLyOP4h0uGEjYJKrRI+EVPlb5Jk+Gt9P97lOGwtA==
@@ -748,13 +748,6 @@
748748
dependencies:
749749
"@babel/helper-plugin-utils" "^7.18.6"
750750

751-
"@babel/plugin-transform-object-assign@^7.16.7":
752-
version "7.18.6"
753-
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-object-assign/-/plugin-transform-object-assign-7.18.6.tgz#7830b4b6f83e1374a5afb9f6111bcfaea872cdd2"
754-
integrity sha512-mQisZ3JfqWh2gVXvfqYCAAyRs6+7oev+myBsTwW5RnPhYXOTuCEw2oe3YgxlXMViXUS53lG8koulI7mJ+8JE+A==
755-
dependencies:
756-
"@babel/helper-plugin-utils" "^7.18.6"
757-
758751
"@babel/plugin-transform-object-super@^7.0.0", "@babel/plugin-transform-object-super@^7.18.6":
759752
version "7.18.6"
760753
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-object-super/-/plugin-transform-object-super-7.18.6.tgz#fb3c6ccdd15939b6ff7939944b51971ddc35912c"
@@ -997,7 +990,7 @@
997990
"@babel/types" "^7.4.4"
998991
esutils "^2.0.2"
999992

1000-
"@babel/preset-typescript@^7.13.0", "@babel/preset-typescript@^7.16.7":
993+
"@babel/preset-typescript@^7.13.0":
1001994
version "7.18.6"
1002995
resolved "https://registry.yarnpkg.com/@babel/preset-typescript/-/preset-typescript-7.18.6.tgz#ce64be3e63eddc44240c6358daefac17b3186399"
1003996
integrity sha512-s9ik86kXBAnD760aybBucdpnLsAt0jK1xqJn2juOn9lkOvSHV60os5hxoVJsPzMQxvnUJFAlkont2DvvaYEBtQ==
@@ -1716,11 +1709,6 @@
17161709
dependencies:
17171710
"@types/node" "*"
17181711

1719-
"@types/invariant@^2.2.35":
1720-
version "2.2.35"
1721-
resolved "https://registry.yarnpkg.com/@types/invariant/-/invariant-2.2.35.tgz#cd3ebf581a6557452735688d8daba6cf0bd5a3be"
1722-
integrity sha512-DxX1V9P8zdJPYQat1gHyY0xj3efl8gnMVjiM9iCY6y27lj+PoQWkgjt8jDqmovPqULkKVpKRg8J36iQiA+EtEg==
1723-
17241712
"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0":
17251713
version "2.0.4"
17261714
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz#8467d4b3c087805d63580480890791277ce35c44"
@@ -4139,11 +4127,6 @@ lodash.debounce@^4.0.8:
41394127
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
41404128
integrity sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==
41414129

4142-
lodash.isequal@^4.5.0:
4143-
version "4.5.0"
4144-
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
4145-
integrity sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==
4146-
41474130
lodash.throttle@^4.1.1:
41484131
version "4.1.1"
41494132
resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4"
@@ -5253,20 +5236,6 @@ react-native-gradle-plugin@^0.0.7:
52535236
resolved "https://registry.yarnpkg.com/react-native-gradle-plugin/-/react-native-gradle-plugin-0.0.7.tgz#96602f909745239deab7b589443f14fce5da2056"
52545237
integrity sha512-+4JpbIx42zGTONhBTIXSyfyHICHC29VTvhkkoUOJAh/XHPEixpuBduYgf6Y4y9wsN1ARlQhBBoptTvXvAFQf5g==
52555238

5256-
react-native-reanimated@~2.9.1:
5257-
version "2.9.1"
5258-
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-2.9.1.tgz#d9a932e312c13c05b4f919e43ebbf76d996e0bc1"
5259-
integrity sha512-309SIhDBwY4F1n6e5Mr5D1uPZm2ESIcmZsGXHUu8hpKX4oIOlZj2MilTk+kHhi05LjChoJkcpfkstotCJmPRPg==
5260-
dependencies:
5261-
"@babel/plugin-proposal-export-namespace-from" "^7.17.12"
5262-
"@babel/plugin-transform-object-assign" "^7.16.7"
5263-
"@babel/preset-typescript" "^7.16.7"
5264-
"@types/invariant" "^2.2.35"
5265-
invariant "^2.2.4"
5266-
lodash.isequal "^4.5.0"
5267-
setimmediate "^1.0.5"
5268-
string-hash-64 "^1.0.3"
5269-
52705239
52715240
version "0.69.6"
52725241
resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.69.6.tgz#cdd1a5757d902b91b165c28fdda4e518ed6f683a"
@@ -5901,11 +5870,6 @@ [email protected]:
59015870
resolved "https://registry.yarnpkg.com/stream-buffers/-/stream-buffers-2.2.0.tgz#91d5f5130d1cef96dcfa7f726945188741d09ee4"
59025871
integrity sha512-uyQK/mx5QjHun80FLJTfaWE7JtwfRMKBLkMne6udYOmvH0CawotVa7TfgYHzAnpphn4+TweIx1QKMnRIbipmUg==
59035872

5904-
string-hash-64@^1.0.3:
5905-
version "1.0.3"
5906-
resolved "https://registry.yarnpkg.com/string-hash-64/-/string-hash-64-1.0.3.tgz#0deb56df58678640db5c479ccbbb597aaa0de322"
5907-
integrity sha512-D5OKWKvDhyVWWn2x5Y9b+37NUllks34q1dCDhk/vYcso9fmhs+Tl3KR/gE4v5UNj2UA35cnX4KdVVGkG1deKqw==
5908-
59095873
string-width@^4.1.0, string-width@^4.2.0:
59105874
version "4.2.3"
59115875
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
"react": "18.0.0",
7979
"react-native": "0.69.6",
8080
"react-native-builder-bob": "^0.20.1",
81-
"react-native-reanimated": "^2.12.0",
8281
"release-it": "^15.0.0",
8382
"typescript": "^4.5.2"
8483
},
@@ -88,8 +87,7 @@
8887
"peerDependencies": {
8988
"@gorhom/portal": "^1",
9089
"react": "*",
91-
"react-native": "*",
92-
"react-native-reanimated": "^2"
90+
"react-native": "*"
9391
},
9492
"jest": {
9593
"preset": "react-native",
@@ -167,5 +165,8 @@
167165
},
168166
"dependencies": {
169167
"framer-motion": "^7.6.4"
168+
},
169+
"optionalDependencies": {
170+
"react-native-reanimated": "^2.12.0"
170171
}
171172
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Animated } from 'react-native';
2+
3+
export default Animated.View;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Animated from 'react-native-reanimated';
2+
3+
export default Animated.View;

src/animated/AnimatedView/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ComponentProps, ComponentType } from 'react';
2+
import type { AnimateStyle } from 'react-native-reanimated';
3+
import type { ViewProps, ViewStyle, Animated } from 'react-native';
4+
5+
interface Props extends Omit<ViewProps, 'style'> {
6+
style?:
7+
| AnimateStyle<ViewStyle>
8+
| ComponentProps<typeof Animated.View>['style'];
9+
}
10+
11+
let AnimatedView: ComponentType<Props>;
12+
13+
// @ts-ignore
14+
if (global._WORKLET) {
15+
AnimatedView = require('./Reanimated').default;
16+
} else {
17+
AnimatedView = require('./Animated').default;
18+
}
19+
20+
export default AnimatedView;

src/animated/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default as AnimatedView } from './AnimatedView';
2+
export { default as useAnimatedPresence } from './useAnimatedPresence';
3+
export { default as useAnimatedStyles } from './useAnimatedStyles';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let useAnimatedPresence:
2+
| typeof import('./useAnimatedPresence').default
3+
| typeof import('./useReanimated2Presence').default;
4+
5+
// @ts-ignore
6+
if (global._WORKLET) {
7+
useAnimatedPresence = require('./useReanimated2Presence').default;
8+
} else {
9+
useAnimatedPresence = require('./useAnimatedPresence').default;
10+
}
11+
12+
export default useAnimatedPresence;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { usePresence } from 'framer-motion';
2+
import { useEffect, useRef } from 'react';
3+
import { Animated } from 'react-native';
4+
5+
const { timing } = Animated;
6+
7+
type TimingConfig = Partial<Omit<Animated.TimingAnimationConfig, 'toValue'>>;
8+
9+
const defaultConfig: TimingConfig = {
10+
duration: 300,
11+
};
12+
13+
export default function useAnimatedPresence(
14+
timingConfig: TimingConfig = defaultConfig
15+
) {
16+
const animatedIsPresent = useRef(new Animated.Value(0)).current;
17+
18+
const [isPresent, safeToRemove] = usePresence();
19+
20+
useEffect(() => {
21+
timing(animatedIsPresent, {
22+
toValue: isPresent ? 1 : 0,
23+
duration: timingConfig.duration,
24+
useNativeDriver: true,
25+
}).start(({ finished }) => {
26+
if (finished && !isPresent && safeToRemove) {
27+
safeToRemove();
28+
}
29+
});
30+
}, [animatedIsPresent, isPresent, safeToRemove, timingConfig.duration]);
31+
32+
return animatedIsPresent;
33+
}

src/hooks/useAnimatedPresence.ts renamed to src/animated/useAnimatedPresence/useReanimated2Presence.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { usePresence } from 'framer-motion';
22
import {
33
runOnJS,
4-
withTiming,
5-
withSequence,
64
useDerivedValue,
5+
withSequence,
6+
withTiming,
77
WithTimingConfig,
88
} from 'react-native-reanimated';
99

10-
export default function useAnimatedPresence(
11-
timingConfig: WithTimingConfig = {
12-
duration: 300,
13-
}
10+
export default function useReanimated2Presence(
11+
timingConfig: WithTimingConfig = { duration: 50 }
1412
) {
1513
const [isPresent, safeToRemove] = usePresence();
1614

0 commit comments

Comments
 (0)