react-native-bottom-sheet icon indicating copy to clipboard operation
react-native-bottom-sheet copied to clipboard

Animate bottom sheet when unmounts

Open andrecrimb opened this issue 2 years ago • 39 comments

Feature Request

Why it is needed

Bottom sheet has currently available the animateOnMount prop which:

... initially mount the sheet closed and when it is mounted and calculated the layout, it will snap to initial snap point index.

Our team came across a use case, where we need to have the same animation but when the BS unmounts (some sort of exiting animation). I believe this feature is quite beneficial for those who need to handle several bottom sheets and seek some sort of a continuation flow. (eg. one BS slides down and another one slides up)

Possible implementation

Reanimated 2 allows animated views to have exiting animations.

Code sample

I imagine the use of animatedOnUnmount (or animateOnExiting) something like:

 return (
   <BottomSheet
      animateOnExiting={true}
      {...otherProps}
    />
)

andrecrimb avatar Aug 15 '22 10:08 andrecrimb

+1

Norcy avatar Aug 21 '22 15:08 Norcy

For now you can do this:

const App = () => {
    const [isBottomSheetMounted, setBottomSheetMounted] = useState(false)
    const [isBottomSheetAnimationCompleted, setBottomSheetAnimationCompleted] = useState(false)

    const handleBottomSheetChange = (index: number) => {
        if (!isBottomSheetAnimationCompleted && index === 0) {
            setBottomSheetAnimationCompleted(true)
        }

        if (index === -1) {
            setBottomSheetMounted(false)
        }
    }
    
    return (
      <View>
       {isBottomSheetMounted && (
          <Portal>
            <BottomSheet onChange={handleBottomSheetChange} />
          </Portal>
       )}
     </View>
    )
}

export default App

francois-pasquier avatar Aug 26 '22 13:08 francois-pasquier

@francois-pasquier thanks for your comment 🙏🏽 How a Portal is adding such animation (sliding down), as mentioned on the description?

andrecrimb avatar Sep 05 '22 09:09 andrecrimb

Well, the Portal is only there to mount the bottom sheet on the root node. When handleBottomSheetChange is called, it dismounts the Portal only after the animation is completed.

Does it not work as expected?

francois-pasquier avatar Sep 05 '22 09:09 francois-pasquier

@francois-pasquier thanks for your reply!

For me isn't clear how your suggestion can cover cases, eg., where a component with the BS conditionally unmounts, because of its parents.

Eg. Here's how we can achieve such using Animated.View

import React from 'react';
import { View, Button } from 'react-native';
import BottomSheet from '@gorhom/bottom-sheet';
import Animated, { SlideOutDown } from 'react-native-reanimated';

const ChildrenComponentWithBottomSheet = () => {
  const snapPoints = React.useMemo(() => [600, 700], []);
  return (
    <Animated.View style={{ flex: 1 }} exiting={SlideOutDown}>
      <BottomSheet snapPoints={snapPoints}>
        <View />
      </BottomSheet>
    </Animated.View>
  );
};

const ChildrenComponent = () => (
  <View style={{ flex: 1 }}>
    <ChildrenComponentWithBottomSheet />
  </View>
);

const SomeComponentOnTheAppTree = () => {
  const [toggle, setToggle] = React.useState(true);

  return (
    <View
      style={{
        flex: 1,
        backgroundColor: 'grey',
      }}
    >
      <View style={{ position: 'absolute', top: 100, zIndex: 9999, left: 10 }}>
        <Button
          color="red"
          title={`Conditionally rendering children with BS.\n ${
            toggle ? 'Mounted' : 'Unmounted'
          }`}
          onPress={() => setToggle((v) => !v)}
        />
      </View>
      {toggle ? <ChildrenComponent /> : null}
    </View>
  );
};

https://user-images.githubusercontent.com/16760718/190382025-c427693d-9597-4d79-b496-a3e956208301.mov

andrecrimb avatar Sep 15 '22 10:09 andrecrimb

+1

ilkkaylik avatar Sep 28 '22 06:09 ilkkaylik

👍

wadey1 avatar Sep 28 '22 06:09 wadey1

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Oct 28 '22 09:10 github-actions[bot]

👍

ilkkaylik avatar Oct 28 '22 09:10 ilkkaylik

👍🏽

andrecrimb avatar Oct 28 '22 13:10 andrecrimb

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 29 '22 09:11 github-actions[bot]

The issue still exists

pedpess avatar Nov 29 '22 10:11 pedpess

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Dec 31 '22 09:12 github-actions[bot]

👍🏽

andrecrimb avatar Dec 31 '22 09:12 andrecrimb

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jan 31 '23 09:01 github-actions[bot]

please GH don't close this 😢

pedpess avatar Jan 31 '23 09:01 pedpess

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Mar 04 '23 09:03 github-actions[bot]

👍🏽

andrecrimb avatar Mar 04 '23 11:03 andrecrimb

.

gstcarv avatar Mar 22 '23 20:03 gstcarv

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Apr 22 '23 09:04 github-actions[bot]

🙏🏽

andrecrimb avatar Apr 22 '23 19:04 andrecrimb

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar May 23 '23 09:05 github-actions[bot]

still valid

ilkkaylik avatar May 23 '23 09:05 ilkkaylik

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jun 23 '23 09:06 github-actions[bot]

🙏🏽

andrecrimb avatar Jun 23 '23 11:06 andrecrimb

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jul 25 '23 09:07 github-actions[bot]

Still valid

andrecrimb avatar Jul 25 '23 09:07 andrecrimb

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Aug 25 '23 09:08 github-actions[bot]

Still valid.

andrecrimb avatar Aug 25 '23 09:08 andrecrimb

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Sep 26 '23 09:09 github-actions[bot]