|
1 |
| -import React, {useRef, useState} from 'react' |
2 |
| -import {Transition, Transitioning} from 'react-native-reanimated' |
3 |
| -import {Card} from '../Card' |
4 |
| -import {CardContainer} from '../CardContainer' |
5 |
| -import { |
6 |
| - animationDuration, |
7 |
| - collapsedHeight, |
8 |
| - expandedHeight, |
9 |
| - initialHeight, |
10 |
| -} from '../constants' |
11 |
| - |
12 |
| -const transition = ( |
13 |
| - <Transition.Change interpolation="easeInOut" durationMs={animationDuration} /> |
14 |
| -) |
| 1 | +import React from 'react'; |
| 2 | +import Animated, { |
| 3 | + useSharedValue, |
| 4 | + withTiming, |
| 5 | + useAnimatedStyle, |
| 6 | + Easing, |
| 7 | +} from 'react-native-reanimated'; |
| 8 | +import {Card} from '../Card'; |
| 9 | +import {CardContainer} from '../CardContainer'; |
| 10 | +import {animationDuration, collapsedHeight, expandedHeight} from '../constants'; |
15 | 11 |
|
16 | 12 | export const Reanimated2 = ({heavyLoad}) => {
|
17 |
| - const ref = useRef() |
18 |
| - let [height, setHeight] = useState(initialHeight) |
| 13 | + const height = useSharedValue(collapsedHeight); |
| 14 | + |
| 15 | + const config = { |
| 16 | + duration: animationDuration, |
| 17 | + easing: Easing.bezier(0.5, 0.01, 0, 1), |
| 18 | + }; |
| 19 | + |
| 20 | + const style = useAnimatedStyle(() => { |
| 21 | + return { |
| 22 | + height: withTiming(height.value, config), |
| 23 | + }; |
| 24 | + }); |
19 | 25 |
|
20 | 26 | const onPress = () => {
|
21 |
| - ref.current.animateNextTransition() |
22 |
| - setHeight(height === expandedHeight ? collapsedHeight : expandedHeight) |
23 |
| - } |
| 27 | + height.value = |
| 28 | + height.value === expandedHeight ? collapsedHeight : expandedHeight; |
| 29 | + }; |
24 | 30 |
|
25 | 31 | return (
|
26 |
| - <Transitioning.View ref={ref} transition={transition}> |
| 32 | + <> |
27 | 33 | <CardContainer>
|
28 | 34 | <Card />
|
29 | 35 | </CardContainer>
|
30 | 36 |
|
31 |
| - <CardContainer style={{height}} onPress={onPress}> |
32 |
| - <Card expandable heavyLoad={heavyLoad} /> |
| 37 | + <CardContainer onPress={onPress}> |
| 38 | + <Animated.View style={style}> |
| 39 | + <Card expandable heavyLoad={heavyLoad} /> |
| 40 | + </Animated.View> |
33 | 41 | </CardContainer>
|
34 | 42 |
|
35 | 43 | <CardContainer>
|
36 | 44 | <Card />
|
37 | 45 | </CardContainer>
|
38 |
| - </Transitioning.View> |
39 |
| - ) |
40 |
| -} |
| 46 | + </> |
| 47 | + ); |
| 48 | +}; |
0 commit comments