Skip to content

Commit 03a9c16

Browse files
author
Henning Hall
committed
reanimated 2
1 parent c440d86 commit 03a9c16

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/libs/Reanimated2.js

+33-25
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
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';
1511

1612
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+
});
1925

2026
const onPress = () => {
21-
ref.current.animateNextTransition()
22-
setHeight(height === expandedHeight ? collapsedHeight : expandedHeight)
23-
}
27+
height.value =
28+
height.value === expandedHeight ? collapsedHeight : expandedHeight;
29+
};
2430

2531
return (
26-
<Transitioning.View ref={ref} transition={transition}>
32+
<>
2733
<CardContainer>
2834
<Card />
2935
</CardContainer>
3036

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>
3341
</CardContainer>
3442

3543
<CardContainer>
3644
<Card />
3745
</CardContainer>
38-
</Transitioning.View>
39-
)
40-
}
46+
</>
47+
);
48+
};

0 commit comments

Comments
 (0)