Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native Animated Accordion/ Drawer/ Drop-down/ Collapsible-card

Tags:

react-native

I want to implement an animated accordion list/ drawer / drop-down menu / collapsible card.

The animation should be performant and look like this:

enter image description here

like image 846
Vipul Avatar asked Nov 24 '25 03:11

Vipul


1 Answers

After a lot of searching, I could find many libraries. But I wanted to implement it without any library. Also, some tutorials showed how to build one, but they were not performant.

Finally, this is how I implemented it. The complete snack code is here: https://snack.expo.dev/@vipulchandra04/a85348

I am storing isOpen (whether the menu is open or closed) in a state. Then changing that state on button press. I am using the LayoutAnimation API in React-Native to animate the opening and closing of the list. LayoutAnimation runs the animation natively, thus it is performant.

const Accordion = ({ title, children }) => {
  const [isOpen, setIsOpen] = useState(false);

  const toggleOpen = () => {
    setIsOpen(value => !value);
    LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
  }

  return (
    <>
      <TouchableOpacity onPress={toggleOpen} activeOpacity={0.6}>
        {title}
      </TouchableOpacity>
      <View style={[styles.list, !isOpen ? styles.hidden : undefined]}>
        {children}
      </View>
    </>
  );
};

const styles = StyleSheet.create({
  hidden: {
    height: 0,
  },
  list: {
    overflow: 'hidden'
  },
});

enter image description here

like image 138
Vipul Avatar answered Nov 25 '25 17:11

Vipul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!