Skip to content

[Android] ExpandableSection - fix inner component's animation not working #2990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions src/components/expandableSection/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, {useMemo} from 'react';
import {LayoutAnimation, StyleSheet} from 'react-native';
import React, {useMemo, useState} from 'react';
import {LayoutChangeEvent, StyleSheet} from 'react-native';
import {useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
import View from '../view';
import TouchableOpacity from '../touchableOpacity';
import {useDidUpdate} from 'hooks';

export type ExpandableSectionProps = {
/**
Expand Down Expand Up @@ -38,44 +38,57 @@ export type ExpandableSectionProps = {
*/

function ExpandableSection(props: ExpandableSectionProps) {
const {expanded, sectionHeader, children, top, testID} = props;
const {expanded, sectionHeader, onPress, children, top, testID} = props;
const [height, setHeight] = useState(0);
const animatedHeight = useSharedValue(0);

/**
* TODO: move to reanimated LayoutAnimation after updating to version 2.3.0
* after migration, trigger the animation only in useDidUpdate.
*/
const animate = () => {
LayoutAnimation.configureNext({...LayoutAnimation.Presets.easeInEaseOut, duration: 300});
};
const onLayout = (event: LayoutChangeEvent) => {
const onLayoutHeight = event.nativeEvent.layout.height;

const onPress = () => {
props.onPress?.();
animate();
if (onLayoutHeight > 0 && height !== onLayoutHeight) {
setHeight(onLayoutHeight);
}
};

useDidUpdate(() => {
animate();
}, [expanded]);
const expandableStyle = useAnimatedStyle(() => {
animatedHeight.value = expanded ? withTiming(height) : withTiming(0);

return {
height: animatedHeight.value
};
}, [expanded, height]);

const style = useMemo(() => [styles.hidden, expandableStyle], [expandableStyle]);

const accessibilityState = useMemo(() => {
return {expanded};
}, [expanded]);

const renderChildren = () => {
return (
<View reanimated style={style}>
<View absH onLayout={onLayout}>
{children}
</View>
</View>
);
};

return (
<View style={styles.container}>
{top && expanded && children}
<View style={styles.hidden}>
{top && renderChildren()}
<TouchableOpacity onPress={onPress} testID={testID} accessibilityState={accessibilityState}>
{sectionHeader}
</TouchableOpacity>
{!top && expanded && children}
{!top && renderChildren()}
</View>
);
}

export default ExpandableSection;

const styles = StyleSheet.create({
container: {
hidden: {
overflow: 'hidden'
}
});