The goal:
create a dropdown view, which when collapsed, is 75px in height. when expanded, its 225px in height. When the button that starts the animations is pressed, it runs two animations in parallel, a timing function for height, and one for opacity. the second part of the goal is to animated the opacity of the lower part of the dropdown is made available on expanded == true.
the problem:
the animation runs as expected when expanding the view, no issues. but when collapsing the dropdown, seemingly only the opacity animation runs. the issue is with the height, not being properly reflected in the view. it stays the same height as when expanded.
here's the component:
const ListItem = (props) => {
const [checkInModal, setCheckInModal] = useState(false);
const [animatedHeight, setAnimatedHeight] = useState(new Animated.Value(0))
const [animatedOpacity] = useState(new Animated.Value(0))
const [expanded, setExpanded] = useState(false);
const toggleDropdown = () => {
console.log("BEFORE ANIMATION =>" + "\n" + "expanded: " + expanded + "\n" + "animatedHeight: " + JSON.stringify(animatedHeight) + "\n" + "animatedOpacity: " + JSON.stringify(animatedOpacity))
if (expanded == true) {
// collapse dropdown
Animated.parallel([
Animated.timing(animatedHeight, {
toValue: 0,
duration: 200,
}),
Animated.timing(animatedOpacity, {
toValue: 0,
duration: 400,
})
]).start()
setTimeout(() => console.log("AFTER ANIMATION =>" + "\n" + "animatedHeight: " + JSON.stringify(animatedHeight) + "\n" + "animatedOpacity: " + JSON.stringify(animatedOpacity)), 3000)
// This alone works properly*
// Animated.timing(animatedHeight, {
// toValue: 0,
// duration: 200,
// }).start()
} else {
// expand dropdown
Animated.sequence([
Animated.timing(animatedHeight, {
toValue: 100,
duration: 200,
}),
Animated.timing(animatedOpacity, {
toValue: 100,
duration: 400,
})
]).start()
setTimeout(() => console.log("AFTER ANIMATION =>" + "\n" + "animatedHeight: " + JSON.stringify(animatedHeight) + "\n" + "animatedOpacity: " + JSON.stringify(animatedOpacity)), 3000)
// This alone works properly*
// Animated.timing(animatedHeight, {
// toValue: 100,
// duration: 200,
// }).start()
}
setExpanded(!expanded)
}
const interpolatedHeight = animatedHeight.interpolate({
inputRange: [0, 100],
outputRange: [75, 225]
})
const interpolatedOpacity = animatedOpacity.interpolate({
inputRange: [0, 100],
outputRange: [0.0, 1.0]
})
return (
<Animated.View
style={[styles.container, { height: interpolatedHeight }]}
>
<View style={{ flexDirection: 'row', justifyContent: 'space-between', }}>
<View style={styles.leftContainer}>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={styles.title}>{props.title}</Text>
</View>
<Text style={styles.subtitle}>{time()}</Text>
</View>
<View style={styles.rightContainer}>
<TouchableOpacity onPress={() => toggleDropdown()} style={styles.toggleBtn}>
<Image source={require('../assets/img/chevron-down.png')} resizeMode={'contain'} style={styles.chevron} />
</TouchableOpacity>
</View>
</View>
{expanded == true ? (
<Animated.View style={[styles.bottomContainer, { opacity: interpolatedOpacity }]}>
<Components.BodyText text="Subject:" style={{ fontFamily: Fonts.OPENSANS_BOLD }} />
<Components.BodyText text={props.subject} />
</Animated.View>
) : null}
</Animated.View>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: '#fff',
borderRadius: 25,
width: width * 0.95,
marginBottom: 5,
marginHorizontal: 5,
paddingVertical: 15,
paddingHorizontal: 15
},
leftContainer: {
justifyContent: 'space-between',
},
rightContainer: {
flexDirection: 'row',
alignItems: 'center'
},
title: {
fontFamily: Fonts.OPENSANS_BOLD,
fontSize: 20,
color: '#454A66'
},
subtitle: {
color: '#454A66',
fontSize: 14
},
typeIcon: {
height: 25,
width: 25
},
chevron: {
height: 15,
width: 15
},
toggleBtn: {
borderWidth: 1,
borderColor: Colors.PRIMARY_DARK,
borderRadius: 7,
paddingTop: 4,
paddingBottom: 2.5,
paddingHorizontal: 4,
marginLeft: 10
},
bottomContainer: {
marginVertical: 20
},
buttonContainer: {
flexDirection: 'row',
width: 250,
justifyContent: 'space-between',
alignSelf: 'center',
marginVertical: 20
},
noShadow: {
elevation: 0,
shadowOffset: {
width: 0,
height: 0
},
shadowRadius: 0,
}
});
export default ListItem;
The Console Logs:
When collapsed, pressing the button to expand logs this in the console:
LOG BEFORE ANIMATION =>
expanded: false
animatedHeight: 0
animatedOpacity: 0
LOG AFTER ANIMATION =>
animatedHeight: 100
animatedOpacity: 100
When expanded, pressing the button to collapse logs this in the console:
LOG BEFORE ANIMATION =>
expanded: true
animatedHeight: 100
animatedOpacity: 100
LOG AFTER ANIMATION =>
animatedHeight: 100
animatedOpacity: 100
Not exactly sure what thats about, given that like said, the opacity animation works on collapse and expansion. If the height animation isn't touched at all, except for taking it out of the Animated.parallel(), then the height animation works as expected on expand & collapse.
Any ideas on whats going on here?