gorhom / react-native-bottom-sheet

A performant interactive bottom sheet with fully configurable options 🚀
https://ui.gorhom.dev/components/bottom-sheet
MIT License
6.59k stars 734 forks source link

[v4] | [v2] BottomSheetModal failing to present on iOS #1560

Open walterholohan opened 10 months ago

walterholohan commented 10 months ago

Bug

We just recently updated RN to 0.72.5 and react-native-reanimated to 3.5.4 and released it to production over the weekend. And then we started to get reports from our users that the modals were failing to appear. I will attach some videos below but we were unable to reproduce it on a simulator or a real device however we have ~150,000 MAU so after 24 hours we had over 10 users who were affected.

We had users on iOS 16.x and iOS 17.x who were affected

To unblock our users I had to set animateOnMount to false which makes me believe the issue is with the opening animation of the modal and reanimated

Environment info

Library Version
@gorhom/bottom-sheet 4.4.7
react-native 0.72.5
react-native-reanimated 3.5.4
react-native-gesture-handler 2.13.1

Steps To Reproduce

  1. User tries to press on Link Activity and you can see the modal briefly flashes
  2. User tries to press on Referral Code and again the modal flashes and closes

https://github.com/gorhom/react-native-bottom-sheet/assets/5293650/746ad438-0a85-436a-98c1-d59f051ca18c

Reproducible sample code

can attach an example of code if necessary but I was just following to docs for BottomSheetModal
SrAnthony commented 10 months ago

Same happening to us, affecting at least 15 users so far. We couldn't get a repro yet either.

Bottom sheet 4.5.1 React native 0.72.5 Reanimated 3.5.4

LouisKraemer commented 10 months ago

Same here, animateOnMount to false does fix the issue but really low number of users affected.

walterholohan commented 10 months ago

Thanks @SrAnthony and @LouisKraemer good to hear that you are also experiencing the same issue for your users. I haven't been able to deepdive into the animation implementation but hopefully @gorhom can give some insight or potential avenue's we can explore for a fix

karbone4 commented 10 months ago

Same here, we are also using expo sdk 49. It also happens on android

enchorb commented 10 months ago

Same issue here, more and more users are starting to complain about this - also unable to reproduce for us on simulator or our testing devices. This is happening for our users on both v4 and v5.

hatem-72 commented 10 months ago

Some of our users are affected as well. I think I managed to reproduce it, at least on IOS: Activate the Reduce Motion accessibility setting in your Iphone, restart your app then bottomsheets are not opening anymore.

Reanimated recently added support for this setting -> https://docs.swmansion.com/react-native-reanimated/docs/guides/accessibility/

walterholohan commented 10 months ago

wow great spot @hatem-72 , did you manage to create a patch for this lib?

hatem-72 commented 10 months ago

For the moment, I've only disabled the opening animation for users with reduced motion :

import { useReducedMotion } from 'react-native-reanimated';
import { BottomSheetModal } from '@gorhom/bottom-sheet';

// ...

function MyComponent() {
    const reducedMotion = useReducedMotion();

    return <BottomSheetModal
        // ...
        animateOnMount={!reducedMotion}
    >
        // ...
    </BottomSheetModal>
}
enchorb commented 10 months ago

@hatem-72 great find! reached out to 2 users who were having this issue and they both had it enabled.

walterholohan commented 10 months ago

Can we buy @hatem-72 a coffee?

karbone4 commented 10 months ago

Thanks @hatem-72 for the fix.

I have another bug, I can't dismiss the bottom modal 2 times. If I dismiss, open it again, I can't dismiss it. I found a workaround by overriding ref :

const reducedMotion = useReducedMotion();
useImperativeHandle(
    bottomModalRef,
    () => ({
        dismiss: () => {
            if (reducedMotion) {
                ref?.current?.snapToPosition(0);
            } else {
                ref?.current?.dismiss();
            }
        }
    }),
    [reducedMotion]
);
walterholohan commented 10 months ago

@karbone4 I am unable to reproduce your bug. It works for me

leymytel commented 10 months ago

Hello everyone!

Setting animateOnMount to false causes many weird issues (can't dismiss the modal, sometimes you need to click twice to get it open) . Another temporary fix is to add this patch (thanks to @efstathiosntonas) to override reduced motion to false in reanimated 3.5.4:

diff --git a/node_modules/react-native-reanimated/src/reanimated2/PlatformChecker.ts b/node_modules/react-native-reanimated/src/reanimated2/PlatformChecker.ts
index 9b3fcb1..0111380 100644
--- a/node_modules/react-native-reanimated/src/reanimated2/PlatformChecker.ts
+++ b/node_modules/react-native-reanimated/src/reanimated2/PlatformChecker.ts
@@ -49,5 +49,5 @@ export function isReducedMotion() {
       ? // @ts-ignore Fallback if `window` is undefined.
         !window.matchMedia('(prefers-reduced-motion: no-preference)').matches
       : false
-    : (global as localGlobal)._REANIMATED_IS_REDUCED_MOTION ?? false;
+    : false;
 }
nihilenz commented 10 months ago

Also observed on a device running Android 10 with reduce motion option enabled

levibuzolic commented 10 months ago

Also hitting this issue, rather than disabling the reduce motion detection for Reanimated globally we went with conditionally supplying our own animation config only when required. There are still some minor quirks when dismissing the sheet, but it does at least open and work correctly:

const reduceMotionEnabled = useReducedMotion();
const overrideConfig = useBottomSheetSpringConfigs({
  damping: 500,
  stiffness: 1000,
  mass: 3,
  overshootClamping: true,
  restDisplacementThreshold: 10,
  restSpeedThreshold: 10,
  reduceMotion: ReduceMotion.Never,
});

return (
  <BottomSheetModal
    /*
    There's a bug in BottomSheet which means that sheets will fail to open when reduce motion is enabled. Manually
    providing an animation config with `reduceMotion: ReduceMotion.Never` fixes this, but it does introduce a bit
    of jank when dismissing the sheet. This is a tradeoff we're willing to make for the sake of sheets at least
    working when reduce motion is enabled.

    @see https://github.com/gorhom/react-native-bottom-sheet/issues/1560
    */
    animationConfigs={reduceMotionEnabled ? overrideConfig : undefined}
    // other config here...
  />
);

The animation config was lifted from https://github.com/gorhom/react-native-bottom-sheet/blob/65b5dc03e673c25b3b41af954e1f13fe7668a339/src/constants.ts#L68-L75 to keep thre behaviour the same as before, I noticed that there's a platform fork and on Android the animation using a timing config, so you could probably use that based on platform, but we were happy enough with the spring config for both platforms.

Gyogle commented 10 months ago

In fact, I think this problem is fundamentally a problem with the changes in Reanimated that were made when the BottomSheet version was upgraded, although BottomSheet itself handles reduceOption in a way.

Simple problem solving can be done in the other comment1 and comment2 posted above. But I think the fundamental solution is to allow Reanimated itself to control the relevant settings.

So I posted my opinion and the cause I identified on Reanimated.(https://github.com/software-mansion/react-native-reanimated/discussions/5314#issuecomment-5314) We ask for your interest. Let’s solve this problem related to accessibility together!

andac-ozcan commented 10 months ago

As I see, reduced motion is also enabled if low power mode is active and device battery is <= 10%. Almost all screenshots from our user reports shows that their device battery are <= 10%. Apple mentions this here, under Low Power Mode title. So it may be a wider issue than we think, not related with accessibility settings only.

nguyenhoanganhdev commented 9 months ago

I think, reduced motion is also enabled if low power mode is active and device battery is <= 10%. Almost all screenshots from our user bug reports shows their device battery is <= 10%. Apple mentions this here, under Low Power Mode title. So it may be a wider issue than we think, not related with accessibility settings only.

Thank you very much. This is the root cause of my case

CostasCF commented 9 months ago

Although I tested setting reducedMotion to false, it's not a viable solution because the problem still appears from time to time. I think it's a fundamental problem with how Reanimated and BottomSheet works as @Gyogle states.

mattijsf commented 9 months ago

Inspired by the above answer, main difference being the import of ANIMATION_CONFIGS:

import { useBottomSheetSpringConfigs } from "@gorhom/bottom-sheet"
import { ANIMATION_CONFIGS } from "@gorhom/bottom-sheet/src/constants"
import { Platform } from "react-native"
import { ReduceMotion, WithTimingConfig, useReducedMotion } from "react-native-reanimated"

/**
 * https://github.com/gorhom/react-native-bottom-sheet/issues/1560
 *
 * Usage:
 * ```
 * const animationConfigs = useBottomSheetAnimationConfigsReducedMotionWorkaround()
 * <BottomSheetModal animationConfigs={animationConfigs} ...>
 * ```
 *
 * @returns
 */
export function useBottomSheetAnimationConfigsReducedMotionWorkaround():
  | WithTimingConfig
  | undefined {
  const reducedMotion = useReducedMotion()
  const iOSAnimationConfigWithoutReducedMotion = useBottomSheetSpringConfigs({
    ...ANIMATION_CONFIGS,
    reduceMotion: ReduceMotion.Never,
  })

  if (Platform.OS !== "ios" || !reducedMotion) return undefined

  return iOSAnimationConfigWithoutReducedMotion
}
mattijsf commented 9 months ago

@gorhom would the above be worth a v4 patch release?

salman-ar-sar commented 9 months ago

Tried the fix by @levibuzolic / @mattijsf. That works most of the time but got some weird issues when having multiple BottomSheets. Also tried disabling motion, that was very weird. Currently using the reanimated patch by @leymytel, that seems to be working fine.

@gorhom It would be really great if you can look into this.

jlmosconi commented 9 months ago

In my case, one of our users had the "Reduce Motion" option enabled and that caused the modals to not open. It was solved as follows

import {useReducedMotion} from 'react-native-reanimated'

...
const reducedMotion = useReducedMotion()

<BottomModal animateOnMount={!reducedMotion}

@hatem-72 Thanks!!

marshallcool commented 9 months ago

@leymytel this patch includes animation for users who have enabled motion reduction, I think not everyone will be happy with this behavior

YaoHuiJi commented 8 months ago

This issue will cause a lot of users to be completely unable to use the Apps that uses this library. If you have time, can you please have a look? @gorhom 💗

keithluchtel commented 8 months ago

I am experiencing the same issue with version 3.5.4, but am not having issues with 3.3.0

henryteng07 commented 8 months ago

@karbone4 Same issue where I can't close modal the second time... I tried using your fix but it doesn't seem to be working. Any advice? Thanks!

`const reducedMotion = useReducedMotion();

useImperativeHandle( bottomSheetModalRef, () => ({ dismiss: () => { if (reducedMotion) { ref?.current?.snapToPosition(0); } else { ref?.current?.dismiss(); } }, }), [reducedMotion], );

return ( <BottomSheetModal animateOnMount={!reducedMotion} backdropComponent={BottomSheetBackdrop} ref={bottomSheetModalRef} index={0} snapPoints={snapPoints}>

{ bottomSheetModalRef.current?.dismiss(); }}> `
henryteng07 commented 8 months ago

@karbone4 I got it! Just use ref.current.close() instead of ref.current.dismiss()

mMarcos208 commented 7 months ago

Is this working in the latest version? Same issue.

TomWq commented 7 months ago

Is this working in the latest version? Same issue.

jenskuhrjorgensen commented 7 months ago

Hello everyone!

Setting animateOnMount to false causes many weird issues (can't dismiss the modal, sometimes you need to click twice to get it open) . Another temporary fix is to add this patch (thanks to @efstathiosntonas) to override reduced motion to false in reanimated 3.5.4:

diff --git a/node_modules/react-native-reanimated/src/reanimated2/PlatformChecker.ts b/node_modules/react-native-reanimated/src/reanimated2/PlatformChecker.ts
index 9b3fcb1..0111380 100644
--- a/node_modules/react-native-reanimated/src/reanimated2/PlatformChecker.ts
+++ b/node_modules/react-native-reanimated/src/reanimated2/PlatformChecker.ts
@@ -49,5 +49,5 @@ export function isReducedMotion() {
       ? // @ts-ignore Fallback if `window` is undefined.
         !window.matchMedia('(prefers-reduced-motion: no-preference)').matches
       : false
-    : (global as localGlobal)._REANIMATED_IS_REDUCED_MOTION ?? false;
+    : false;
 }

For me the solution was to disable reduced motion support with this patch, which to me seems like a fine solution for now. Before bumping reanimated to >=3.5.0 we didn't support reduced motion (at least not for our reanimated animations) and with this patch we still don't. Users are not going to notice any degradation in UX. When this has been properly fixed in bottomsheet and/or reanimated we can remove this patch and increase UX for accessibility users.

obasille commented 7 months ago

I'm having the same issue with my app. Reported by a user. Totally caught me by surprise. This library is really a good one, I hope this issue will be addressed as it affects virtually every app using BottomSheetModal.

pckz commented 7 months ago

Some of our users are having this problem. We could replicate activating "Reduce Motion" in a old device (14.7) and users reported the problem were using 16.6.1. In a device with 17.2 Reduce motion option not affect this bug. So, the solutions proposed here were not implemented in last version of this lib? thanks!

regalstreak commented 6 months ago

@gorhom this is a P0 bug, is this being worked on?

fobos531 commented 6 months ago

I've submitted a PR which should fix this in v5 of the library: https://github.com/gorhom/react-native-bottom-sheet/pull/1743

obasille commented 6 months ago

FYI a simple workaround was posted in another issue about the same problem: https://github.com/gorhom/react-native-bottom-sheet/issues/1674#issuecomment-1959923945

nhuesmann commented 5 months ago

Thanks @hatem-72 for the fix.

I have another bug, I can't dismiss the bottom modal 2 times. If I dismiss, open it again, I can't dismiss it. I found a workaround by overriding ref :

const reducedMotion = useReducedMotion();
useImperativeHandle(
    bottomModalRef,
    () => ({
        dismiss: () => {
            if (reducedMotion) {
                ref?.current?.snapToPosition(0);
            } else {
                ref?.current?.dismiss();
            }
        }
    }),
    [reducedMotion]
);

Thank you for this, I was facing the same issue and this resolved it.

github-actions[bot] commented 4 months ago

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.

nihilenz commented 4 months ago

not stale

christophby commented 4 months ago

The change from @hatem-72 should be included in the main code base of the bottom sheet.

github-actions[bot] commented 3 months ago

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.

christophby commented 3 months ago

Not stale

nicolascavallin commented 3 months ago

With last release it was solved.

I've checked on my iOS app and it works as expected.

This is the commit


This issue should be closed @walterholohan @gorhom

kimsergey commented 3 months ago

This problem is still relevant. On version 4.6.3 it is much less common, but still exists

"react-native": "0.73.4", "react-native-reanimated": "3.8.0", "@gorhom/bottom-sheet": "4.6.3"

mihaibulic2 commented 2 months ago

I fixed this in my app and wanted to share all I learned (some of this may be repeating what was said above):

BACKGROUND

MY WORKAROUND (THEORY) What if we don't have the bottom sheet actually transitions when reducedMotion is on? This would side step the issue entirely. We have to worry about 2 cases: showing and dismissing. For showing, it's easy: just set animateOnMount={false}. This does seem to cause a problem for dismissing: when we set animateOnMount={false}, the bottomSheet ref's dismiss function can only dismiss the modal once for some reason. As a workaround for that, if we get a new ref on each dismissal, then each ref would only need to dismiss once. We can do that by changing the key value on each dismissal, forcing React to do a rerender. Code below!

MY WORKAROUND'S CODE Here is a stripped down version of my code. Please lmk if it works for you or if you spot any problems!


const [bottomSheetBumpKey, setBottomSheetBumpKey] = useState<number>(0);
const reduceMotionEnabled = useReducedMotion();

return (<BottomSheetModalProvider>
    <BottomSheetModal
        index={0}
        ref={bottomSheetModalRef}
        snapPoints={bottomModalSnapPoints}
        backgroundComponent={null} // This is required to make the modal dark

        // HACKS! (needed as of v4.6.3)
        // 1. On android: when the user (or battery saver) enables "Reduced Motion" or "Reduced Animation", it makes the bottom sheet flicker / never appear.
        //    -- video: https://www.loom.com/share/d131efdd0b8e4e12b30afb7dd605a7e7
        //    -- details: https://github.com/gorhom/react-native-bottom-sheet/issues/1560#issuecomment-1774254176
        //    -- workaround: manually disable animations in this case (animateOnMount=false)
        // 2. On android: when animations are disabled (animateOnMount=false), the bottom sheet ref can only dismiss the UI once.
        //    -- workaround: change the key when we dismiss (if animateOnMount=false), forcing a rerender + a new ref
        // NOTE: there was an *attempt* to fix this in Bottom Sheet v4.6.3, but it's buggy and causes crashes - next version should fix?
        key={`bottom-sheet-modal-${bottomSheetBumpKey}`}
        animateOnMount={!reduceMotionEnabled}
        onDismiss={() => {
          if (reduceMotionEnabled) {
            setBottomSheetBumpKey((existing) => existing + 1);
          }
        }}>
          <MyCoolComponent/>
        )}
      </BottomSheetModal>
    </BottomSheetModalProvider>);
yolpsoftware commented 1 month ago

Can confirm, issue is no longer present in release 4.6.3, together with "react-native-reanimated": "3.6.2".

github-actions[bot] commented 3 weeks ago

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.

nihilenz commented 3 weeks ago

not stale